Move special setWebhook handling to Telegram and clean up Request.

Make Request::encodeFile public, as it's needed to send files, letting users make use of it.
parent 03a5ab82
......@@ -19,6 +19,7 @@ use Longman\TelegramBot\Exception\TelegramException;
/**
* Class Request
*
* @method static ServerResponse setWebhook(array $data) Use this method to specify a url and receive incoming updates via an outgoing webhook. Whenever there is an update for the bot, we will send an HTTPS POST request to the specified url, containing a JSON-serialized Update. In case of an unsuccessful request, we will give up after a reasonable amount of attempts. Returns true.
* @method static ServerResponse deleteWebhook() Use this method to remove webhook integration if you decide to switch back to getUpdates. Returns True on success. Requires no parameters.
* @method static ServerResponse getWebhookInfo() Use this method to get current webhook status. Requires no parameters. On success, returns a WebhookInfo object. If the bot is using getUpdates, will return an object with the url field empty.
* @method static ServerResponse getMe() A simple method for testing your bot's auth token. Requires no parameters. Returns basic information about the bot in form of a User object.
......@@ -344,9 +345,9 @@ class Request
* @return resource
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
protected static function encodeFile($file)
public static function encodeFile($file)
{
$fp = fopen($file, 'r');
$fp = fopen($file, 'rb');
if ($fp === false) {
throw new TelegramException('Cannot open "' . $file . '" for reading');
}
......@@ -905,33 +906,6 @@ class Request
return self::send('getUpdates', $data);
}
/**
* Set webhook
*
* @link https://core.telegram.org/bots/api#setwebhook
*
* @param string $url
* @param array $data Optional parameters.
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function setWebhook($url = '', array $data = [])
{
$data = array_intersect_key($data, array_flip([
'certificate',
'max_connections',
'allowed_updates',
]));
$data['url'] = $url;
if (isset($data['certificate'])) {
self::assignEncodedFile($data, 'certificate', $data['certificate']);
}
return self::send('setWebhook', $data);
}
/**
* Use this method to edit text and game messages sent by the bot or via the bot (for inline bots).
*
......
......@@ -787,7 +787,19 @@ class Telegram
throw new TelegramException('Hook url is empty!');
}
$result = Request::setWebhook($url, $data);
$data = array_intersect_key($data, array_flip([
'certificate',
'max_connections',
'allowed_updates',
]));
$data['url'] = $url;
// If the certificate is passed as a path, encode and add the file to the data array.
if (!empty($data['certificate']) && is_string($data['certificate'])) {
$data['certificate'] = Request::encodeFile($data['certificate']);
}
$result = Request::setWebhook($data);
if (!$result->isOk()) {
throw new TelegramException(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment