Skip to content

Commit

Permalink
supporting forward from chat field
Browse files Browse the repository at this point in the history
  • Loading branch information
MBoretto committed May 13, 2016
1 parent 432aa92 commit e5b6d41
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 45 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ and have interactions in a matter of minutes.

The Bot can:
- retrieve updates with webhook and getUpdate methods.
- supports all types and methods according to Telegram API (9 April 2016).
- supports all types and methods according to Telegram API (6 May 2016).
- supports supergroups.
- handle commands in chat with other bots.
- manage Channel from the bot admin interface.
Expand Down
109 changes: 66 additions & 43 deletions src/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,57 @@ public static function insertUser(User $user, $date, Chat $chat = null)
}
}

/**
* Insert chat
*
* @todo Needs to return something if successful
*
* @param Entities\Chat $chat
* @param string $date
* @param int $migrate_to_chat_id
*/
public static function insertChat(Chat $chat, $date, $migrate_to_chat_id = null)
{
if (!self::isDbConnected()) {
return false;
}

$chat_id = $chat->getId();
$chat_title = $chat->getTitle();
$type = $chat->getType();

try {
//chat table
$sth2 = self::$pdo->prepare('INSERT INTO `' . TB_CHAT . '`
(
`id`, `type`, `title`, `created_at` ,`updated_at`, `old_id`
)
VALUES (
:id, :type, :title, :date, :date, :oldid
)
ON DUPLICATE KEY UPDATE `type`=:type, `title`=:title, `updated_at`=:date
');

if ($migrate_to_chat_id) {
$type = 'supergroup';

$sth2->bindParam(':id', $migrate_to_chat_id, \PDO::PARAM_INT);
$sth2->bindParam(':oldid', $chat_id, \PDO::PARAM_INT);
} else {
$sth2->bindParam(':id', $chat_id, \PDO::PARAM_INT);
$sth2->bindParam(':oldid', $migrate_to_chat_id, \PDO::PARAM_INT);
}

$sth2->bindParam(':type', $type, \PDO::PARAM_INT);
$sth2->bindParam(':title', $chat_title, \PDO::PARAM_STR, 255);
$sth2->bindParam(':date', $date, \PDO::PARAM_STR);

$status = $sth2->execute();
} catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}
}

