Commit 9bc25ee8 authored by Avtandil Kikabidze's avatar Avtandil Kikabidze

Merge pull request #5 from MBoretto/master

Now the bot handle command llike /command@botname
parents 3a579eab 5f3d7fa8
...@@ -88,12 +88,13 @@ Create set.php and put: ...@@ -88,12 +88,13 @@ Create set.php and put:
$loader = require __DIR__.'/vendor/autoload.php'; $loader = require __DIR__.'/vendor/autoload.php';
$API_KEY = 'your_bot_api_key'; $API_KEY = 'your_bot_api_key';
$BOT_NAME = 'namebot';
// create Telegram API object // create Telegram API object
$telegram = new Longman\TelegramBot\Telegram($API_KEY); $telegram = new Longman\TelegramBot\Telegram($API_KEY,$BOT_NAME);
// set webhook // 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: ...@@ -104,9 +105,9 @@ After create hook.php and put:
$loader = require __DIR__.'/vendor/autoload.php'; $loader = require __DIR__.'/vendor/autoload.php';
$API_KEY = 'your_bot_api_key'; $API_KEY = 'your_bot_api_key';
$BOT_NAME = 'namebot';
// create Telegram API object // create Telegram API object
$telegram = new Longman\TelegramBot\Telegram($API_KEY); $telegram = new Longman\TelegramBot\Telegram($API_KEY,$BOT_NAME);
// handle telegram webhook request // handle telegram webhook request
$telegram->handle(); $telegram->handle();
......
...@@ -20,7 +20,8 @@ ...@@ -20,7 +20,8 @@
"require": { "require": {
"php": ">=5.3.0", "php": ">=5.3.0",
"ext-pdo": "*", "ext-pdo": "*",
"hoa/math": "~0.0" "hoa/math":"~0.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
......
This diff is collapsed.
...@@ -59,8 +59,11 @@ class Message extends Entity ...@@ -59,8 +59,11 @@ class Message extends Entity
protected $_command; protected $_command;
protected $bot_name;
public function __construct(array $data) { public function __construct(array $data,$bot_name) {
$this->bot_name = $bot_name;
$this->message_id = isset($data['message_id']) ? $data['message_id'] : null; $this->message_id = isset($data['message_id']) ? $data['message_id'] : null;
if (empty($this->message_id)) { if (empty($this->message_id)) {
...@@ -102,7 +105,12 @@ class Message extends Entity ...@@ -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() { public function getCommand() {
...@@ -110,13 +118,24 @@ class Message extends Entity ...@@ -110,13 +118,24 @@ class Message extends Entity
return $this->_command; return $this->_command;
} }
$cmd = $this->getFullCommand();
$cmd = strtok($this->text, ' ');
if (substr($cmd, 0, 1) === '/') { if (substr($cmd, 0, 1) === '/') {
$cmd = substr($cmd, 1); $cmd = substr($cmd, 1);
//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 $this->_command = $cmd;
} }
}
return false; return false;
} }
...@@ -162,8 +181,8 @@ class Message extends Entity ...@@ -162,8 +181,8 @@ class Message extends Entity
public function getText($without_cmd = false) { public function getText($without_cmd = false) {
$text = $this->text; $text = $this->text;
if ($without_cmd) { if ($without_cmd) {
$command = $this->getCommand(); $command = $this->getFullCommand();
$text = substr($text, strlen('/'.$command.' '), strlen($text)); $text = substr($text, strlen($command.' '), strlen($text));
} }
return $text; return $text;
......
...@@ -17,11 +17,12 @@ class Update extends Entity ...@@ -17,11 +17,12 @@ class Update extends Entity
protected $update_id; protected $update_id;
protected $message; 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; $update_id = isset($data['update_id']) ? $data['update_id'] : null;
...@@ -31,8 +32,9 @@ class Update extends Entity ...@@ -31,8 +32,9 @@ class Update extends Entity
throw new \Exception('update_id is empty!'); throw new \Exception('update_id is empty!');
} }
$this->bot_name = $bot_name;
$this->update_id = $update_id; $this->update_id = $update_id;
$this->message = new Message($message); $this->message = new Message($message,$bot_name);
} }
......
...@@ -37,6 +37,14 @@ class Telegram ...@@ -37,6 +37,14 @@ class Telegram
*/ */
protected $api_key = ''; protected $api_key = '';
/**
* Telegram API name
*
* @var string
*/
protected $bot_name = '';
/** /**
* Raw request data * Raw request data
* *
...@@ -101,8 +109,10 @@ class Telegram ...@@ -101,8 +109,10 @@ class Telegram
* *
* @param string $api_key * @param string $api_key
*/ */
public function __construct($api_key) { public function __construct($api_key,$bot_name) {
$this->api_key = $api_key; $this->api_key = $api_key;
$this->bot_name = $bot_name;
Request::initialize($this); Request::initialize($this);
} }
...@@ -220,7 +230,7 @@ class Telegram ...@@ -220,7 +230,7 @@ class Telegram
} }
$update = new Update($post); $update = new Update($post,$this->bot_name);
$this->insertRequest($update); $this->insertRequest($update);
...@@ -374,6 +384,15 @@ class Telegram ...@@ -374,6 +384,15 @@ class Telegram
return $this->api_key; return $this->api_key;
} }
/**
* Get BOT NAME
*
* @return string
*/
public function getBotName() {
return $this->bot_name;
}
/** /**
* Set Webhook for bot * 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