Commit 4a788a36 authored by MBoretto's avatar MBoretto

Renaming to Conversation, Readme

parent 6a0d3d33
......@@ -348,7 +348,7 @@ You can also store inline query and chosen inline query in the database.
### Channels Support
All methods implemented can be used to manage channels.
With [admin commands](#admin-commands) you can manage your channel directly with your bot private chat.
With [admin commands](#admin-commands) you can manage your channels directly with your bot private chat.
### Commands
......@@ -397,7 +397,7 @@ $telegram->setCommandConfig('date', ['google_api_key' => 'your_google_api_key_he
Enabling this feature, the admin bot can perform some super user commands like:
- Send message to all chats */sendtoall*
- List all the chats started with the bot */chats*
- Send a message to a channel */sendtochannel* (NEW! see below how to configure it)
- Send a message to your channels */sendtochannel* (NEW! see below how to configure it)
You can specify one or more admins with this option:
```php
......@@ -414,7 +414,11 @@ To enable this feature follow these steps:
- Enable admin interface for your user as explained in the admin section above.
- Enter your channel name as a parameter for the */sendtochannel* command:
```php
$telegram->setCommandConfig('sendtochannel', ['your_channel' => '@type_here_your_channel']);
$telegram->setCommandConfig('sendtochannel', ['your_channel' => ['@type_here_your_channel']]);
```
- If you want to manage more channels:
```php
$telegram->setCommandConfig('sendtochannel', ['your_channel'=>['@type_here_your_channel', '@type_here_another_channel', '@and_so_on']]);
```
- Enjoy!
......
......@@ -10,7 +10,7 @@
namespace Longman\TelegramBot\Commands\AdminCommands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Tracking;
use Longman\TelegramBot\Conversation;
use Longman\TelegramBot\Entities\Message;
use Longman\TelegramBot\Entities\ReplyKeyboardHide;
use Longman\TelegramBot\Entities\ReplyKeyboardMarkup;
......@@ -50,9 +50,9 @@ class SendtochannelCommand extends AdminCommand
$data['chat_id'] = $chat_id;
//tracking
$path = new Tracking($user_id, $chat_id, $this->getName());
$path->startTrack();
$session = $path->GetData();
$conversation = new Conversation($user_id, $chat_id, $this->getName());
$conversation->start();
$session = $conversation->getData();
if (!isset($session['state'])) {
$state = '0';
......@@ -68,7 +68,7 @@ class SendtochannelCommand extends AdminCommand
case 0:
if ($type != 'Message' || !in_array(trim($text), $channels)) {
$session['state'] = '0';
$path->updateTrack($session);
$conversation->update($session);
$keyboard = [];
foreach ($channels as $channel) {
......@@ -97,7 +97,7 @@ class SendtochannelCommand extends AdminCommand
case 1:
if ($session['last_message_id'] == $message->getMessageId()) {
$session['state'] = 1;
$path->updateTrack($session);
$conversation->update($session);
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]);
$data['text'] = 'Insert the content you want to share: text, photo, audio...';
......@@ -112,7 +112,7 @@ class SendtochannelCommand extends AdminCommand
case 2:
if ($session['last_message_id'] == $message->getMessageId() || !($text == 'Yes' || $text == 'No')) {
$session['state'] = 2;
$path->updateTrack($session);
$conversation->update($session);
if ($session['message_type'] == 'Video' || $session['message_type'] == 'Photo') {
$keyboard = [['Yes', 'No']];
......@@ -143,7 +143,7 @@ class SendtochannelCommand extends AdminCommand
case 3:
if (($session['last_message_id'] == $message->getMessageId() || $type != 'Message' ) && $session['set_caption']) {
$session['state'] = 3;
$path->updateTrack($session);
$conversation->update($session);
$data['text'] = 'Insert caption:';
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]);
......@@ -156,7 +156,7 @@ class SendtochannelCommand extends AdminCommand
case 4:
if ($session['last_message_id'] == $message->getMessageId() || !($text == 'Yes' || $text == 'No')) {
$session['state'] = '4';
$path->updateTrack($session);
$conversation->update($session);
$data['text'] = 'Message will look like this:';
$result = Request::sendMessage($data);
......@@ -194,7 +194,7 @@ class SendtochannelCommand extends AdminCommand
$session['last_message_id'] = $message->getMessageId();
// no break
case 5:
$path->stopTrack();
$conversation->stop();
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]);
......@@ -225,9 +225,20 @@ class SendtochannelCommand extends AdminCommand
}
/**
* SendBack
*
* Received a message, the bot can send a copy of it to another chat/channel.
* You don't have to care about the type of thei message, the function detect it and use the proper
* REQUEST:: function to send it.
* $data include all the var that you need to send the message to the propor chat
*
* @todo Complete dock block
* @todo This method will be moved at an higher level maybe in AdminCommans or Command
* @todo Looking for a more significative name
*
* @param Longman\TelegramBot\Entities\Message $message
* @param array $data
*
* @return Longman\TelegramBot\Entities\ServerResponse
*/
public function sendBack(Message $message, array $data)
{
......
......@@ -10,7 +10,7 @@
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Tracking;
use Longman\TelegramBot\Conversation;
use Longman\TelegramBot\Commands\SystemCommand;
/**
......@@ -50,7 +50,7 @@ class GenericmessageCommand extends SystemCommand
$chat_id = $message->getChat()->getId();
$user_id = $message->getFrom()->getId();
//Fetch Track if exist
$command = (new Tracking($user_id, $chat_id))->getTrackCommand();
$command = (new Conversation($user_id, $chat_id))->getConversationCommand();
if (! is_null($command)) {
return $this->telegram->executeCommand($command, $this->update);
}
......
......@@ -10,7 +10,7 @@
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Tracking;
use Longman\TelegramBot\Conversation;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\ForceReply;
use Longman\TelegramBot\Entities\ReplyKeyboardHide;
......@@ -57,11 +57,11 @@ class SurveyCommand extends UserCommand
$data['chat_id'] = $chat_id;
//tracking
$path = new Tracking($user_id, $chat_id, $this->getName());
$path->startTrack();
$conversation = new Conversation($user_id, $chat_id, $this->getName());
$conversation->start();
//cache data from the tracking session if any
$session = $path->GetData();
$session = $conversation->GetData();
if (!isset($session['state'])) {
$state = '0';
} else {
......@@ -75,7 +75,7 @@ class SurveyCommand extends UserCommand
case 0:
if (empty($text)) {
$session['state'] = '0';
$path->updateTrack($session);
$conversation->update($session);
$data['text'] = 'Type your name:';
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]);
......@@ -88,7 +88,7 @@ class SurveyCommand extends UserCommand
case 1:
if (empty($text)) {
$session['state'] = 1;
$path->updateTrack($session);
$conversation->update($session);
$data['text'] = 'Type your surname:';
$result = Request::sendMessage($data);
......@@ -102,7 +102,7 @@ class SurveyCommand extends UserCommand
case 2:
if (empty($text) || !is_numeric($text)) {
$session['state'] = '2';
$path->updateTrack($session);
$conversation->update($session);
$data['text'] = 'Type your age:';
if (!empty($text) && !is_numeric($text)) {
$data['text'] = 'Type your age, must be a number';
......@@ -117,7 +117,7 @@ class SurveyCommand extends UserCommand
case 3:
if (empty($text) || !($text == 'M' || $text == 'F')) {
$session['state'] = '3';
$path->updateTrack($session);
$conversation->update($session);
$keyboard = [['M','F']];
$reply_keyboard_markup = new ReplyKeyboardMarkup(
......@@ -143,7 +143,7 @@ class SurveyCommand extends UserCommand
case 4:
if (is_null($message->getLocation())) {
$session['state'] = '4';
$path->updateTrack($session);
$conversation->update($session);
$data['text'] = 'Insert your home location (need location object):';
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]);
......@@ -158,7 +158,7 @@ class SurveyCommand extends UserCommand
case 5:
if (is_null($message->getPhoto())) {
$session['state'] = '5';
$path->updateTrack($session);
$conversation->update($session);
$data['text'] = 'Insert your picture:';
$result = Request::sendMessage($data);
......@@ -168,7 +168,7 @@ class SurveyCommand extends UserCommand
// no break
case 6:
$path->stopTrack();
$conversation->stop();
$out_text = '/Survey result:' . "\n";
unset($session['state']);
foreach ($session as $k => $v) {
......
......@@ -12,16 +12,16 @@ namespace Longman\TelegramBot;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\TrackingDB;
use Longman\TelegramBot\ConversationDB;
use Longman\TelegramBot\Entities\Update;
/**
* Class Tracking
* Class Conversation
*/
class Tracking
class Conversation
{
/**
* Track has been fetched true false
* Conversation has been fetched true false
*
* @var bool
*/
......@@ -32,10 +32,10 @@ class Tracking
*
* @var array
*/
protected $track = null;
protected $conversation = null;
/**
* Data stored inside the track
* Data stored inside the Conversation
*
* @var array
*/
......@@ -57,21 +57,21 @@ class Tracking
/**
* Group name let you share the session among commands
* Call this as the same name of the command if you don't need to share the tracking
* Call this as the same name of the command if you don't need to share the conversation
*
* @var strint
*/
protected $group_name;
/**
* Command to be execute if the track is active
* Command to be execute if the conversation is active
*
* @var string
*/
protected $command;
/**
* Tracking contructor initialize a new track
* Conversation contructor initialize a new conversation
*
* @param int $user_id
* @param int $chat_id
......@@ -91,55 +91,55 @@ class Tracking
}
/**
* Check if the track already exist
* Check if the conversation already exist
*
* @return bool
*/
protected function trackExist()
protected function conversationExist()
{
//Track info already fetched
//Conversation info already fetched
if ($this->is_fetched) {
return true;
}
$track = TrackingDB::selectTrack($this->user_id, $this->chat_id, 1);
$conversation = ConversationDB::selectConversation($this->user_id, $this->chat_id, 1);
$this->is_fetched = true;
if (isset($track[0])) {
if (isset($conversation[0])) {
//Pick only the first element
$this->track = $track[0];
$this->conversation = $conversation[0];
if (is_null($this->group_name)) {
//Track name and command has not been specified. command has to be retrieved
//Conversation name and command has not been specified. command has to be retrieved
return true;
}
//a track with the same name was already opened store the data
if ($this->track['track_name'] == $this->group_name) {
$this->data = json_decode($this->track['data'], true);
if ($this->conversation['conversation_name'] == $this->group_name) {
$this->data = json_decode($this->conversation['data'], true);
return true;
}
//a track with a differet name has been opened unsetting the DB one and reacreatea a new one
TrackingDB::updateTrack(['is_active' => 0], ['chat_id' => $this->chat_id, 'user_id' => $this->user_id]);
//a with a differet name has been opened unsetting the DB one and reacreatea a new one
ConversationDB::updateConversation(['is_active' => 0], ['chat_id' => $this->chat_id, 'user_id' => $this->user_id]);
return false;
}
$this->track = null;
$this->conversation = null;
return false;
}
/**
* Check if the tracke already exist
* Check if the Conversation already exist
*
* Check if a track has already been created in the database. If the track is not found, a new track is created. startTrack fetch the data stored in the database
* Check if a conversation has already been created in the database. If the conversation is not found, a new conversation is created. start fetch the data stored in the database
*
* @return bool
*/
public function startTrack()
public function start()
{
if (!$this->trackExist()) {
$status = TrackingDB::insertTrack($this->command, $this->group_name, $this->user_id, $this->chat_id);
if (!$this->conversationExist()) {
$status = ConversationDB::insertConversation($this->command, $this->group_name, $this->user_id, $this->chat_id);
$this->is_fetched = true;
}
return true;
......@@ -150,49 +150,49 @@ class Tracking
*
* @param array $data
*/
public function updateTrack($data)
public function update($data)
{
//track must exist!
if ($this->trackExist()) {
//conversation must exist!
if ($this->conversationExist()) {
$fields['data'] = json_encode($data);
TrackingDB::updateTrack($fields, ['chat_id' => $this->chat_id, 'user_id' => $this->user_id]);
ConversationDB::updateConversation($fields, ['chat_id' => $this->chat_id, 'user_id' => $this->user_id]);
//TODO verify query success before convert the private var
$this->data = $data;
}
}
/**
* Delete the track from the database
* Delete the conversation from the database
*
* Currently the Track is not deleted but just unsetted
* Currently the Convevrsation is not deleted but just unsetted
*
* @TODO should return something
*
* @param array $data
*/
public function stopTrack()
public function stop()
{
if ($this->trackExist()) {
TrackingDB::updateTrack(['is_active' => 0], ['chat_id' => $this->chat_id, 'user_id' => $this->user_id]);
if ($this->conversationExist()) {
ConversationDB::updateConversation(['is_active' => 0], ['chat_id' => $this->chat_id, 'user_id' => $this->user_id]);
}
}
/**
* Retrieve the command to execute from the track
* Retrieve the command to execute from the conversation
*
* @param string
*/
public function getTrackCommand()
public function getConversationCommand()
{
if ($this->trackExist()) {
return $this->track['track_command'];
if ($this->conversationExist()) {
return $this->conversation['conversation_command'];
}
return null;
}
/**
* Retrive the data store in the track
* Retrive the data store in the conversation
*
* @param array $data
*/
......
......@@ -13,22 +13,22 @@ namespace Longman\TelegramBot;
use Longman\TelegramBot\DB;
/**
* Class TrackingDB
* Class ConversationDB
*/
class TrackingDB extends DB
class ConversationDB extends DB
{
/**
* Initilize tracking table
* Initilize conversation table
*/
public static function initializeTracking()
public static function initializeConversation()
{
if (!defined('TB_TRACK')) {
define('TB_TRACK', self::$table_prefix . 'track');
if (!defined('TB_CONVERSATION')) {
define('TB_CONVERSATION', self::$table_prefix . 'conversation');
}
}
/**
* Tracking contructor initialize a new track
* Conversation contructor initialize a new conversation
*
* @param int $user_id
* @param int $chat_id
......@@ -36,14 +36,14 @@ class TrackingDB extends DB
*
* @return array
*/
public static function selectTrack($user_id, $chat_id, $limit = null)
public static function selectConversation($user_id, $chat_id, $limit = null)
{
if (!self::isDbConnected()) {
return false;
}
try {
$query = 'SELECT * FROM `' . TB_TRACK . '` ';
$query = 'SELECT * FROM `' . TB_CONVERSATION . '` ';
$query .= 'WHERE `is_active` = 1 ';
$query .= 'AND `chat_id` = :chat_id ';
$query .= 'AND `user_id` = :user_id ';
......@@ -68,28 +68,28 @@ class TrackingDB extends DB
}
/**
* Insert the track in the database
* Insert the conversation in the database
*
* @param string $track_command
* @param string $track_name
* @param string $conversation_command
* @param string $conversation_name
* @param int $user_id
* @param int $chat_id
*
* @return bool
*/
public static function insertTrack($track_command, $track_group_name, $user_id, $chat_id)
public static function insertConversation($conversation_command, $conversation_group_name, $user_id, $chat_id)
{
if (!self::isDbConnected()) {
return false;
}
try {
$sth = self::$pdo->prepare('INSERT INTO `' . TB_TRACK . '`
$sth = self::$pdo->prepare('INSERT INTO `' . TB_CONVERSATION . '`
(
`is_active`, `track_command`, `track_name`, `user_id`, `chat_id`, `data`, `created_at`, `updated_at`
`is_active`, `conversation_command`, `conversation_name`, `user_id`, `chat_id`, `data`, `created_at`, `updated_at`
)
VALUES (
:is_active, :track_command, :track_name, :user_id, :chat_id, :data, :date, :date
:is_active, :conversation_command, :conversation_name, :user_id, :chat_id, :data, :date, :date
)
');
......@@ -98,8 +98,8 @@ class TrackingDB extends DB
$created_at = self::getTimestamp();
$sth->bindParam(':is_active', $active);
$sth->bindParam(':track_command', $track_command);
$sth->bindParam(':track_name', $track_group_name);
$sth->bindParam(':conversation_command', $conversation_command);
$sth->bindParam(':conversation_name', $conversation_group_name);
$sth->bindParam(':user_id', $user_id);
$sth->bindParam(':chat_id', $chat_id);
$sth->bindParam(':data', $data);
......@@ -113,20 +113,20 @@ class TrackingDB extends DB
}
/**
* Update a specific track
* Update a specific conversation
*
* @param array $fields_values
* @param array $where_fields_values
*
* @return bool
*/
public static function updateTrack(array $fields_values, array $where_fields_values)
public static function updateConversation(array $fields_values, array $where_fields_values)
{
return self::update(TB_TRACK, $fields_values, $where_fields_values);
return self::update(TB_CONVERSATION, $fields_values, $where_fields_values);
}
/**
* Insert the track in the database
* Insert the conversation in the database
*
* @param string $table
* @param array $fields_values
......
......@@ -177,7 +177,7 @@ class Telegram
public function enableMySQL(array $credential, $table_prefix = null)
{
$this->pdo = DB::initialize($credential, $this, $table_prefix);
TrackingDB::initializeTracking();
ConversationDB::initializeConversation();
$this->mysql_enabled = true;
return $this;
}
......
......@@ -136,13 +136,13 @@ CREATE TABLE IF NOT EXISTS `telegram_update` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE IF NOT EXISTS `track` (
CREATE TABLE IF NOT EXISTS `conversation` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Row unique id',
`user_id` bigint NULL DEFAULT NULL COMMENT 'User id',
`chat_id` bigint NULL DEFAULT NULL COMMENT 'Telegram chat_id can be a the user id or the chat id ',
`is_active` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1 track is active 0 track has been deactivated',
`track_command` varchar(160) DEFAULT '' COMMENT 'Default Command to execute',
`track_name` varchar(160) NOT NULL DEFAULT '' COMMENT 'Name of the track can be the command name or a generic name for tracking between multiple commands',
`is_active` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1 conversation is active 0 conversation has been deactivated',
`conversation_command` varchar(160) DEFAULT '' COMMENT 'Default Command to execute',
`conversation_name` varchar(160) NOT NULL DEFAULT '' COMMENT 'Name of the conversation can be the command name or a generic name for conversation between multiple commands',
`data` varchar(1000) DEFAULT 'NULL' COMMENT 'Data stored from command',
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
......@@ -151,7 +151,7 @@ CREATE TABLE IF NOT EXISTS `track` (
KEY `user_id` (`user_id`),
KEY `chat_id` (`chat_id`),
KEY `is_active` (`is_active`),
KEY `track_name` (`track_name`),
KEY `conversation_name` (`conversation_name`),
FOREIGN KEY (`user_id`)
REFERENCES `user` (`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