Skip to content

Commit 586d14c

Browse files
authored
Merge pull request #855 from jacklul/token_exception
Throw unique exception on invalid token and log raw response
2 parents 5676b75 + fade1d1 commit 586d14c

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Longman\TelegramBot\Exception;
12+
13+
/**
14+
* Thrown when bot token is invalid
15+
*/
16+
class InvalidBotTokenException extends TelegramException
17+
{
18+
/**
19+
* InvalidBotTokenException constructor
20+
*/
21+
public function __construct()
22+
{
23+
parent::__construct("Invalid bot token!");
24+
}
25+
}

src/Request.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use GuzzleHttp\Exception\RequestException;
1515
use Longman\TelegramBot\Entities\File;
1616
use Longman\TelegramBot\Entities\ServerResponse;
17+
use Longman\TelegramBot\Exception\InvalidBotTokenException;
1718
use Longman\TelegramBot\Exception\TelegramException;
1819

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

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

460-
$response = json_decode(self::execute($action, $data), true);
461+
$raw_response = self::execute($action, $data);
462+
$response = json_decode($raw_response, true);
461463

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

466-
return new ServerResponse($response, $bot_username);
469+
$response = new ServerResponse($response, $bot_username);
470+
471+
if (!$response->isOk() && $response->getErrorCode() === 401 && $response->getDescription() === 'Unauthorized') {
472+
throw new InvalidBotTokenException();
473+
}
474+
475+
return $response;
467476
}
468477

469478
/**

0 commit comments

Comments
 (0)