Skip to content

Commit 0fcd30a

Browse files
committed
Merge branch 'develop' into quickupdate
# Conflicts: # src/DB.php # structure.sql
2 parents f1120ea + 1549a4f commit 0fcd30a

File tree

11 files changed

+258
-30
lines changed

11 files changed

+258
-30
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/Commands/SystemCommands/InlinequeryCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public function execute()
4040
$data = ['inline_query_id' => $inline_query->getId()];
4141

4242
$articles = [
43-
['id' => '001', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => $query ]) ],
44-
['id' => '002', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => $query ]) ],
45-
['id' => '003', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => $query ]) ],
43+
['id' => '001', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => ' ' . $query ])],
44+
['id' => '002', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => ' ' . $query ])],
45+
['id' => '003', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => ' ' . $query ])],
4646
];
4747

4848
$array_article = [];

src/DB.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,10 @@ public static function insertUser(User $user, $date, Chat $chat = null)
348348
throw new TelegramException($e->getMessage());
349349
}
350350

351-
//insert also the relationship to the chat
351+
//insert also the relationship to the chat into user_chat table
352352
if (!is_null($chat)) {
353353
$chat_id = $chat->getId();
354354
try {
355-
//user_chat table
356355
$sth3 = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_USER_CHAT . '`
357356
(
358357
`user_id`, `chat_id`
@@ -427,6 +426,8 @@ public static function insertChat(Chat $chat, $date, $migrate_to_chat_id = null)
427426
/**
428427
* Insert request into database
429428
*
429+
* @todo self::$pdo->lastInsertId() - unsafe usage if expected previous insert fails?
430+
*
430431
* @param Entities\Update &$update
431432
*
432433
* @return bool
@@ -442,14 +443,6 @@ public static function insertRequest(Update &$update)
442443
$chat_id = $message->getChat()->getId();
443444
return self::insertTelegramUpdate($update_id, $chat_id, $message_id, null, null, null, null);
444445
}
445-
} elseif ($update->getUpdateType() == 'edited_message') {
446-
$edited_message = $update->getEditedMessage();
447-
448-
if (self::insertEditedMessageRequest($edited_message)) {
449-
$chat_id = $edited_message->getChat()->getId();
450-
$edited_message_local_id = self::$pdo->lastInsertId();
451-
return self::insertTelegramUpdate($update_id, $chat_id, null, null, null, null, $edited_message_local_id);
452-
}
453446
} elseif ($update->getUpdateType() == 'inline_query') {
454447
$inline_query = $update->getInlineQuery();
455448

@@ -471,6 +464,14 @@ public static function insertRequest(Update &$update)
471464
$callback_query_id = $callback_query->getId();
472465
return self::insertTelegramUpdate($update_id, null, null, null, null, $callback_query_id, null);
473466
}
467+
} elseif ($update->getUpdateType() == 'edited_message') {
468+
$edited_message = $update->getEditedMessage();
469+
470+
if (self::insertEditedMessageRequest($edited_message)) {
471+
$chat_id = $edited_message->getChat()->getId();
472+
$edited_message_local_id = self::$pdo->lastInsertId();
473+
return self::insertTelegramUpdate($update_id, $chat_id, null, null, null, null, $edited_message_local_id);
474+
}
474475
}
475476

476477
return false;
@@ -693,7 +694,6 @@ public static function insertMessageRequest(Message &$message)
693694
}
694695

695696
try {
696-
//message Table
697697
$sth = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_MESSAGE . '`
698698
(
699699
`id`, `user_id`, `date`, `chat_id`, `forward_from`, `forward_from_chat`,
@@ -850,7 +850,6 @@ public static function insertEditedMessageRequest(Message &$edited_message)
850850
$entities = $edited_message->getEntities();
851851

852852
try {
853-
//edited_message Table
854853
$sth = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_EDITED_MESSAGE . '`
855854
(
856855
`chat_id`, `message_id`, `user_id`, `edit_date`, `text`, `entities`, `caption`

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;

src/Entities/Update.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Update extends Entity
1717

1818
protected $update_id;
1919
protected $message;
20+
protected $edited_message;
2021
protected $inline_query;
2122
protected $chosen_inline_result;
2223
protected $callback_query;
@@ -37,6 +38,12 @@ public function __construct(array $data, $bot_name)
3738
$this->update_type = 'message';
3839
}
3940

41+
$this->edited_message = isset($data['edited_message']) ? $data['edited_message'] : null;
42+
if (!empty($this->edited_message)) {
43+
$this->edited_message = new Message($this->edited_message, $bot_name);
44+
$this->update_type = 'edited_message';
45+
}
46+
4047
if (empty($update_id)) {
4148
throw new TelegramException('update_id is empty!');
4249
}
@@ -69,6 +76,10 @@ public function getMessage()
6976
{
7077
return $this->message;
7178
}
79+
public function getEditedMessage()
80+
{
81+
return $this->edited_message;
82+
}
7283
public function getInlineQuery()
7384
{
7485
return $this->inline_query;

0 commit comments

Comments
 (0)