Unverified Commit 55c2e1a7 authored by Armando Lüscher's avatar Armando Lüscher Committed by GitHub

Merge branch 'develop' into 726-games

parents d15384ec 586d14cb
......@@ -112,6 +112,7 @@ class ServerResponse extends Entity
$result_object_types = [
'total_count' => 'UserProfilePhotos', //Response from getUserProfilePhotos
'stickers' => 'StickerSet', //Response from getStickerSet
'file_id' => 'File', //Response from getFile
'title' => 'Chat', //Response from getChat
'username' => 'User', //Response from getMe
......
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* 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!");
}
}
......@@ -14,6 +14,7 @@ use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Longman\TelegramBot\Entities\File;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\InvalidBotTokenException;
use Longman\TelegramBot\Exception\TelegramException;
/**
......@@ -463,13 +464,21 @@ class Request
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;
}
/**
......
......@@ -298,4 +298,71 @@ class ServerResponseTest extends TestCase
//... they are not finished...
}
public function getStickerSet()
{
return '{
"ok":true,
"result":{
"name":"stickerset_name",
"title":"Some name",
"contains_masks":false,
"stickers":[
{
"width":512,
"height":324,
"emoji":"\ud83d\ude33",
"set_name":"stickerset_name",
"thumb":{"file_id":"AAQEABOKTFsZAASfA4t3pp1_VlH1AAIC","file_size":3120,"width":128,"height":81},
"file_id":"CAADBAADzAIAAph_7gOATSb9ehxv5QI",
"file_size":14246
},
{
"width":419,
"height":512,
"emoji":"\u2764",
"set_name":"stickerset_name",
"thumb":{"file_id":"AAQEABMj8qoZAASePUHuDSJ2uGIKAAIC","file_size":3500,"width":105,"height":128},
"file_id":"CAADBAADzQIAAph_7gNPFguft4qtjAI",
"file_size":17814
},
{
"width":512,
"height":276,
"emoji":"\ud83d\ude36",
"set_name":"stickerset_name",
"thumb":{"file_id":"AAQEABMiaWcZAATNUEPkYkd0Fh2JBAABAg","file_size":2642,"file_path":"thumbnails\/file_8.jpg","width":128,"height":69},
"file_id":"CAADBAADzwIAAph_7gOClxA3gK5wqAI",
"file_size":12258
},
{
"width":512,
"height":327,
"emoji":"\ud83d\udcbb",
"set_name":"stickerset_name",
"thumb":{"file_id":"AAQEABPC3d8ZAAQUJJnFB1VfII2RAAIC","file_size":3824,"file_path":"thumbnails\/file_10.jpg","width":128,"height":82},
"file_id":"CAADBAAD0QIAAph_7gO-vBJGkTeWqwI",
"file_size":18282
}
]
}
}';
}
public function testGetStickerSet()
{
$result = $this->getStickerSet();
$server = new ServerResponse(json_decode($result, true), 'testbot');
$server_result = $server->getResult();
self::assertInstanceOf('\Longman\TelegramBot\Entities\StickerSet', $server_result);
self::assertEquals('stickerset_name', $server_result->getName());
self::assertEquals('Some name', $server_result->getTitle());
self::assertFalse($server_result->getContainsMasks());
$stickers = $server_result->getStickers();
self::assertCount(4, $stickers);
self::assertInstanceOf('\Longman\TelegramBot\Entities\Sticker', $stickers[0]);
}
}
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