Handle methods that don't require parameters better.

Keep track of methods that need a dummy parameter for some cURL versions to function properly.
parent 52c453bb
...@@ -16,6 +16,13 @@ use Longman\TelegramBot\Entities\File; ...@@ -16,6 +16,13 @@ use Longman\TelegramBot\Entities\File;
use Longman\TelegramBot\Entities\ServerResponse; use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
/**
* Class Request
*
* @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.
*/
class Request class Request
{ {
/** /**
...@@ -63,12 +70,17 @@ class Request ...@@ -63,12 +70,17 @@ class Request
/** /**
* Available actions to send * Available actions to send
* *
* This is basically the list of all methods listed on the official API documentation.
*
* @link https://core.telegram.org/bots/api
*
* @var array * @var array
*/ */
private static $actions = [ private static $actions = [
'getUpdates', 'getUpdates',
'setWebhook', 'setWebhook',
'deleteWebhook', 'deleteWebhook',
'getWebhookInfo',
'getMe', 'getMe',
'sendMessage', 'sendMessage',
'forwardMessage', 'forwardMessage',
...@@ -98,10 +110,22 @@ class Request ...@@ -98,10 +110,22 @@ class Request
'editMessageText', 'editMessageText',
'editMessageCaption', 'editMessageCaption',
'editMessageReplyMarkup', 'editMessageReplyMarkup',
'getWebhookInfo',
'deleteMessage', 'deleteMessage',
]; ];
/**
* Some methods need a dummy param due to certain cURL issues.
*
* @see Request::addDummyParamIfNecessary()
*
* @var array
*/
private static $actions_need_dummy_param = [
'deleteWebhook',
'getWebhookInfo',
'getMe',
];
/** /**
* Initialize * Initialize
* *
...@@ -344,6 +368,7 @@ class Request ...@@ -344,6 +368,7 @@ class Request
public static function send($action, array $data = []) public static function send($action, array $data = [])
{ {
self::ensureValidAction($action); self::ensureValidAction($action);
self::addDummyParamIfNecessary($action, $data);
$bot_username = self::$telegram->getBotUsername(); $bot_username = self::$telegram->getBotUsername();
...@@ -366,6 +391,27 @@ class Request ...@@ -366,6 +391,27 @@ class Request
return new ServerResponse($response, $bot_username); return new ServerResponse($response, $bot_username);
} }
/**
* Add a dummy parameter if the passed action requires it.
*
* If a method doesn't require parameters, we need to add a dummy one anyway,
* because of some cURL version failed POST request without parameters.
*
* @link https://github.com/php-telegram-bot/core/pull/228
*
* @todo Would be nice to find a better solution for this!
*
* @param string $action
* @param array $data
*/
protected static function addDummyParamIfNecessary($action, array &$data)
{
if (in_array($action, self::$actions_need_dummy_param, true)) {
// Can be anything, using a single letter to minimise request size.
$data = ['d'];
}
}
/** /**
* Make sure the data isn't empty, else throw an exception * Make sure the data isn't empty, else throw an exception
* *
...@@ -410,21 +456,6 @@ class Request ...@@ -410,21 +456,6 @@ class Request
} }
} }
/**
* Returns basic information about the bot in form of a User object
*
* @link https://core.telegram.org/bots/api#getme
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function getMe()
{
// Added fake parameter, because of some cURL version failed POST request without parameters
// see https://github.com/php-telegram-bot/core/pull/228
return self::send('getMe', ['whoami']);
}
/** /**
* Use this method to send text messages. On success, the sent Message is returned * Use this method to send text messages. On success, the sent Message is returned
* *
...@@ -901,20 +932,6 @@ class Request ...@@ -901,20 +932,6 @@ class Request
return self::send('setWebhook', $data); return self::send('setWebhook', $data);
} }
/**
* Delete webhook
*
* @link https://core.telegram.org/bots/api#deletewebhook
*
* @return \Longman\TelegramBot\Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function deleteWebhook()
{
// Must send some arbitrary data for this to work for now...
return self::send('deleteWebhook', ['delete']);
}
/** /**
* Use this method to edit text and game messages sent by the bot or via the bot (for inline bots). * Use this method to edit text and game messages sent by the bot or via the bot (for inline bots).
* *
...@@ -1030,20 +1047,6 @@ class Request ...@@ -1030,20 +1047,6 @@ class Request
return $results; return $results;
} }
/**
* Use this method to get current webhook status.
*
* @link https://core.telegram.org/bots/api#getwebhookinfo
*
* @return Entities\ServerResponse
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function getWebhookInfo()
{
// Must send some arbitrary data for this to work for now...
return self::send('getWebhookInfo', ['info']);
}
/** /**
* Enable request limiter * Enable request limiter
* *
......
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