Commit 02c121f2 authored by MBoretto's avatar MBoretto

All types implemented and stored

parent ada86a45
......@@ -124,7 +124,7 @@ The bot can handle updates with **webhook** or **getUpdate** method:
## Webhook installation
You need server with https and composer support.
You must set a [WebHook](https://core.telegram.org/bots/api#setwebhook)
You must set a [WebHook](https://core.telegram.org/bots/api#setwebhook).
Create *set.php* (just edit *example-set.php*) and put into it:
```php
<?php
......@@ -145,14 +145,12 @@ try {
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
echo $e;
}
```
And open your *set.php* via browser.
After create *hook.php* (just edit *example-hook.php*) and put:
After, create *hook.php* (or just edit *example-hook.php*) and put:
```php
<?php
$loader = require __DIR__.'/vendor/autoload.php';
......@@ -171,11 +169,10 @@ try {
// echo $e;
}
```
## getUpdate installation NEW!
## getUpdate installation
You need the database Mysql active.
Create *getUpdateCLI.php* (just edit *example-getUpdateCLI.php*) and
put into it:
Create *getUpdateCLI.php* (just edit *example-getUpdateCLI.php*) and put into it:
```php
#!/usr/bin/env php
<?php
......@@ -223,6 +220,8 @@ You can set a custom prefix to all the tables while you are enabling Mysql:
```php
$telegram->enableMySQL($credentials, $BOT_NAME.'_');
```
### Types New!
All types implemented (except InputFile) according to Telegram API (2015 September 18).
### Commands
The bot is able to recognise commands in chat with multiple bot(
......@@ -241,16 +240,15 @@ Favourite colour? **/black, /red**
Favourite number? **/1, /134**
Maybe you would like to develop your own commands. A good practice is
to store them outside vendor/. This can be done adding the method:
to store them outside *vendor/*. This can be done adding the method:
```php
$COMMANDS_FOLDER = __DIR__.'/Commands/';
$telegram->addCommandsPath($COMMANDS_FOLDER);
```
### Admin Commands (new!)
Enabling this feature, the admin bot can perform some super user
command like send message to all.
### Admin Commands
Enabling this feature, the admin bot can perform some super user command like send message to all.
You can specify one or more admin with this option:
```php
......@@ -286,12 +284,13 @@ print_r($results);
```
### Logging
Thrown Exception are stored in TelegramException.log file.
You can also log incoming messages on a text file, set this option with the methods:
```php
$telegram->setLogRequests(true);
$telegram->setLogPath($BOT_NAME.'.log');
```
Thrown Exception are stored in TelegramException.log file.
-----
This code is available on
......
......@@ -12,6 +12,8 @@
namespace Longman\TelegramBot;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Entities\User;
use Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Exception\TelegramException;
/**
......@@ -150,17 +152,23 @@ class DB
}
/**
* Insert request in db
* Insert users eventually connection to chats
*
* @return bool
*/
public static function insertRequest(Update $update)
public static function insertUser(User $user, $date, Chat $chat = null)
{
if (!self::isDbConnected()) {
return false;
}
$message = $update->getMessage();
$user_id = $user->getId();
$username = $user->getUsername();
$first_name = $user->getFirstName();
$last_name = $user->getLastName();
$chat_id = $chat->getId();
try {
//Users table
......@@ -175,13 +183,6 @@ class DB
`last_name`=:last_name, `updated_at`=:date
');
$from = $message->getFrom();
$date = self::toTimestamp($message->getDate());
$user_id = $from->getId();
$username = $from->getUsername();
$first_name = $from->getFirstName();
$last_name = $from->getLastName();
$sth1->bindParam(':id', $user_id, \PDO::PARAM_INT);
$sth1->bindParam(':username', $username, \PDO::PARAM_STR, 255);
$sth1->bindParam(':first_name', $first_name, \PDO::PARAM_STR, 255);
......@@ -190,6 +191,104 @@ class DB
$status = $sth1->execute();
} catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}
//insert also the relationship to the chat
if (!is_null($chat)) {
try {
//user_chat table
$sth3 = self::$pdo->prepare('INSERT IGNORE INTO `'.TB_USERS_CHATS.'`
(
`user_id`, `chat_id`
)
VALUES (:user_id, :chat_id)
');
$sth3->bindParam(':user_id', $user_id, \PDO::PARAM_INT);
$sth3->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT);
$status = $sth3->execute();
} catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}
}
}
/**
* Insert request in db
*
* @return bool
*/
public static function insertRequest(Update $update)
{
if (!self::isDbConnected()) {
return false;
}
$update_id = $update->getUpdateId();
$message = $update->getMessage();
$from = $message->getFrom();
$chat = $message->getChat();
$user_id = $from->getId();
$chat_id = $chat->getId();
$chat_title = $chat->getTitle();
$date = self::toTimestamp($message->getDate());
$message_id = $message->getMessageId();
$forward_from = $message->getForwardFrom();
$forward_date = self::toTimestamp($message->getForwardDate());
$reply_to_message = $message->getReplyToMessage();
$text = $message->getText();
$audio = $message->getAudio();
$document = $message->getDocument();
$photo = $message->getPhoto();
$sticker = $message->getSticker();
$video = $message->getVideo();
$voice = $message->getVoice();
$caption = $message->getCaption();
$contact = $message->getContact();
$location = $message->getLocation();
$new_chat_participant = $message->getNewChatParticipant();
$left_chat_participant = $message->getLeftChatParticipant();
$new_chat_title = $message->getNewChatTitle();
$delete_chat_photo = $message->getDeleteChatPhoto();
$group_chat_created = $message->getGroupChatCreated();
//inser user and the relation with the chat
self::insertUser($from, $date, $chat);
//Insert the forwarded message user in users table
if (is_object($forward_from)) {
self::insertUser($forward_from, $forward_date);
$forward_from = $forward_from->getId();
} else {
$forward_from = '';
}
//Insert the new chat user
if (is_object($new_chat_participant)) {
self::insertUser($new_chat_participant, $date, $chat);
$new_chat_participant = $new_chat_participant->getId();
} else {
$new_chat_participant = '';
}
//Insert the left chat user
if (is_object($left_chat_participant)) {
self::insertUser($left_chat_participant, $date, $chat);
$left_chat_participant = $left_chat_participant->getId();
} else {
$left_chat_participant = '';
}
try {
//chats table
$sth2 = self::$pdo->prepare('INSERT INTO `'.TB_CHATS.'`
(
......@@ -199,8 +298,6 @@ class DB
ON DUPLICATE KEY UPDATE `title`=:title, `updated_at`=:date
');
$chat_id = $message->getChat()->getId();
$chat_title = $message->getChat()->getTitle();
$sth2->bindParam(':id', $chat_id, \PDO::PARAM_INT);
$sth2->bindParam(':title', $chat_title, \PDO::PARAM_STR, 255);
......@@ -208,19 +305,6 @@ class DB
$status = $sth2->execute();
//user_chat table
$sth3 = self::$pdo->prepare('INSERT IGNORE INTO `'.TB_USERS_CHATS.'`
(
`user_id`, `chat_id`
)
VALUES (:user_id, :chat_id)
');
$sth3->bindParam(':user_id', $user_id, \PDO::PARAM_INT);
$sth3->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT);
$status = $sth3->execute();
//Messages Table
$sth = self::$pdo->prepare('INSERT IGNORE INTO `'.TB_MESSAGES.'`
(
......@@ -237,39 +321,18 @@ class DB
:new_chat_title, :new_chat_photo, :delete_chat_photo, :group_chat_created
)');
$update_id = $update->getUpdateId();
$message_id = $message->getMessageId();
$forward_from = $message->getForwardFrom();
$forward_date = self::toTimestamp($message->getForwardDate());
$reply_to_message = $message->getReplyToMessage();
if (is_object($reply_to_message)) {
$reply_to_message = $reply_to_message->toJSON();
}
$text = $message->getText();
$audio = $message->getAudio();
$document = $message->getDocument();
$photo = $message->getPhoto();
$sticker = $message->getSticker();
$video = $message->getVideo();
$voice = $message->getVoice();
$caption = $message->getCaption();
$contact = $message->getContact();
$location = $message->getLocation();
$new_chat_participant = $message->getNewChatParticipant();
$left_chat_participant = $message->getLeftChatParticipant();
$new_chat_title = $message->getNewChatTitle();
$delete_chat_photo = $message->getDeleteChatPhoto();
$group_chat_created = $message->getGroupChatCreated();
$sth->bindParam(':update_id', $update_id, \PDO::PARAM_INT);
$sth->bindParam(':message_id', $message_id, \PDO::PARAM_INT);
$sth->bindParam(':user_id', $user_id, \PDO::PARAM_INT);
$sth->bindParam(':date', $date, \PDO::PARAM_STR);
$sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_STR);
//TODO insert the user in users tabl
$forward_from = is_object($forward_from) ? $forward_from->toJSON(): '';
$sth->bindParam(':forward_from', $forward_from, \PDO::PARAM_STR);
$sth->bindParam(':forward_date', $forward_date, \PDO::PARAM_STR);
$reply_to_message = is_object($reply_to_message) ? $reply_to_message->toJSON(): '';
$sth->bindParam(':reply_to_message', $reply_to_message, \PDO::PARAM_STR);
$sth->bindParam(':text', $text, \PDO::PARAM_STR);
......@@ -277,33 +340,42 @@ class DB
$sth->bindParam(':audio', $audio, \PDO::PARAM_STR);
$document = is_object($document) ? $document->toJSON(): '';
$sth->bindParam(':document', $document, \PDO::PARAM_STR);
//Array of photosize...
$sth->bindParam(':photo', !!, \PDO::PARAM_STR);
//TODO what about the Entities array of photosize?
$var = [];
if (is_array($photo)) {
foreach ($photo as $elm) {
$var[] = json_decode($elm->toJSON(), true);
}
$photo = json_encode($var);
} else {
$forward_from = '';
}
$sth->bindParam(':photo', $photo, \PDO::PARAM_STR);
$sticker = is_object($sticker) ? $sticker->toJSON(): '';
$sth->bindParam(':sticker', $sticker, \PDO::PARAM_STR);
$video = is_object($video) ? $video->toJSON(): '';
$sth->bindParam(':video' , $video, \PDO::PARAM_STR);
$sth->bindParam(':video', $video, \PDO::PARAM_STR);
$voice = is_object($voice) ? $voice->toJSON(): '';
$sth->bindParam(':voice', $voice, \PDO::PARAM_STR);
$sth->bindParam(':caption', !!, \PDO::PARAM_STR);
$contanct = is_object($contanct) ? $contanct->toJSON(): '';
$sth->bindParam(':contact', $contanct, \PDO::PARAM_STR);
$sth->bindParam(':caption', $caption, \PDO::PARAM_STR);
$contact = is_object($contact) ? $contact->toJSON(): '';
$sth->bindParam(':contact', $contact, \PDO::PARAM_STR);
$location = is_object($location) ? $location->toJSON(): '';
$sth->bindParam(':location', $location, \PDO::PARAM_STR);
$sth->bindParam(':new_chat_participant', $new_chat_paticipant, \PDO::PARAM_STR);
$sth->bindParam(':left_chat_participant', $left_chat_paticipant, \PDO::PARAM_STR);
//TODO in table users
$sth->bindParam(':new_chat_participant', !!, \PDO::PARAM_STR);
$sth->bindParam(':left_chat_participant', !!, \PDO::PARAM_STR);
$sth->bindParam(':new_chat_title', !!, \PDO::PARAM_STR);
$sth->bindParam(':new_chat_title', $new_chat_title, \PDO::PARAM_STR);
//Array of Photosize
$sth->bindParam(':new_chat_photo', !!, \PDO::PARAM_STR);
$sth->bindParam(':delete_chat_photo', !!, \PDO::PARAM_STR);
$sth->bindParam(':group_chat_created', !!, \PDO::PARAM_STR);
$sth->bindParam(':new_chat_photo', $new_chat_photo, \PDO::PARAM_STR);
$sth->bindParam(':delete_chat_photo', $delete_chat_photo, \PDO::PARAM_STR);
$sth->bindParam(':group_chat_created', $group_chat_created, \PDO::PARAM_STR);
$status = $sth->execute();
......
......@@ -29,10 +29,9 @@ class Document extends Entity
}
$this->thumb = isset($data['thumb']) ? $data['thumb'] : null;
if (empty($this->file_id)) {
throw new TelegramException('thumb is empty!');
if (!empty($this->thumb)) {
$this->thumb = new PhotoSize($this->thumb);
}
$this->thumb = new PhotoSize($this->thumb);
$this->file_name = isset($data['file_name']) ? $data['file_name'] : null;
$this->mime_type = isset($data['mime_type']) ? $data['mime_type'] : null;
......
......@@ -122,9 +122,9 @@ class Message extends Entity
$this->photo = isset($data['photo']) ? $data['photo'] : null; //array of photosize
if (!empty($this->photo)) {
foreach($this->photo as $photo){
if(!empty($photo)) {
$photos[] = new PhotoSize($photo);
foreach ($this->photo as $photo) {
if (!empty($photo)) {
$photos[] = new PhotoSize($photo);
}
}
$this->photo = $photos;
......
......@@ -3,24 +3,24 @@ CREATE TABLE `messages` (
`message_id` bigint COMMENT 'Unique message identifier',
`user_id` bigint COMMENT 'User identifier',
`date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Date the message was sent in timestamp format',
`chat_id` bigint NOT NULL DEFAULT '0' COMMENT 'User or GroupChat object. Conversation the message belongs to — user in case of a private message, GroupChat in case of a group',
`forward_from` VARCHAR(511) DEFAULT '' COMMENT 'User object. For forwarded messages, sender of the original message',
`forward_date` int(511) UNSIGNED DEFAULT 0 COMMENT 'For forwarded messages, date the original message was sent in Unix time',
`chat_id` bigint NOT NULL DEFAULT '0' COMMENT 'Chat identifier.',
`forward_from` bigint NOT NULL DEFAULT '0' COMMENT 'User id. For forwarded messages, sender of the original message',
`forward_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'For forwarded messages, date the original message was sent in Unix time',
`reply_to_message` LONGTEXT COMMENT 'Message object. For replies, the original message. Note that the Message object in this field will not contain further reply_to_message fields even if it itself is a reply.',
`text` LONGTEXT COMMENT 'For text messages, the actual UTF-8 text of the message',
`audio` VARCHAR(511) DEFAULT '' COMMENT 'Audio object. Message is an audio file, information about the file',
`document` VARCHAR(511) DEFAULT '' COMMENT 'Document object. Message is a general file, information about the file',
`photo` VARCHAR(511) DEFAULT '' COMMENT 'Array of PhotoSize objects. Message is a photo, available sizes of the photo',
`sticker` VARCHAR(511) DEFAULT '' COMMENT 'Sticker object. Message is a sticker, information about the sticker',
`video` VARCHAR(511) DEFAULT '' COMMENT 'Video object. Message is a video, information about the video',
`voice` VARCHAR(511) DEFAULT '' COMMENT 'Voice Object. Message is a Voice, information about the Voice',
`audio` TEXT DEFAULT '' COMMENT 'Audio object. Message is an audio file, information about the file',
`document` TEXT DEFAULT '' COMMENT 'Document object. Message is a general file, information about the file',
`photo` TEXT DEFAULT '' COMMENT 'Array of PhotoSize objects. Message is a photo, available sizes of the photo',
`sticker` TEXT DEFAULT '' COMMENT 'Sticker object. Message is a sticker, information about the sticker',
`video` TEXT DEFAULT '' COMMENT 'Video object. Message is a video, information about the video',
`voice` TEXT DEFAULT '' COMMENT 'Voice Object. Message is a Voice, information about the Voice',
`caption` LONGTEXT COMMENT 'For message with caption, the actual UTF-8 text of the caption',
`contact` VARCHAR(511) DEFAULT '' COMMENT 'Contact object. Message is a shared contact, information about the contact',
`location` VARCHAR(511) DEFAULT '' COMMENT 'Location object. Message is a shared location, information about the location',
`new_chat_participant` VARCHAR(511) COMMENT 'User object. A new member was added to the group, information about them (this member may be bot itself)',
`left_chat_participant` VARCHAR(511) COMMENT 'User object. A member was removed from the group, information about them (this member may be bot itself)',
`new_chat_title` VARCHAR(511) DEFAULT '' COMMENT 'A group title was changed to this value',
`new_chat_photo` VARCHAR(511) DEFAULT '' COMMENT 'Array of PhotoSize objects. A group photo was change to this value',
`contact` TEXT DEFAULT '' COMMENT 'Contact object. Message is a shared contact, information about the contact',
`location` TEXT DEFAULT '' COMMENT 'Location object. Message is a shared location, information about the location',
`new_chat_participant` bigint NOT NULL DEFAULT '0' COMMENT 'User id. A new member was added to the group, information about them (this member may be bot itself)',
`left_chat_participant` bigint NOT NULL DEFAULT '0' COMMENT 'User id. A member was removed from the group, information about them (this member may be bot itself)',
`new_chat_title` CHAR(255) DEFAULT '' COMMENT 'A group title was changed to this value',
`new_chat_photo` TEXT DEFAULT '' COMMENT 'Array of PhotoSize objects. A group photo was change to this value',
`delete_chat_photo` tinyint(1) DEFAULT 0 COMMENT 'Informs that the group photo was deleted',
`group_chat_created` tinyint(1) DEFAULT 0 COMMENT 'Informs that the group has been created',
PRIMARY KEY (`update_id`),
......
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