Skip to content

Throw unique exception on invalid token #855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Exception/InvalidBotTokenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Longman\TelegramBot\Exception;

/**
* Thrown when bot token is invalid
*/
class InvalidBotTokenException extends TelegramException
{
/**
* InvalidBotTokenException constructor
*/
public function __construct()
{
parent::__construct("Invalid bot token!");
}
}
15 changes: 12 additions & 3 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use GuzzleHttp\Exception\RequestException;
use Longman\TelegramBot\Entities\File;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\InvalidBotTokenException;
use Longman\TelegramBot\Exception\TelegramException;

/**
Expand Down Expand Up @@ -457,13 +458,21 @@ public static function send($action, array $data = [])

self::limitTelegramRequests($action, $data);

$response = json_decode(self::execute($action, $data), true);
$raw_response = self::execute($action, $data);
$response = json_decode($raw_response, true);

if (null === $response) {
throw new TelegramException('Telegram returned an invalid response! Please review your bot name and API key.');
TelegramLog::debug($raw_response);
throw new TelegramException('Telegram returned an invalid response!');
}

return new ServerResponse($response, $bot_username);
$response = new ServerResponse($response, $bot_username);

if (!$response->isOk() && $response->getErrorCode() === 401 && $response->getDescription() === 'Unauthorized') {
throw new InvalidBotTokenException();
}

return $response;
}

/**
Expand Down