Commit d846fa4a authored by MBoretto's avatar MBoretto

send message to multiple chat and other stuff

parent 4378f6cf
...@@ -15,7 +15,7 @@ A Telegram Bot based on the official [Telegram Bot API](https://core.telegram.or ...@@ -15,7 +15,7 @@ A Telegram Bot based on the official [Telegram Bot API](https://core.telegram.or
## introduction ## introduction
This is a pure php Telegram Bot, fully extensible via plugins. Telegram recently announced official support for a [Bot API](https://telegram.org/blog/bot-revolution) allowing integrators of all sorts to bring automated interactions to the mobile platform. This Bot aims to provide a platform where one could simply write a plugin and have interactions in a matter of minutes. This is a pure php Telegram Bot, fully extensible via plugins. Telegram recently announced official support for a [Bot API](https://telegram.org/blog/bot-revolution) allowing integrators of all sorts to bring automated interactions to the mobile platform. This Bot aims to provide a platform where one could simply write a plugin and have interactions in a matter of minutes.
The Bot support Reply Markup and handle commands in group chat. The Bot supports Reply Markup and handle commands in group chat with multiple bot.
Instructions Instructions
...@@ -153,31 +153,66 @@ $credentials = array('host'=>'localhost', 'user'=>'dbuser', 'password'=>'dbpass' ...@@ -153,31 +153,66 @@ $credentials = array('host'=>'localhost', 'user'=>'dbuser', 'password'=>'dbpass'
$telegram->enableMySQL($credentials); $telegram->enableMySQL($credentials);
``` ```
You can set a custom prefix to all the tables while you are enabling mysql:
```php
$telegram->enableMySQL($credentials, $BOT_NAME.'_');
```
Commads
--------------
The bot is able to recognise commands in chat with multiple bot.
It can execute command triggering a chat event. Here's the list:
-Groupchatcreated (GroupchatcreatedCommand.php)
-Newchatparticipant (NewchatparticipantCommand.php)
-Deletechatphoto (DeletechatphotoCommand.php)
-Newchattitle (NewchattitleCommand.php)
-Leftchatparticipant (LeftchatparticipantCommand.php)
GenericCommand.php let you handle commands that non exist or use commands as var:
Favourite colour? /black /red
Favourite number? /1 /134
Utilis
------
Maybe you would like to develop your own commands. A good practice is to store them outside vendor/. This can be done adding before the method: Maybe you would like to develop your own commands. A good practice is to store them outside vendor/. This can be done adding before the method:
```php ```php
$COMMANDS_FOLDER = __DIR__.'/Commands/'; $COMMANDS_FOLDER = __DIR__.'/Commands/';
$telegram->addCommandsPath($COMMANDS_FOLDER); $telegram->addCommandsPath($COMMANDS_FOLDER);
```
Send message to all active chats (new!)
---------------------------------------
To do this you have to enable the mysql connections.
Here's an example of use:
```php
$results = $telegram->sendToActiveChats(
'sendMessage', //callback function to execute (see Request.php methods)
array('text'=>'Hey! Checkout the new feature!!'), //Param to evaluate the request
true, //Send to chats (group chat)
true, //Send to users (single chat)
null, //'yyyy-mm-dd hh:mm:ss' date range from
null //'yyyy-mm-dd hh:mm:ss' date range to
);
print_r($results);
``` ```
Utilis
------
You can also log incoming messages on a text file, set this option with the methods: You can also log incoming messages on a text file, set this option with the methods:
```php ```php
$telegram->setLogRequests(true);
$telegram->setLogRequests(true); $telegram->setLogPath($BOT_NAME.'.log');
$telegram->setLogPath($BOT_NAME.'.log');
``` ```
This code is available on [Github][0]. Pull requests are welcome. This code is available on [Github][0]. Pull requests are welcome.
......
<?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;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
class DeletechatphotoCommand extends Command
{
protected $name = 'Deletechatphoto';
protected $description = 'Delete chat photo';
protected $usage = '/';
protected $version = '1.0.0';
protected $enabled = true;
public function execute()
{
$update = $this->getUpdate();
$message = $this->getMessage();
$delete_chat_photo = $message->getDeleteChatPhoto();
// temporary do nothing
}
}
<?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;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
class GenericCommand extends Command
{
protected $name = 'Generic';
protected $description = 'Handle genric commands or is executed by defaul when a command is not found';
protected $usage = '/';
protected $version = '1.0.0';
protected $enabled = true;
public function execute()
{
$update = $this->getUpdate();
$message = $this->getMessage();
//you can use $command as param
$command = $message->getCommand();
$data['text'] = 'Command: '.$command.' not found.. :(';
$result = Request::sendMessage($data);
return $result;
}
}
<?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;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
class GroupchatcreatedCommand extends Command
{
protected $name = 'Groupchatcreated';
protected $description = 'Group chat created';
protected $usage = '/';
protected $version = '1.0.0';
protected $enabled = true;
public function execute()
{
$update = $this->getUpdate();
$message = $this->getMessage();
$group_chat_created = $message->getGroupChatCreated();
// temporary do nothing
}
}
...@@ -16,7 +16,7 @@ use Longman\TelegramBot\Entities\Update; ...@@ -16,7 +16,7 @@ use Longman\TelegramBot\Entities\Update;
class LeftchatparticipantCommand extends Command class LeftchatparticipantCommand extends Command
{ {
protected $name = 'left_chat_participant'; protected $name = 'leftchatparticipant';
protected $description = 'Left Chat Participant'; protected $description = 'Left Chat Participant';
protected $usage = '/'; protected $usage = '/';
protected $version = '1.0.0'; protected $version = '1.0.0';
......
...@@ -16,7 +16,7 @@ use Longman\TelegramBot\Entities\Update; ...@@ -16,7 +16,7 @@ use Longman\TelegramBot\Entities\Update;
class NewchatparticipantCommand extends Command class NewchatparticipantCommand extends Command
{ {
protected $name = 'new_chat_participant'; protected $name = 'Newchatparticipant';
protected $description = 'New Chat Participant'; protected $description = 'New Chat Participant';
protected $usage = '/'; protected $usage = '/';
protected $version = '1.0.0'; protected $version = '1.0.0';
......
<?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;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
class NewchattitleCommand extends Command
{
protected $name = 'Newchattitle';
protected $description = 'New chat Title';
protected $usage = '/';
protected $version = '1.0.0';
protected $enabled = true;
public function execute()
{
$update = $this->getUpdate();
$message = $this->getMessage();
$new_chat_title = $message->getNewChatTitle();
// temporary do nothing
}
}
...@@ -57,7 +57,6 @@ class User extends Entity ...@@ -57,7 +57,6 @@ class User extends Entity
public function getUsername() public function getUsername()
{ {
return $this->username; return $this->username;
} }
} }
...@@ -102,6 +102,14 @@ class Request ...@@ -102,6 +102,14 @@ class Request
return $result; return $result;
} }
public static function getMe()
{
$result = self::send('getMe');
return $result;
}
public static function setWebhook($url) public static function setWebhook($url)
{ {
$result = self::send('setWebhook', array('url' => $url)); $result = self::send('setWebhook', array('url' => $url));
......
This diff is collapsed.
...@@ -2,8 +2,8 @@ CREATE TABLE `messages` ( ...@@ -2,8 +2,8 @@ CREATE TABLE `messages` (
`update_id` bigint UNSIGNED COMMENT 'The update\'s unique identifier.', `update_id` bigint UNSIGNED COMMENT 'The update\'s unique identifier.',
`message_id` bigint COMMENT 'Unique message identifier', `message_id` bigint COMMENT 'Unique message identifier',
`user_id` bigint COMMENT 'User identifier', `user_id` bigint COMMENT 'User identifier',
`date` int(11) UNSIGNED COMMENT 'Date the message was sent in Unix time', `date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Date the message was sent in timestamp format',
`chat` CHAR(255) COMMENT 'User or GroupChat object. Conversation the message belongs to — user in case of a private message, GroupChat in case of a group', `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` CHAR(255) DEFAULT '' COMMENT 'User object. For forwarded messages, sender of the original message', `forward_from` CHAR(255) DEFAULT '' COMMENT 'User object. For forwarded messages, sender of the original message',
`forward_date` int(11) UNSIGNED DEFAULT 0 COMMENT 'For forwarded messages, date the original message was sent in Unix time', `forward_date` int(11) UNSIGNED DEFAULT 0 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.', `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.',
...@@ -24,13 +24,37 @@ CREATE TABLE `messages` ( ...@@ -24,13 +24,37 @@ CREATE TABLE `messages` (
PRIMARY KEY (`update_id`), PRIMARY KEY (`update_id`),
KEY `message_id` (`message_id`), KEY `message_id` (`message_id`),
KEY `user_id` (`user_id`) KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE `users` ( CREATE TABLE `users` (
`id` bigint UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Unique user identifier', `id` bigint NOT NULL DEFAULT '0' COMMENT 'Unique user identifier',
`username` CHAR(255) NOT NULL DEFAULT '' COMMENT 'User username',
`first_name` CHAR(255) NOT NULL DEFAULT '' COMMENT 'User first name', `first_name` CHAR(255) NOT NULL DEFAULT '' COMMENT 'User first name',
`last_name` CHAR(255) NOT NULL DEFAULT '' COMMENT 'User last name', `last_name` CHAR(255) DEFAULT '' COMMENT 'User last name',
`username` CHAR(255) DEFAULT '' COMMENT 'User username',
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Entry date creation',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Entry date update',
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
KEY `username` (`username`) KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
\ No newline at end of file
CREATE TABLE `chats` (
`id` bigint NOT NULL DEFAULT '0' COMMENT 'Unique user or chat identifier',
`title` CHAR(255) DEFAULT '' COMMENT 'chat title null if case of single chat with the bot',
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Entry date creation',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'Entry date update',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
CREATE TABLE `users_chats` (
`user_id` bigint NOT NULL DEFAULT '0' COMMENT 'Unique user identifier',
`chat_id` bigint NOT NULL DEFAULT '0' COMMENT 'Unique user or chat identifier',
PRIMARY KEY (`user_id`, `chat_id`),
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (`chat_id`) REFERENCES `chats` (`id`)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
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