Skip to content

Commit 1549a4f

Browse files
committed
Merge pull request php-telegram-bot#198 from jacklul/apiupdate
API 2.1
2 parents 52e1ef1 + dbeb0d8 commit 1549a4f

File tree

10 files changed

+336
-26
lines changed

10 files changed

+336
-26
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\Commands\SystemCommands;
12+
13+
use Longman\TelegramBot\Commands\SystemCommand;
14+
15+
/**
16+
* Edited message command
17+
*/
18+
class EditedmessageCommand extends SystemCommand
19+
{
20+
/**#@+
21+
* {@inheritdoc}
22+
*/
23+
protected $name = 'editedmessage';
24+
protected $description = 'User edited message';
25+
protected $version = '1.0.0';
26+
/**#@-*/
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
/*public function execute()
32+
{
33+
$update = $this->getUpdate();
34+
$edited_message = $update->getEditedMessage();
35+
}*/
36+
}

src/DB.php

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ protected static function defineTable()
134134
if (!defined('TB_MESSAGE')) {
135135
define('TB_MESSAGE', self::$table_prefix.'message');
136136
}
137+
if (!defined('TB_EDITED_MESSAGE')) {
138+
define('TB_EDITED_MESSAGE', self::$table_prefix.'edited_message');
139+
}
137140
if (!defined('TB_INLINE_QUERY')) {
138141
define('TB_INLINE_QUERY', self::$table_prefix.'inline_query');
139142
}
@@ -260,13 +263,14 @@ protected static function getTimestamp($time = null)
260263
* @param int $inline_query_id
261264
* @param int $chosen_inline_result_id
262265
* @param int $callback_query_id
266+
* @param int $edited_message_id
263267
*
264268
* @return bool|null
265269
*/
266-
public static function insertTelegramUpdate($id, $chat_id, $message_id, $inline_query_id, $chosen_inline_result_id, $callback_query_id)
270+
public static function insertTelegramUpdate($id, $chat_id, $message_id, $inline_query_id, $chosen_inline_result_id, $callback_query_id, $edited_message_id)
267271
{
268-
if (is_null($message_id) && is_null($inline_query_id) && is_null($chosen_inline_result_id) && is_null($callback_query_id)) {
269-
throw new TelegramException('Error both query_id and message_id are null');
272+
if (is_null($message_id) && is_null($inline_query_id) && is_null($chosen_inline_result_id) && is_null($callback_query_id) && is_null($edited_message_id)) {
273+
throw new TelegramException('message_id, inline_query_id, chosen_inline_result_id, callback_query_id, edited_message_id are all null');
270274
}
271275

272276
if (!self::isDbConnected()) {
@@ -277,10 +281,10 @@ public static function insertTelegramUpdate($id, $chat_id, $message_id, $inline_
277281
//telegram_update table
278282
$sth_insert_telegram_update = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_TELEGRAM_UPDATE . '`
279283
(
280-
`id`, `chat_id`, `message_id`, `inline_query_id`, `chosen_inline_result_id`, `callback_query_id`
284+
`id`, `chat_id`, `message_id`, `inline_query_id`, `chosen_inline_result_id`, `callback_query_id`, `edited_message_id`
281285
)
282286
VALUES (
283-
:id, :chat_id, :message_id, :inline_query_id, :chosen_inline_result_id, :callback_query_id
287+
:id, :chat_id, :message_id, :inline_query_id, :chosen_inline_result_id, :callback_query_id, :edited_message_id
284288
)
285289
');
286290

@@ -290,6 +294,7 @@ public static function insertTelegramUpdate($id, $chat_id, $message_id, $inline_
290294
$sth_insert_telegram_update->bindParam(':inline_query_id', $inline_query_id, \PDO::PARAM_INT);
291295
$sth_insert_telegram_update->bindParam(':chosen_inline_result_id', $chosen_inline_result_id, \PDO::PARAM_INT);
292296
$sth_insert_telegram_update->bindParam(':callback_query_id', $callback_query_id, \PDO::PARAM_INT);
297+
$sth_insert_telegram_update->bindParam(':edited_message_id', $edited_message_id, \PDO::PARAM_INT);
293298

294299
$status = $sth_insert_telegram_update->execute();
295300
} catch (PDOException $e) {
@@ -419,6 +424,8 @@ public static function insertChat(Chat $chat, $date, $migrate_to_chat_id = null)
419424
/**
420425
* Insert request into database
421426
*
427+
* @todo self::$pdo->lastInsertId() - unsafe usage if expected previous insert fails?
428+
*
422429
* @param Entities\Update &$update
423430
*
424431
* @return bool
@@ -431,22 +438,28 @@ public static function insertRequest(Update &$update)
431438
$message_id = $message->getMessageId();
432439
$chat_id = $message->getChat()->getId();
433440
self::insertMessageRequest($message);
434-
return self::insertTelegramUpdate($update_id, $chat_id, $message_id, null, null, null);
441+
return self::insertTelegramUpdate($update_id, $chat_id, $message_id, null, null, null, null);
442+
} elseif ($update->getUpdateType() == 'edited_message') {
443+
$edited_message = $update->getEditedMessage();
444+
$chat_id = $edited_message->getChat()->getId();
445+
self::insertEditedMessageRequest($edited_message);
446+
$edited_message_local_id = self::$pdo->lastInsertId();
447+
return self::insertTelegramUpdate($update_id, $chat_id, null, null, null, null, $edited_message_local_id);
435448
} elseif ($update->getUpdateType() == 'inline_query') {
436449
$inline_query = $update->getInlineQuery();
437450
$inline_query_id = $inline_query->getId();
438451
self::insertInlineQueryRequest($inline_query);
439-
return self::insertTelegramUpdate($update_id, null, null, $inline_query_id, null, null);
452+
return self::insertTelegramUpdate($update_id, null, null, $inline_query_id, null, null, null);
440453
} elseif ($update->getUpdateType() == 'chosen_inline_result') {
441454
$chosen_inline_result = $update->getChosenInlineResult();
442455
self::insertChosenInlineResultRequest($chosen_inline_result);
443456
$chosen_inline_result_local_id = self::$pdo->lastInsertId();
444-
return self::insertTelegramUpdate($update_id, null, null, null, $chosen_inline_result_local_id, null);
457+
return self::insertTelegramUpdate($update_id, null, null, null, $chosen_inline_result_local_id, null, null);
445458
} elseif ($update->getUpdateType() == 'callback_query') {
446459
$callback_query = $update->getCallbackQuery();
447460
$callback_query_id = $callback_query->getId();
448461
self::insertCallbackQueryRequest($callback_query);
449-
return self::insertTelegramUpdate($update_id, null, null, null, null, $callback_query_id);
462+
return self::insertTelegramUpdate($update_id, null, null, null, null, $callback_query_id, null);
450463
}
451464
}
452465

@@ -552,7 +565,6 @@ public static function insertChosenInlineResultRequest(ChosenInlineResult &$chos
552565
}
553566
}
554567

555-
556568
/**
557569
* Insert callback query request into database
558570
*
@@ -800,6 +812,72 @@ public static function insertMessageRequest(Message &$message)
800812
return true;
801813
}
802814

815+
/**
816+
* Insert Edited Message request in db
817+
*
818+
* @param Entities\Message &$edited_message
819+
*
820+
* @return bool If the insert was successful
821+
*/
822+
public static function insertEditedMessageRequest(Message &$edited_message)
823+
{
824+
if (!self::isDbConnected()) {
825+
return false;
826+
}
827+
828+
$from = $edited_message->getFrom();
829+
$chat = $edited_message->getChat();
830+
831+
$chat_id = $chat->getId();
832+
833+
$edit_date = self::getTimestamp($edited_message->getEditDate());
834+
835+
$entities = $edited_message->getEntities();
836+
837+
try {
838+
//edited_message Table
839+
$sth = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_EDITED_MESSAGE . '`
840+
(
841+
`chat_id`, `message_id`, `user_id`, `edit_date`, `text`, `entities`, `caption`
842+
)
843+
VALUES (
844+
:chat_id, :message_id, :user_id, :date, :text, :entities, :caption
845+
)');
846+
847+
$message_id = $edited_message->getMessageId();
848+
$from_id = $from->getId();
849+
850+
$text = $edited_message->getText();
851+
$caption = $edited_message->getCaption();
852+
853+
$sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT);
854+
$sth->bindParam(':message_id', $message_id, \PDO::PARAM_INT);
855+
$sth->bindParam(':user_id', $from_id, \PDO::PARAM_INT);
856+
$sth->bindParam(':date', $edit_date, \PDO::PARAM_STR);
857+
858+
$var = [];
859+
if (is_array($entities)) {
860+
foreach ($entities as $elm) {
861+
$var[] = json_decode($elm, true);
862+
}
863+
864+
$entities = json_encode($var);
865+
} else {
866+
$entities = null;
867+
}
868+
869+
$sth->bindParam(':text', $text, \PDO::PARAM_STR);
870+
$sth->bindParam(':entities', $entities, \PDO::PARAM_STR);
871+
$sth->bindParam(':caption', $caption, \PDO::PARAM_STR);
872+
873+
$status = $sth->execute();
874+
} catch (PDOException $e) {
875+
throw new TelegramException($e->getMessage());
876+
}
877+
878+
return true;
879+
}
880+
803881
/**
804882
* Select Group and single Chats
805883
*

src/Entities/ChatMember.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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\Entities;
12+
13+
class ChatMember extends Entity
14+
{
15+
protected $user;
16+
protected $status;
17+
18+
public function __construct(array $data)
19+
{
20+
$this->user = isset($data['user']) ? $data['user'] : null;
21+
if (empty($this->user)) {
22+
throw new TelegramException('user is empty!');
23+
}
24+
$this->user = new User($data['user']);
25+
26+
$this->status = isset($data['status']) ? $data['status'] : null;
27+
if ($this->status === '') {
28+
throw new TelegramException('status is empty!');
29+
}
30+
}
31+
32+
public function getUser()
33+
{
34+
return $this->user;
35+
}
36+
37+
public function getStatus()
38+
{
39+
return $this->status;
40+
}
41+
}

src/Entities/Message.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class Message extends Entity
2828

2929
protected $forward_date;
3030

31+
protected $edit_date;
32+
3133
protected $reply_to_message;
3234

3335
protected $text;
@@ -131,6 +133,8 @@ protected function init(array & $data, & $bot_name)
131133

132134
$this->forward_date = isset($data['forward_date']) ? $data['forward_date'] : null;
133135

136+
$this->edit_date = isset($data['edit_date']) ? $data['edit_date'] : null;
137+
134138
$this->text = isset($data['text']) ? $data['text'] : null;
135139
$command = $this->getCommand();
136140
if (!empty($command)) {
@@ -360,6 +364,11 @@ public function getForwardDate()
360364
return $this->forward_date;
361365
}
362366

367+
public function getEditDate()
368+
{
369+
return $this->edit_date;
370+
}
371+
363372
public function getReplyToMessage()
364373
{
365374
return $this->reply_to_message;

src/Entities/MessageEntity.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class MessageEntity extends Entity
1616
protected $offset;
1717
protected $length;
1818
protected $url;
19+
protected $user;
1920

2021
/**
2122
* @todo check for type value from this list: https://core.telegram.org/bots/api#messageentity
@@ -38,6 +39,7 @@ public function __construct(array $data)
3839
}
3940

4041
$this->url = isset($data['url']) ? $data['url'] : null;
42+
$this->user = isset($data['user']) ? new User($data['user']) : null;
4143
}
4244

4345
public function getType()
@@ -59,4 +61,9 @@ public function getUrl()
5961
{
6062
return $this->url;
6163
}
64+
65+
public function getUser()
66+
{
67+
return $this->user;
68+
}
6269
}

src/Entities/ServerResponse.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,42 @@
1414

1515
class ServerResponse extends Entity
1616
{
17-
1817
protected $ok;
1918
protected $result;
2019
protected $error_code;
2120
protected $description;
2221

23-
2422
public function __construct(array $data, $bot_name)
2523
{
2624
if (isset($data['ok']) & isset($data['result'])) {
2725
if (is_array($data['result'])) {
28-
if ($data['ok'] & !$this->isAssoc($data['result'])) {
29-
//get update
26+
if ($data['ok'] & !$this->isAssoc($data['result']) & !isset($data['result'][0]['user'])) {
27+
//Get Update
3028
foreach ($data['result'] as $update) {
3129
$this->result[] = new Update($update, $bot_name);
3230
}
31+
} elseif ($data['ok'] & !$this->isAssoc($data['result']) & isset($data['result'][0]['user'])) {
32+
//Response from getChatAdministrators
33+
$this->result = [];
34+
foreach ($data['result'] as $user) {
35+
array_push($this->result, new ChatMember($user));
36+
}
3337
} elseif ($data['ok'] & $this->isAssoc($data['result'])) {
3438
if (isset($data['result']['total_count'])) {
35-
//getUserProfilePhotos
39+
//Response from getUserProfilePhotos
3640
$this->result = new UserProfilePhotos($data['result']);
3741
} elseif (isset($data['result']['file_id'])) {
38-
//Response getFile
42+
//Response from getFile
3943
$this->result = new File($data['result']);
4044
} elseif (isset($data['result']['username'])) {
41-
//Response getMe
45+
//Response from getMe
4246
$this->result = new User($data['result']);
47+
} elseif (isset($data['result']['id'])) {
48+
//Response from getChat
49+
$this->result = new Chat($data['result']);
50+
} elseif (isset($data['result']['user'])) {
51+
//Response from getChatMember
52+
$this->result = new ChatMember($data['result']);
4353
} else {
4454
//Response from sendMessage
4555
$this->result = new Message($data['result'], $bot_name);
@@ -50,17 +60,20 @@ public function __construct(array $data, $bot_name)
5060
$this->error_code = null;
5161
$this->description = null;
5262
} else {
53-
if ($data['ok'] & $data['result'] == true) {
63+
if ($data['ok'] & $data['result'] === true) {
5464
//Response from setWebhook set
5565
$this->ok = $data['ok'];
5666
$this->result = true;
5767
$this->error_code = null;
58-
68+
5969
if (isset($data['description'])) {
6070
$this->description = $data['description'];
6171
} else {
6272
$this->description = '';
6373
}
74+
} elseif (is_numeric($data['result'])) {
75+
//Response from getChatMembersCount
76+
$this->result = $data['result'];
6477
} else {
6578
$this->ok = false;
6679
$this->result = null;

0 commit comments

Comments
 (0)