Commit 1549a4fd authored by Jack'lul's avatar Jack'lul

Merge pull request #198 from jacklul/apiupdate

API 2.1
parents 52e1ef1e dbeb0d84
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
/**
* Edited message command
*/
class EditedmessageCommand extends SystemCommand
{
/**#@+
* {@inheritdoc}
*/
protected $name = 'editedmessage';
protected $description = 'User edited message';
protected $version = '1.0.0';
/**#@-*/
/**
* {@inheritdoc}
*/
/*public function execute()
{
$update = $this->getUpdate();
$edited_message = $update->getEditedMessage();
}*/
}
......@@ -134,6 +134,9 @@ class DB
if (!defined('TB_MESSAGE')) {
define('TB_MESSAGE', self::$table_prefix.'message');
}
if (!defined('TB_EDITED_MESSAGE')) {
define('TB_EDITED_MESSAGE', self::$table_prefix.'edited_message');
}
if (!defined('TB_INLINE_QUERY')) {
define('TB_INLINE_QUERY', self::$table_prefix.'inline_query');
}
......@@ -260,13 +263,14 @@ class DB
* @param int $inline_query_id
* @param int $chosen_inline_result_id
* @param int $callback_query_id
* @param int $edited_message_id
*
* @return bool|null
*/
public static function insertTelegramUpdate($id, $chat_id, $message_id, $inline_query_id, $chosen_inline_result_id, $callback_query_id)
public static function insertTelegramUpdate($id, $chat_id, $message_id, $inline_query_id, $chosen_inline_result_id, $callback_query_id, $edited_message_id)
{
if (is_null($message_id) && is_null($inline_query_id) && is_null($chosen_inline_result_id) && is_null($callback_query_id)) {
throw new TelegramException('Error both query_id and message_id are null');
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)) {
throw new TelegramException('message_id, inline_query_id, chosen_inline_result_id, callback_query_id, edited_message_id are all null');
}
if (!self::isDbConnected()) {
......@@ -277,10 +281,10 @@ class DB
//telegram_update table
$sth_insert_telegram_update = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_TELEGRAM_UPDATE . '`
(
`id`, `chat_id`, `message_id`, `inline_query_id`, `chosen_inline_result_id`, `callback_query_id`
`id`, `chat_id`, `message_id`, `inline_query_id`, `chosen_inline_result_id`, `callback_query_id`, `edited_message_id`
)
VALUES (
:id, :chat_id, :message_id, :inline_query_id, :chosen_inline_result_id, :callback_query_id
:id, :chat_id, :message_id, :inline_query_id, :chosen_inline_result_id, :callback_query_id, :edited_message_id
)
');
......@@ -290,6 +294,7 @@ class DB
$sth_insert_telegram_update->bindParam(':inline_query_id', $inline_query_id, \PDO::PARAM_INT);
$sth_insert_telegram_update->bindParam(':chosen_inline_result_id', $chosen_inline_result_id, \PDO::PARAM_INT);
$sth_insert_telegram_update->bindParam(':callback_query_id', $callback_query_id, \PDO::PARAM_INT);
$sth_insert_telegram_update->bindParam(':edited_message_id', $edited_message_id, \PDO::PARAM_INT);
$status = $sth_insert_telegram_update->execute();
} catch (PDOException $e) {
......@@ -419,6 +424,8 @@ class DB
/**
* Insert request into database
*
* @todo self::$pdo->lastInsertId() - unsafe usage if expected previous insert fails?
*
* @param Entities\Update &$update
*
* @return bool
......@@ -431,22 +438,28 @@ class DB
$message_id = $message->getMessageId();
$chat_id = $message->getChat()->getId();
self::insertMessageRequest($message);
return self::insertTelegramUpdate($update_id, $chat_id, $message_id, null, null, null);
return self::insertTelegramUpdate($update_id, $chat_id, $message_id, null, null, null, null);
} elseif ($update->getUpdateType() == 'edited_message') {
$edited_message = $update->getEditedMessage();
$chat_id = $edited_message->getChat()->getId();
self::insertEditedMessageRequest($edited_message);
$edited_message_local_id = self::$pdo->lastInsertId();
return self::insertTelegramUpdate($update_id, $chat_id, null, null, null, null, $edited_message_local_id);
} elseif ($update->getUpdateType() == 'inline_query') {
$inline_query = $update->getInlineQuery();
$inline_query_id = $inline_query->getId();
self::insertInlineQueryRequest($inline_query);
return self::insertTelegramUpdate($update_id, null, null, $inline_query_id, null, null);
return self::insertTelegramUpdate($update_id, null, null, $inline_query_id, null, null, null);
} elseif ($update->getUpdateType() == 'chosen_inline_result') {
$chosen_inline_result = $update->getChosenInlineResult();
self::insertChosenInlineResultRequest($chosen_inline_result);
$chosen_inline_result_local_id = self::$pdo->lastInsertId();
return self::insertTelegramUpdate($update_id, null, null, null, $chosen_inline_result_local_id, null);
return self::insertTelegramUpdate($update_id, null, null, null, $chosen_inline_result_local_id, null, null);
} elseif ($update->getUpdateType() == 'callback_query') {
$callback_query = $update->getCallbackQuery();
$callback_query_id = $callback_query->getId();
self::insertCallbackQueryRequest($callback_query);
return self::insertTelegramUpdate($update_id, null, null, null, null, $callback_query_id);
return self::insertTelegramUpdate($update_id, null, null, null, null, $callback_query_id, null);
}
}
......@@ -552,7 +565,6 @@ class DB
}
}
/**
* Insert callback query request into database
*
......@@ -800,6 +812,72 @@ class DB
return true;
}
/**
* Insert Edited Message request in db
*
* @param Entities\Message &$edited_message
*
* @return bool If the insert was successful
*/
public static function insertEditedMessageRequest(Message &$edited_message)
{
if (!self::isDbConnected()) {
return false;
}
$from = $edited_message->getFrom();
$chat = $edited_message->getChat();
$chat_id = $chat->getId();
$edit_date = self::getTimestamp($edited_message->getEditDate());
$entities = $edited_message->getEntities();
try {
//edited_message Table
$sth = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_EDITED_MESSAGE . '`
(
`chat_id`, `message_id`, `user_id`, `edit_date`, `text`, `entities`, `caption`
)
VALUES (
:chat_id, :message_id, :user_id, :date, :text, :entities, :caption
)');
$message_id = $edited_message->getMessageId();
$from_id = $from->getId();
$text = $edited_message->getText();
$caption = $edited_message->getCaption();
$sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT);
$sth->bindParam(':message_id', $message_id, \PDO::PARAM_INT);
$sth->bindParam(':user_id', $from_id, \PDO::PARAM_INT);
$sth->bindParam(':date', $edit_date, \PDO::PARAM_STR);
$var = [];
if (is_array($entities)) {
foreach ($entities as $elm) {
$var[] = json_decode($elm, true);
}
$entities = json_encode($var);
} else {
$entities = null;
}
$sth->bindParam(':text', $text, \PDO::PARAM_STR);
$sth->bindParam(':entities', $entities, \PDO::PARAM_STR);
$sth->bindParam(':caption', $caption, \PDO::PARAM_STR);
$status = $sth->execute();
} catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}
return true;
}
/**
* Select Group and single Chats
*
......
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Entities;
class ChatMember extends Entity
{
protected $user;
protected $status;
public function __construct(array $data)
{
$this->user = isset($data['user']) ? $data['user'] : null;
if (empty($this->user)) {
throw new TelegramException('user is empty!');
}
$this->user = new User($data['user']);
$this->status = isset($data['status']) ? $data['status'] : null;
if ($this->status === '') {
throw new TelegramException('status is empty!');
}
}
public function getUser()
{
return $this->user;
}
public function getStatus()
{
return $this->status;
}
}
......@@ -28,6 +28,8 @@ class Message extends Entity
protected $forward_date;
protected $edit_date;
protected $reply_to_message;
protected $text;
......@@ -131,6 +133,8 @@ class Message extends Entity
$this->forward_date = isset($data['forward_date']) ? $data['forward_date'] : null;
$this->edit_date = isset($data['edit_date']) ? $data['edit_date'] : null;
$this->text = isset($data['text']) ? $data['text'] : null;
$command = $this->getCommand();
if (!empty($command)) {
......@@ -360,6 +364,11 @@ class Message extends Entity
return $this->forward_date;
}
public function getEditDate()
{
return $this->edit_date;
}
public function getReplyToMessage()
{
return $this->reply_to_message;
......
......@@ -16,6 +16,7 @@ class MessageEntity extends Entity
protected $offset;
protected $length;
protected $url;
protected $user;
/**
* @todo check for type value from this list: https://core.telegram.org/bots/api#messageentity
......@@ -38,6 +39,7 @@ class MessageEntity extends Entity
}
$this->url = isset($data['url']) ? $data['url'] : null;
$this->user = isset($data['user']) ? new User($data['user']) : null;
}
public function getType()
......@@ -59,4 +61,9 @@ class MessageEntity extends Entity
{
return $this->url;
}
public function getUser()
{
return $this->user;
}
}
......@@ -14,32 +14,42 @@ use Longman\TelegramBot\Exception\TelegramException;
class ServerResponse extends Entity
{
protected $ok;
protected $result;
protected $error_code;
protected $description;
public function __construct(array $data, $bot_name)
{
if (isset($data['ok']) & isset($data['result'])) {
if (is_array($data['result'])) {
if ($data['ok'] & !$this->isAssoc($data['result'])) {
//get update
if ($data['ok'] & !$this->isAssoc($data['result']) & !isset($data['result'][0]['user'])) {
//Get Update
foreach ($data['result'] as $update) {
$this->result[] = new Update($update, $bot_name);
}
} elseif ($data['ok'] & !$this->isAssoc($data['result']) & isset($data['result'][0]['user'])) {
//Response from getChatAdministrators
$this->result = [];
foreach ($data['result'] as $user) {
array_push($this->result, new ChatMember($user));
}
} elseif ($data['ok'] & $this->isAssoc($data['result'])) {
if (isset($data['result']['total_count'])) {
//getUserProfilePhotos
//Response from getUserProfilePhotos
$this->result = new UserProfilePhotos($data['result']);
} elseif (isset($data['result']['file_id'])) {
//Response getFile
//Response from getFile
$this->result = new File($data['result']);
} elseif (isset($data['result']['username'])) {
//Response getMe
//Response from getMe
$this->result = new User($data['result']);
} elseif (isset($data['result']['id'])) {
//Response from getChat
$this->result = new Chat($data['result']);
} elseif (isset($data['result']['user'])) {
//Response from getChatMember
$this->result = new ChatMember($data['result']);
} else {
//Response from sendMessage
$this->result = new Message($data['result'], $bot_name);
......@@ -50,17 +60,20 @@ class ServerResponse extends Entity
$this->error_code = null;
$this->description = null;
} else {
if ($data['ok'] & $data['result'] == true) {
if ($data['ok'] & $data['result'] === true) {
//Response from setWebhook set
$this->ok = $data['ok'];
$this->result = true;
$this->error_code = null;
if (isset($data['description'])) {
$this->description = $data['description'];
} else {
$this->description = '';
}
} elseif (is_numeric($data['result'])) {
//Response from getChatMembersCount
$this->result = $data['result'];
} else {
$this->ok = false;
$this->result = null;
......
......@@ -17,6 +17,7 @@ class Update extends Entity
protected $update_id;
protected $message;
protected $edited_message;
protected $inline_query;
protected $chosen_inline_result;
protected $callback_query;
......@@ -37,6 +38,12 @@ class Update extends Entity
$this->update_type = 'message';
}
$this->edited_message = isset($data['edited_message']) ? $data['edited_message'] : null;
if (!empty($this->edited_message)) {
$this->edited_message = new Message($this->edited_message, $bot_name);
$this->update_type = 'edited_message';
}
if (empty($update_id)) {
throw new TelegramException('update_id is empty!');
}
......@@ -69,6 +76,10 @@ class Update extends Entity
{
return $this->message;
}
public function getEditedMessage()
{
return $this->edited_message;
}
public function getInlineQuery()
{
return $this->inline_query;
......
......@@ -56,7 +56,12 @@ class Request
'getUserProfilePhotos',
'getFile',
'kickChatMember',
'leaveChat',
'unbanChatMember',
'getChat',
'getChatAdministrators',
'getChatMember',
'getChatMembersCount',
'answerCallbackQuery',
'answerInlineQuery',
'editMessageText',
......@@ -658,6 +663,22 @@ class Request
return self::send('kickChatMember', $data);
}
/**
* Leave Chat
*
* @param array $data
*
* @return mixed
*/
public static function leaveChat(array $data)
{
if (empty($data)) {
throw new TelegramException('Data is empty!');
}
return self::send('leaveChat', $data);
}
/**
* Unban Chat Member
*
......@@ -674,6 +695,78 @@ class Request
return self::send('unbanChatMember', $data);
}
/**
* Get Chat
*
* @todo add get response in ServerResponse.php?
*
* @param array $data
*
* @return mixed
*/
public static function getChat(array $data)
{
if (empty($data)) {
throw new TelegramException('Data is empty!');
}
return self::send('getChat', $data);
}
/**
* Get Chat Administrators
*
* @todo add get response in ServerResponse.php?
*
* @param array $data
*
* @return mixed
*/
public static function getChatAdministrators(array $data)
{
if (empty($data)) {
throw new TelegramException('Data is empty!');
}
return self::send('getChatAdministrators', $data);
}
/**
* Get Chat Members Count
*
* @todo add get response in ServerResponse.php?
*
* @param array $data
*
* @return mixed
*/
public static function getChatMembersCount(array $data)
{
if (empty($data)) {
throw new TelegramException('Data is empty!');
}
return self::send('getChatMembersCount', $data);
}
/**
* Get Chat Member
*
* @todo add get response in ServerResponse.php?
*
* @param array $data
*
* @return mixed
*/
public static function getChatMember(array $data)
{
if (empty($data)) {
throw new TelegramException('Data is empty!');
}
return self::send('getChatMember', $data);
}
/**
* Answer callback query
*
......
......@@ -455,7 +455,7 @@ class Telegram
$command = 'genericmessage';
$update_type = $this->update->getUpdateType();
if (in_array($update_type, ['inline_query', 'chosen_inline_result', 'callback_query'])) {
if (in_array($update_type, ['inline_query', 'chosen_inline_result', 'callback_query', 'edited_message'])) {
$command = $this->getCommandFromType($update_type);
} elseif ($update_type === 'message') {
$message = $this->update->getMessage();
......
CREATE TABLE IF NOT EXISTS `user` (
CREATE TABLE IF NOT EXISTS `user` (
`id` bigint COMMENT 'Unique user identifier',
`first_name` CHAR(255) NOT NULL DEFAULT '' COMMENT 'User first name',
`last_name` CHAR(255) DEFAULT NULL COMMENT 'User last name',
......@@ -9,7 +9,7 @@ CREATE TABLE IF NOT EXISTS `user` (
KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE IF NOT EXISTS `chat` (
CREATE TABLE IF NOT EXISTS `chat` (
`id` bigint COMMENT 'Unique user or chat identifier',
`type` ENUM('private', 'group', 'supergroup', 'channel') NOT NULL COMMENT 'chat type private, group, supergroup or channel',
`title` CHAR(255) DEFAULT '' COMMENT 'chat title null if case of single chat with the bot',
......@@ -73,7 +73,7 @@ CREATE TABLE IF NOT EXISTS `callback_query` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE IF NOT EXISTS `message` (
CREATE TABLE IF NOT EXISTS `message` (
`chat_id` bigint COMMENT 'Chat identifier.',
`id` bigint UNSIGNED COMMENT 'Unique message identifier',
`user_id` bigint NULL COMMENT 'User identifier',
......@@ -121,13 +121,32 @@ CREATE TABLE IF NOT EXISTS `message` (
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 (`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`),
FOREIGN KEY (`left_chat_member`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE IF NOT EXISTS `edited_message` (
`id` bigint UNSIGNED AUTO_INCREMENT COMMENT 'Unique identifier for this entry.',
`chat_id` bigint COMMENT 'Chat identifier.',
`message_id` bigint UNSIGNED COMMENT 'Unique message identifier',
`user_id` bigint NULL COMMENT 'User identifier',
`edit_date` timestamp NULL DEFAULT NULL COMMENT 'Date the message was sent in timestamp format',
`text` TEXT DEFAULT NULL COMMENT 'For text messages, the actual UTF-8 text of the message max message length 4096 char utf8',
`entities` TEXT DEFAULT NULL COMMENT 'For text messages, special entities like usernames, URLs, bot commands, etc. that appear in the text',
`caption` TEXT DEFAULT NULL COMMENT 'For message with caption, the actual UTF-8 text of the caption',
PRIMARY KEY (`id`),
KEY `chat_id` (`chat_id`),
KEY `message_id` (`message_id`),
KEY `user_id` (`user_id`),
FOREIGN KEY (`chat_id`) REFERENCES `chat` (`id`),
FOREIGN KEY (`chat_id`, `message_id`) REFERENCES `message` (`chat_id`, `id`),
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE IF NOT EXISTS `telegram_update` (
`id` bigint UNSIGNED COMMENT 'The update\'s unique identifier.',
`chat_id` bigint NULL DEFAULT NULL COMMENT 'Chat identifier.',
......@@ -135,17 +154,20 @@ CREATE TABLE IF NOT EXISTS `telegram_update` (
`inline_query_id` bigint UNSIGNED DEFAULT NULL COMMENT 'The inline query unique identifier.',
`chosen_inline_result_id` bigint UNSIGNED DEFAULT NULL COMMENT 'The chosen query unique identifier.',
`callback_query_id` bigint UNSIGNED DEFAULT NULL COMMENT 'The callback query unique identifier.',
`edited_message_id` bigint UNSIGNED DEFAULT NULL COMMENT 'Unique edited message identifier',
PRIMARY KEY (`id`),
KEY `message_id` (`chat_id`, `message_id`),
KEY `inline_query_id` (`inline_query_id`),
KEY `chosen_inline_result_id` (`chosen_inline_result_id`),
KEY `callback_query_id` (`callback_query_id`),
KEY `edited_message_id` (`edited_message_id`),
FOREIGN KEY (`chat_id`, `message_id`) REFERENCES `message` (`chat_id`,`id`),
FOREIGN KEY (`chat_id`, `message_id`) REFERENCES `message` (`chat_id`, `id`),
FOREIGN KEY (`inline_query_id`) REFERENCES `inline_query` (`id`),
FOREIGN KEY (`chosen_inline_result_id`) REFERENCES `chosen_inline_result` (`id`),
FOREIGN KEY (`callback_query_id`) REFERENCES `callback_query` (`id`)
FOREIGN KEY (`callback_query_id`) REFERENCES `callback_query` (`id`),
FOREIGN KEY (`edited_message_id`) REFERENCES `edited_message` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE IF NOT EXISTS `conversation` (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment