Skip to content

Commit 64860c5

Browse files
committed
fix incompatibility with setWebHook and ServerResponse
1 parent 5ab89fd commit 64860c5

File tree

3 files changed

+53
-21
lines changed

3 files changed

+53
-21
lines changed

src/Entities/ServerResponse.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
10+
*
11+
* ServerResponse.php
1012
*/
1113
namespace Longman\TelegramBot\Entities;
1214

@@ -23,20 +25,49 @@ class ServerResponse extends Entity
2325
public function __construct(array $data, $bot_name)
2426
{
2527

26-
if (isset($data['ok'])) {
27-
if ($data['ok']) {
28+
if (isset($data['ok']) & isset($data['result'])) {
29+
if ($data['ok'] & $data['result'] != 1) {
30+
//Response from sendMessage set
2831
$this->ok = $data['ok'];
2932
$this->result = new Message($data['result'], $bot_name);
3033
$this->error_code = null;
3134
$this->description = null;
35+
} elseif($data['ok'] & $data['result'] == 1) {
36+
//Response from setWebhook set
37+
$this->ok = $data['ok'];
38+
$this->result = $data['result'];
39+
$this->error_code = null;
40+
$this->description = $data['description'];
41+
3242
} else {
3343
$this->ok = false;
3444
$this->result = null;
3545
$this->error_code = $data['error_code'];
3646
$this->description = $data['description'];
3747
}
3848
} else {
39-
throw new TelegramException('ok is not set!');
49+
//webHook not set
50+
$this->ok = false;
51+
52+
if (isset($data['result'])) {
53+
$this->result = $data['result'];
54+
} else {
55+
$this->result = null;
56+
}
57+
58+
if (isset($data['error_code'])) {
59+
$this->error_code = $data['error_code'];
60+
} else {
61+
$this->error_code = null;
62+
}
63+
64+
if (isset($data['description'])) {
65+
$this->description = $data['description'];
66+
} else {
67+
$this->description = null;
68+
}
69+
70+
//throw new TelegramException('ok(variable) is not set!');
4071
}
4172
}
4273

src/Request.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
use Longman\TelegramBot\Exception\TelegramException;
1414
use Longman\TelegramBot\Entities\ServerResponse;
15-
use Longman\TelegramBot\Entities\User;
1615

1716
class Request
1817
{
@@ -74,16 +73,16 @@ public static function send($action, array $data = null)
7473
}
7574

7675
if (defined('PHPUNIT_TESTSUITE')) {
77-
$fake_response['ok'] = 1;
78-
79-
//some fake data just to let iniatilize the class
80-
$data['message_id'] = '123';
81-
$data['date'] = '123';
82-
83-
$data['from'] = array( 'id' => 123,'first_name' => 'botname', 'username'=> 'namebot');
84-
85-
$data['chat'] = array('id'=> $data['chat_id'] );
86-
$fake_response['result'] = $data;
76+
$fake_response['ok'] = 1; // :)
77+
78+
//some fake data just to let iniatilize the class method SendMessage
79+
if (isset( $data['chat_id'])) {
80+
$data['message_id'] = '123';
81+
$data['date'] = '123';
82+
$data['chat'] = array('id'=> $data['chat_id'] );
83+
$data['from'] = array( 'id' => 123,'first_name' => 'botname', 'username'=> 'namebot');
84+
$fake_response['result'] = $data;
85+
}
8786

8887
return new ServerResponse($fake_response, self::$telegram->getBotName());
8988
}
@@ -112,6 +111,7 @@ public static function send($action, array $data = null)
112111
$response['description'] = 'Empty server response';
113112
}
114113

114+
//return json_decode($result, true);
115115
return new ServerResponse(json_decode($result, true), self::$telegram->getBotName());
116116
}
117117

@@ -135,6 +135,7 @@ public static function getMe()
135135

136136
public static function setWebhook($url)
137137
{
138+
138139
$result = self::send('setWebhook', array('url' => $url));
139140
return $result;
140141
}

src/Telegram.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,12 @@ public function handle()
299299
if ($this->admin_enabled) {
300300
$message = $update->getMessage();
301301

302-
$from = $message->getFrom();
303-
$user_id = $from->getId();
302+
//$from = $message->getFrom();
303+
//$user_id = $from->getId();
304304

305305
//Admin command avaiable only in single chat with the bot
306-
//$chat = $message->getChat();
307-
//$user_id = $chat->getId();
306+
$chat = $message->getChat();
307+
$user_id = $chat->getId();
308308

309309
if (in_array($user_id, $this->admins_list)) {
310310
$this->addCommandsPath(BASE_PATH.'/Admin');
@@ -791,11 +791,11 @@ public function setWebHook($url)
791791
}
792792
$result = Request::setWebhook($url);
793793

794-
if (!$result['ok']) {
795-
throw new TelegramException('Webhook was not set! Error: '.$result[error_code].' '. $result['description']);
794+
if (!$result->isOk()) {
795+
throw new TelegramException('Webhook was not set! Error: '.$result->getErrorCode().' '. $result->getDescription());
796796
}
797797

798-
return $result['description'];
798+
return $result;
799799
}
800800

801801
/**

0 commit comments

Comments
 (0)