/**
* Insert request into database
*
Expand Down Expand Up @@ -561,64 +612,35 @@ public static function insertMessageRequest(Message &$message)

$date = self::getTimestamp($message->getDate());
$forward_from = $message->getForwardFrom();
if ($forward_from) {
$forward_date = self::getTimestamp($message->getForwardDate());
}

$forward_from_chat = $message->getForwardFromChat();
$photo = $message->getPhoto();
$entities = $message->getEntities();
$new_chat_member = $message->getNewChatMember();

$new_chat_photo = $message->getNewChatPhoto();
$left_chat_member = $message->getLeftChatMember();

$migrate_from_chat_id = $message->getMigrateFromChatId();
$migrate_to_chat_id = $message->getMigrateToChatId();

try {
//chat table
$sth2 = self::$pdo->prepare('INSERT INTO `' . TB_CHAT . '`
(
`id`, `type`, `title`, `created_at` ,`updated_at`, `old_id`
)
VALUES (
:id, :type, :title, :date, :date, :oldid
)
ON DUPLICATE KEY UPDATE `type`=:type, `title`=:title, `updated_at`=:date
');

$chat_title = $chat->getTitle();
$type = $chat->getType();

if ($migrate_to_chat_id) {
$type = 'supergroup';

$sth2->bindParam(':id', $migrate_to_chat_id, \PDO::PARAM_INT);
$sth2->bindParam(':oldid', $chat_id, \PDO::PARAM_INT);
} else {
$sth2->bindParam(':id', $chat_id, \PDO::PARAM_INT);
$sth2->bindParam(':oldid', $migrate_to_chat_id, \PDO::PARAM_INT);
}

$sth2->bindParam(':type', $type, \PDO::PARAM_INT);
$sth2->bindParam(':title', $chat_title, \PDO::PARAM_STR, 255);
$sth2->bindParam(':date', $date, \PDO::PARAM_STR);

$status = $sth2->execute();
} catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}
self::insertChat($chat, $date, $migrate_to_chat_id);

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

//Forwarded object
if ($forward_from || $forward_from_chat) {
$forward_date = self::getTimestamp($message->getForwardDate());
}
//Insert the forwarded message user in users table
$forward_from = null;
if (is_object($forward_from)) {
self::insertUser($forward_from, $forward_date);
$forward_from = $forward_from->getId();
}

if (is_object($forward_from_chat)) {
self::insertChat($forward_from_chat, $forward_date);
$forward_from_chat = $forward_from_chat->getId();
}

//New and left chat member
if ($new_chat_member) {
//Insert the new chat user
self::insertUser($new_chat_member, $date, $chat);
Expand All @@ -633,7 +655,7 @@ public static function insertMessageRequest(Message &$message)
//message Table
$sth = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_MESSAGE . '`
(
`id`, `user_id`, `date`, `chat_id`, `forward_from`,
`id`, `user_id`, `date`, `chat_id`, `forward_from`, `forward_from_chat`,
`forward_date`, `reply_to_chat`, `reply_to_message`, `text`, `entities`, `audio`, `document`,
`photo`, `sticker`, `video`, `voice`, `caption`, `contact`,
`location`, `venue`, `new_chat_member`, `left_chat_member`,
Expand All @@ -642,7 +664,7 @@ public static function insertMessageRequest(Message &$message)
`migrate_from_chat_id`, `migrate_to_chat_id`, `pinned_message`
)
VALUES (
:message_id, :user_id, :date, :chat_id, :forward_from,
:message_id, :user_id, :date, :chat_id, :forward_from, :forward_from_chat,
:forward_date, :reply_to_chat, :reply_to_message, :text, :entities, :audio, :document,
:photo, :sticker, :video, :voice, :caption, :contact,
:location, :venue, :new_chat_member, :left_chat_member,
Expand Down Expand Up @@ -687,6 +709,7 @@ public static function insertMessageRequest(Message &$message)
$sth->bindParam(':user_id', $from_id, \PDO::PARAM_INT);
$sth->bindParam(':date', $date, \PDO::PARAM_STR);
$sth->bindParam(':forward_from', $forward_from, \PDO::PARAM_INT);
$sth->bindParam(':forward_from_chat', $forward_from_chat, \PDO::PARAM_INT);
$sth->bindParam(':forward_date', $forward_date, \PDO::PARAM_STR);
$reply_chat_id = null;
if ($reply_to_message_id) {
Expand Down
13 changes: 13 additions & 0 deletions src/Entities/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class Message extends Entity

protected $forward_from;

protected $forward_from_chat;

protected $forward_date;

protected $reply_to_message;
Expand Down Expand Up @@ -120,6 +122,12 @@ protected function init(array & $data, & $bot_name)
$this->forward_from = isset($data['forward_from']) ? $data['forward_from'] : null;
if (!empty($this->forward_from)) {
$this->forward_from = new User($this->forward_from);

}

$this->forward_from_chat = isset($data['forward_from_chat']) ? $data['forward_from_chat'] : null;
if (!empty($this->forward_from_chat)) {
$this->forward_from_chat = new Chat($this->forward_from_chat);
}

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

public function getForwardFromChat()
{
return $this->forward_from_chat;
}

public function getForwardDate()
{
return $this->forward_date;
Expand Down
2 changes: 1 addition & 1 deletion src/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Telegram
*
* @var string
*/
protected $version = '0.31.0';
protected $version = '0.32.0';

/**
* Telegram API key
Expand Down
3 changes: 3 additions & 0 deletions structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ CREATE TABLE IF NOT EXISTS `message` (
`user_id` bigint NULL COMMENT 'User identifier',
`date` timestamp NULL DEFAULT NULL COMMENT 'Date the message was sent in timestamp format',
`forward_from` bigint NULL DEFAULT NULL COMMENT 'User id. For forwarded messages, sender of the original message',
`forward_from_chat` bigint NULL DEFAULT NULL COMMENT 'Chat id. For forwarded messages from channel',
`forward_date` timestamp NULL DEFAULT NULL COMMENT 'For forwarded messages, date the original message was sent in Unix time',
`reply_to_chat` bigint NULL DEFAULT NULL COMMENT 'Chat identifier.',
`reply_to_message` bigint UNSIGNED DEFAULT NULL COMMENT 'Message is a reply to another message.',
Expand Down Expand Up @@ -111,6 +112,7 @@ CREATE TABLE IF NOT EXISTS `message` (
PRIMARY KEY (`chat_id`, `id`),
KEY `user_id` (`user_id`),
KEY `forward_from` (`forward_from`),
KEY `forward_from_chat` (`forward_from_chat`),
KEY `reply_to_chat` (`reply_to_chat`),
KEY `reply_to_message` (`reply_to_message`),
KEY `new_chat_member` (`new_chat_member`),
Expand All @@ -121,6 +123,7 @@ CREATE TABLE IF NOT EXISTS `message` (
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`),
FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`),
FOREIGN KEY (`forward_from`) REFERENCES `user` (`id`),
FOREIGN KEY (`forward_from_chat`) REFERENCES `chat` (`id`),
FOREIGN KEY (`reply_to_chat`, `reply_to_message`) REFERENCES `message` (`chat_id`,`id`),
FOREIGN KEY (`forward_from`) REFERENCES `user` (`id`),
FOREIGN KEY (`new_chat_member`) REFERENCES `user` (`id`),
Expand Down

0 comments on commit e5b6d41

Please sign in to comment.