Skip to content

Commit e5b6d41

Browse files
committed
supporting forward from chat field
1 parent 432aa92 commit e5b6d41

File tree

5 files changed

+84
-45
lines changed

5 files changed

+84
-45
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ and have interactions in a matter of minutes.
6666

6767
The Bot can:
6868
- retrieve updates with webhook and getUpdate methods.
69-
- supports all types and methods according to Telegram API (9 April 2016).
69+
- supports all types and methods according to Telegram API (6 May 2016).
7070
- supports supergroups.
7171
- handle commands in chat with other bots.
7272
- manage Channel from the bot admin interface.

src/DB.php

Lines changed: 66 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,57 @@ public static function insertUser(User $user, $date, Chat $chat = null)
363363
}
364364
}
365365

366+
/**
367+
* Insert chat
368+
*
369+
* @todo Needs to return something if successful
370+
*
371+
* @param Entities\Chat $chat
372+
* @param string $date
373+
* @param int $migrate_to_chat_id
374+
*/
375+
public static function insertChat(Chat $chat, $date, $migrate_to_chat_id = null)
376+
{
377+
if (!self::isDbConnected()) {
378+
return false;
379+
}
380+
381+
$chat_id = $chat->getId();
382+
$chat_title = $chat->getTitle();
383+
$type = $chat->getType();
384+
385+
try {
386+
//chat table
387+
$sth2 = self::$pdo->prepare('INSERT INTO `' . TB_CHAT . '`
388+
(
389+
`id`, `type`, `title`, `created_at` ,`updated_at`, `old_id`
390+
)
391+
VALUES (
392+
:id, :type, :title, :date, :date, :oldid
393+
)
394+
ON DUPLICATE KEY UPDATE `type`=:type, `title`=:title, `updated_at`=:date
395+
');
396+
397+
if ($migrate_to_chat_id) {
398+
$type = 'supergroup';
399+
400+
$sth2->bindParam(':id', $migrate_to_chat_id, \PDO::PARAM_INT);
401+
$sth2->bindParam(':oldid', $chat_id, \PDO::PARAM_INT);
402+
} else {
403+
$sth2->bindParam(':id', $chat_id, \PDO::PARAM_INT);
404+
$sth2->bindParam(':oldid', $migrate_to_chat_id, \PDO::PARAM_INT);
405+
}
406+
407+
$sth2->bindParam(':type', $type, \PDO::PARAM_INT);
408+
$sth2->bindParam(':title', $chat_title, \PDO::PARAM_STR, 255);
409+
$sth2->bindParam(':date', $date, \PDO::PARAM_STR);
410+
411+
$status = $sth2->execute();
412+
} catch (PDOException $e) {
413+
throw new TelegramException($e->getMessage());
414+
}
415+
}
416+
366417
/**
367418
* Insert request into database
368419
*
@@ -561,64 +612,35 @@ public static function insertMessageRequest(Message &$message)
561612

562613
$date = self::getTimestamp($message->getDate());
563614
$forward_from = $message->getForwardFrom();
564-
if ($forward_from) {
565-
$forward_date = self::getTimestamp($message->getForwardDate());
566-
}
567-
615+
$forward_from_chat = $message->getForwardFromChat();
568616
$photo = $message->getPhoto();
569617
$entities = $message->getEntities();
570618
$new_chat_member = $message->getNewChatMember();
571-
572619
$new_chat_photo = $message->getNewChatPhoto();
573620
$left_chat_member = $message->getLeftChatMember();
574-
575621
$migrate_from_chat_id = $message->getMigrateFromChatId();
576622
$migrate_to_chat_id = $message->getMigrateToChatId();
577623

578-
try {
579-
//chat table
580-
$sth2 = self::$pdo->prepare('INSERT INTO `' . TB_CHAT . '`
581-
(
582-
`id`, `type`, `title`, `created_at` ,`updated_at`, `old_id`
583-
)
584-
VALUES (
585-
:id, :type, :title, :date, :date, :oldid
586-
)
587-
ON DUPLICATE KEY UPDATE `type`=:type, `title`=:title, `updated_at`=:date
588-
');
589-
590-
$chat_title = $chat->getTitle();
591-
$type = $chat->getType();
592-
593-
if ($migrate_to_chat_id) {
594-
$type = 'supergroup';
595-
596-
$sth2->bindParam(':id', $migrate_to_chat_id, \PDO::PARAM_INT);
597-
$sth2->bindParam(':oldid', $chat_id, \PDO::PARAM_INT);
598-
} else {
599-
$sth2->bindParam(':id', $chat_id, \PDO::PARAM_INT);
600-
$sth2->bindParam(':oldid', $migrate_to_chat_id, \PDO::PARAM_INT);
601-
}
602-
603-
$sth2->bindParam(':type', $type, \PDO::PARAM_INT);
604-
$sth2->bindParam(':title', $chat_title, \PDO::PARAM_STR, 255);
605-
$sth2->bindParam(':date', $date, \PDO::PARAM_STR);
606-
607-
$status = $sth2->execute();
608-
} catch (PDOException $e) {
609-
throw new TelegramException($e->getMessage());
610-
}
624+
self::insertChat($chat, $date, $migrate_to_chat_id);
611625

612626
//Insert user and the relation with the chat
613627
self::insertUser($from, $date, $chat);
614628

629+
//Forwarded object
630+
if ($forward_from || $forward_from_chat) {
631+
$forward_date = self::getTimestamp($message->getForwardDate());
632+
}
615633
//Insert the forwarded message user in users table
616-
$forward_from = null;
617634
if (is_object($forward_from)) {
618635
self::insertUser($forward_from, $forward_date);
619636
$forward_from = $forward_from->getId();
620637
}
621-
638+
if (is_object($forward_from_chat)) {
639+
self::insertChat($forward_from_chat, $forward_date);
640+
$forward_from_chat = $forward_from_chat->getId();
641+
}
642+
643+
//New and left chat member
622644
if ($new_chat_member) {
623645
//Insert the new chat user
624646
self::insertUser($new_chat_member, $date, $chat);
@@ -633,7 +655,7 @@ public static function insertMessageRequest(Message &$message)
633655
//message Table
634656
$sth = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_MESSAGE . '`
635657
(
636-
`id`, `user_id`, `date`, `chat_id`, `forward_from`,
658+
`id`, `user_id`, `date`, `chat_id`, `forward_from`, `forward_from_chat`,
637659
`forward_date`, `reply_to_chat`, `reply_to_message`, `text`, `entities`, `audio`, `document`,
638660
`photo`, `sticker`, `video`, `voice`, `caption`, `contact`,
639661
`location`, `venue`, `new_chat_member`, `left_chat_member`,
@@ -642,7 +664,7 @@ public static function insertMessageRequest(Message &$message)
642664
`migrate_from_chat_id`, `migrate_to_chat_id`, `pinned_message`
643665
)
644666
VALUES (
645-
:message_id, :user_id, :date, :chat_id, :forward_from,
667+
:message_id, :user_id, :date, :chat_id, :forward_from, :forward_from_chat,
646668
:forward_date, :reply_to_chat, :reply_to_message, :text, :entities, :audio, :document,
647669
:photo, :sticker, :video, :voice, :caption, :contact,
648670
:location, :venue, :new_chat_member, :left_chat_member,
@@ -687,6 +709,7 @@ public static function insertMessageRequest(Message &$message)
687709
$sth->bindParam(':user_id', $from_id, \PDO::PARAM_INT);
688710
$sth->bindParam(':date', $date, \PDO::PARAM_STR);
689711
$sth->bindParam(':forward_from', $forward_from, \PDO::PARAM_INT);
712+
$sth->bindParam(':forward_from_chat', $forward_from_chat, \PDO::PARAM_INT);
690713
$sth->bindParam(':forward_date', $forward_date, \PDO::PARAM_STR);
691714
$reply_chat_id = null;
692715
if ($reply_to_message_id) {

src/Entities/Message.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class Message extends Entity
2424

2525
protected $forward_from;
2626

27+
protected $forward_from_chat;
28+
2729
protected $forward_date;
2830

2931
protected $reply_to_message;
@@ -120,6 +122,12 @@ protected function init(array & $data, & $bot_name)
120122
$this->forward_from = isset($data['forward_from']) ? $data['forward_from'] : null;
121123
if (!empty($this->forward_from)) {
122124
$this->forward_from = new User($this->forward_from);
125+
126+
}
127+
128+
$this->forward_from_chat = isset($data['forward_from_chat']) ? $data['forward_from_chat'] : null;
129+
if (!empty($this->forward_from_chat)) {
130+
$this->forward_from_chat = new Chat($this->forward_from_chat);
123131
}
124132

125133
$this->forward_date = isset($data['forward_date']) ? $data['forward_date'] : null;
@@ -343,6 +351,11 @@ public function getForwardFrom()
343351
return $this->forward_from;
344352
}
345353

354+
public function getForwardFromChat()
355+
{
356+
return $this->forward_from_chat;
357+
}
358+
346359
public function getForwardDate()
347360
{
348361
return $this->forward_date;

src/Telegram.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Telegram
3030
*
3131
* @var string
3232
*/
33-
protected $version = '0.31.0';
33+
protected $version = '0.32.0';
3434

3535
/**
3636
* Telegram API key

structure.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ CREATE TABLE IF NOT EXISTS `message` (
8282
`user_id` bigint NULL COMMENT 'User identifier',
8383
`date` timestamp NULL DEFAULT NULL COMMENT 'Date the message was sent in timestamp format',
8484
`forward_from` bigint NULL DEFAULT NULL COMMENT 'User id. For forwarded messages, sender of the original message',
85+
`forward_from_chat` bigint NULL DEFAULT NULL COMMENT 'Chat id. For forwarded messages from channel',
8586
`forward_date` timestamp NULL DEFAULT NULL COMMENT 'For forwarded messages, date the original message was sent in Unix time',
8687
`reply_to_chat` bigint NULL DEFAULT NULL COMMENT 'Chat identifier.',
8788
`reply_to_message` bigint UNSIGNED DEFAULT NULL COMMENT 'Message is a reply to another message.',
@@ -111,6 +112,7 @@ CREATE TABLE IF NOT EXISTS `message` (
111112
PRIMARY KEY (`chat_id`, `id`),
112113
KEY `user_id` (`user_id`),
113114
KEY `forward_from` (`forward_from`),
115+
KEY `forward_from_chat` (`forward_from_chat`),
114116
KEY `reply_to_chat` (`reply_to_chat`),
115117
KEY `reply_to_message` (`reply_to_message`),
116118
KEY `new_chat_member` (`new_chat_member`),
@@ -121,6 +123,7 @@ CREATE TABLE IF NOT EXISTS `message` (
121123
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),
122124
FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`),
123125
FOREIGN KEY (`forward_from`) REFERENCES `user` (`id`),
126+
FOREIGN KEY (`forward_from_chat`) REFERENCES `chat` (`id`),
124127
FOREIGN KEY (`reply_to_chat`, `reply_to_message`) REFERENCES `message` (`chat_id`,`id`),
125128
FOREIGN KEY (`forward_from`) REFERENCES `user` (`id`),
126129
FOREIGN KEY (`new_chat_member`) REFERENCES `user` (`id`),

0 commit comments

Comments
 (0)