Commit cfb663e8 authored by MBoretto's avatar MBoretto

Added support for command like /command@bot1

parent 3a579eab
......@@ -88,12 +88,13 @@ Create set.php and put:
$loader = require __DIR__.'/vendor/autoload.php';
$API_KEY = 'your_bot_api_key';
$BOT_NAME = 'namebot';
// create Telegram API object
$telegram = new Longman\TelegramBot\Telegram($API_KEY);
$telegram = new Longman\TelegramBot\Telegram($API_KEY,$BOT_NAME);
// set webhook
$telegram->setWebHook('https://yourdomain/yourpath_to_hook.php');
echo $telegram->setWebHook('https://yourdomain/yourpath_to_hook.php');
```
......@@ -104,9 +105,9 @@ After create hook.php and put:
$loader = require __DIR__.'/vendor/autoload.php';
$API_KEY = 'your_bot_api_key';
$BOT_NAME = 'namebot';
// create Telegram API object
$telegram = new Longman\TelegramBot\Telegram($API_KEY);
$telegram = new Longman\TelegramBot\Telegram($API_KEY,$BOT_NAME);
// handle telegram webhook request
$telegram->handle();
......@@ -136,4 +137,4 @@ If you like living on the edge, please report any bugs you find on the [PHP Tele
Created by [Avtandil Kikabidze][1].
[0]: https://github.com/akalongman/php-telegram-bot
[1]: mailto:akalongman@gmail.com
\ No newline at end of file
[1]: mailto:akalongman@gmail.com
......@@ -59,8 +59,11 @@ class Message extends Entity
protected $_command;
protected $bot_name;
public function __construct(array $data) {
publicfunction __construct(array $data,$bot_name) {
$this->bot_name = $bot_name;
$this->message_id = isset($data['message_id']) ? $data['message_id'] : null;
if (empty($this->message_id)) {
......@@ -102,7 +105,12 @@ class Message extends Entity
}
}
//return the entire command like /echo or /echo@bot1 if specified
public function getFullCommand(){
return strtok($this->text, ' ');
}
public function getCommand() {
......@@ -110,13 +118,24 @@ class Message extends Entity
return $this->_command;
}
$cmd = strtok($this->text, ' ');
$cmd = $this->getFullCommand();
if (substr($cmd, 0, 1) === '/') {
$cmd = substr($cmd, 1);
return $this->_command = $cmd;
//check if command is follow by botname
$split_cmd = explode('@', $cmd);
if(isset($split_cmd[1])){
//command is followed by name check if is addressed to me
if(strtolower($split_cmd[1]) == strtolower($this->bot_name)){
return $this->_command = $split_cmd[0];
}
}else{
//command is not followed by name
return $this->_command = $cmd;
}
}
return false;
}
......@@ -162,8 +181,8 @@ class Message extends Entity
public function getText($without_cmd = false) {
$text = $this->text;
if ($without_cmd) {
$command = $this->getCommand();
$text = substr($text, strlen('/'.$command.' '), strlen($text));
$command = $this->getFullCommand();
$text = substr($text, strlen($command.' '), strlen($text));
}
return $text;
......
......@@ -17,11 +17,12 @@ class Update extends Entity
protected $update_id;
protected $message;
protected $bot_name;
public function __construct(array $data) {
public function __construct(array $data, $bot_name) {
$update_id = isset($data['update_id']) ? $data['update_id'] : null;
......@@ -31,8 +32,9 @@ class Update extends Entity
throw new \Exception('update_id is empty!');
}
$this->bot_name = $bot_name;
$this->update_id = $update_id;
$this->message = new Message($message);
$this->message = new Message($message,$bot_name);
}
......
......@@ -37,6 +37,14 @@ class Telegram
*/
protected $api_key = '';
/**
* Telegram API name
*
* @var string
*/
protected $bot_name = '';
/**
* Raw request data
*
......@@ -101,8 +109,10 @@ class Telegram
*
* @param string $api_key
*/
public function __construct($api_key) {
public function __construct($api_key,$bot_name) {
$this->api_key = $api_key;
$this->bot_name = $bot_name;
Request::initialize($this);
}
......@@ -220,7 +230,7 @@ class Telegram
}
$update = new Update($post);
$update = new Update($post,$this->bot_name);
$this->insertRequest($update);
......@@ -374,6 +384,15 @@ class Telegram
return $this->api_key;
}
/**
* Get BOT NAME
*
* @return string
*/
public function getBotName() {
return $this->bot_name;
}
/**
* Set Webhook for bot
*
......
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