Commit e5ccb10c authored by Avtandil Kikabidze's avatar Avtandil Kikabidze

Merge pull request #15 from MBoretto/master

Full Reply Markup support, new methods in Message Entities
parents 2b293902 1053c036
...@@ -14,7 +14,8 @@ A Telegram Bot based on the official [Telegram Bot API](https://core.telegram.or ...@@ -14,7 +14,8 @@ 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 the group chat.
Instructions Instructions
......
...@@ -39,4 +39,4 @@ ...@@ -39,4 +39,4 @@
"phpspec/phpspec": "~2.1", "phpspec/phpspec": "~2.1",
"squizlabs/php_codesniffer": "2.3.*" "squizlabs/php_codesniffer": "2.3.*"
} }
} }
\ No newline at end of file
This diff is collapsed.
<?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.
* Written by <marco.bore@gmail.com>
*/
namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Entities\ReplyKeyboardMarkup;
use Longman\TelegramBot\Entities\ReplyKeyboardHide;
use Longman\TelegramBot\Entities\ForceReply;
class ForceReplyCommand extends Command
{
protected $name = 'forcereply';
protected $description = 'Force reply with reply markup';
protected $usage = '/forcereply';
protected $version = '0.0.5';
protected $enabled = true;
public function execute()
{
$update = $this->getUpdate();
$message = $this->getMessage();
$message_id = $message->getMessageId();
$chat_id = $message->getChat()->getId();
$text = $message->getText(true);
$data = array();
$data['chat_id'] = $chat_id;
$data['text'] = 'Write something:';
#$data['reply_to_message_id'] = $message_id;
$json = (
new ForceReply(
array(
'selective' => false
)
)
)->toJSON();
#echo $json;
$data['reply_markup'] = $json;
$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.
* Written by Marco Boretto <marco.bore@gmail.com>
*/
namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Entities\ReplyKeyboardMarkup;
use Longman\TelegramBot\Entities\ReplyKeyboardHide;
use Longman\TelegramBot\Entities\ForceReply;
class HidekeyboardCommand extends Command
{
protected $name = 'hidekeyboard';
protected $description = 'Hide the custom keyboard';
protected $usage = '/hidekeyboard';
protected $version = '0.0.5';
protected $enabled = true;
public function execute()
{
$update = $this->getUpdate();
$message = $this->getMessage();
$message_id = $message->getMessageId();
$chat_id = $message->getChat()->getId();
$text = $message->getText(true);
$data = array();
$data['chat_id'] = $chat_id;
$data['text'] = 'Keyboard Hided';
#$data['reply_to_message_id'] = $message_id;
$json = (
new ReplyKeyBoardHide(
array(
'selective' => false
)
)
)->toJSON();
$data['reply_markup'] = $json;
$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.
* written by Marco Boretto <marco.bore@gmail.com>
*/
namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Entities\ReplyKeyboardMarkup;
use Longman\TelegramBot\Entities\ReplyKeyboardHide;
use Longman\TelegramBot\Entities\ForceReply;
class KeyboardCommand extends Command
{
protected $name = 'keyboard';
protected $description = 'Show a custom keybord with reply markup';
protected $usage = '/keyboard';
protected $version = '0.0.5';
protected $enabled = true;
public function execute()
{
$update = $this->getUpdate();
$message = $this->getMessage();
$message_id = $message->getMessageId();
$chat_id = $message->getChat()->getId();
$text = $message->getText(true);
$data = array();
$data['chat_id'] = $chat_id;
$data['text'] = 'Press a Button:';
#$data['reply_to_message_id'] = $message_id;
#Keyboard examples
$keyboards = array();
//0
$keyboard[0] = array('7','8','9');
$keyboard[1] = array('4','5','6');
$keyboard[2] = array('1','2','3');
$keyboard[3] = array(' ','0',' ');
$keyboards[] = $keyboard;
unset($keyboard);
//1
$keyboard[0] = array('7','8','9','+');
$keyboard[1] = array('4','5','6','-');
$keyboard[2] = array('1','2','3','*');
$keyboard[3] = array(' ','0',' ','/');
$keyboards[] = $keyboard;
unset($keyboard);
//2
$keyboard[0] = array('A');
$keyboard[1] = array('B');
$keyboard[2] = array('C');
$keyboards[] = $keyboard;
unset($keyboard);
//3
$keyboard[0] = array('A');
$keyboard[1] = array('B');
$keyboard[2] = array('C','D');
$keyboards[] = $keyboard;
unset($keyboard);
$json = (
new ReplyKeyboardMarkup(
array(
'keyboard' => $keyboards[1] ,
'resize_keyboard' => true,
'one_time_keyboard' => false,
'selective' => false
)
)
)->toJSON();
#echo $json;
$data['reply_markup'] = $json;
$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.
* Written by Marco Boretto <marco.bore@gmail.com>
*/
namespace Longman\TelegramBot\Entities;
use Longman\TelegramBot\Exception\TelegramException;
class ForceReply extends Entity
{
protected $force_reply;
protected $selective;
public function __construct(array $data = null)
{
$this->force_reply = true;
$this->selective = isset($data['selective']) ? $data['selective'] : false;
}
}
...@@ -100,6 +100,26 @@ class Message extends Entity ...@@ -100,6 +100,26 @@ class Message extends Entity
if (!empty($this->reply_to_message)) { if (!empty($this->reply_to_message)) {
$this->reply_to_message = new Message($this->reply_to_message); $this->reply_to_message = new Message($this->reply_to_message);
} }
$this->new_chat_participant = isset($data['new_chat_participant']) ? $data['new_chat_participant'] : null;
if (!empty($this->new_chat_participant)) {
$this->new_chat_participant = new User($this->new_chat_participant);
}
$this->left_chat_participant = isset($data['left_chat_participant']) ? $data['left_chat_participant'] : null;
if (!empty($this->left_chat_participant)) {
$this->left_chat_participant = new User($this->left_chat_participant);
}
$this->new_chat_title = isset($data['new_chat_title']) ? $data['new_chat_title'] : null;
$this->delete_chat_photo = isset($data['delete_chat_photo']) ? $data['delete_chat_photo'] : null;
$this->group_chat_created = isset($data['group_chat_created']) ? $data['group_chat_created'] : null;
} }
//return the entire command like /echo or /echo@bot1 if specified //return the entire command like /echo or /echo@bot1 if specified
...@@ -178,6 +198,36 @@ class Message extends Entity ...@@ -178,6 +198,36 @@ class Message extends Entity
return $this->reply_to_message; return $this->reply_to_message;
} }
public function getNewChatParticipant()
{
return $this->new_chat_participant;
}
public function getLeftChatParticipant()
{
return $this->left_chat_participant;
}
public function getNewChatTitle()
{
return $this->new_chat_title;
}
public function getDeleteChatPhoto()
{
return $this->delete_chat_photo;
}
public function getGroupChatCreated()
{
return $this->group_chat_created;
}
public function getText($without_cmd = false) public function getText($without_cmd = false)
{ {
$text = $this->text; $text = $this->text;
......
<?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.
* Written by Marco Boretto <marco.bore@gmail.com>
*/
namespace Longman\TelegramBot\Entities;
use Longman\TelegramBot\Exception\TelegramException;
class ReplyKeyboardHide extends Entity
{
protected $hide_keyboard;
protected $selective;
public function __construct(array $data = null)
{
$this->hide_keyboard = true;
$this->selective = isset($data['selective']) ? $data['selective'] : false;
}
}
<?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.
* Written by Marco Boretto <marco.bore@gmail.com>
*/
namespace Longman\TelegramBot\Entities;
use Longman\TelegramBot\Exception\TelegramException;
class ReplyKeyboardMarkup extends Entity
{
protected $keyboard;
protected $resize_keyboard;
protected $one_time_keyboard;
protected $selective;
public function __construct($data = array())
{
if (isset($data['keyboard'])) {
if (is_array($data['keyboard'])) {
foreach ($data['keyboard'] as $item) {
if (!is_array($item)) {
throw new TelegramException('Keyboard subfield is not an array!');
}
}
$this->keyboard = $data['keyboard'];
} else {
throw new TelegramException('Keyboard field is not an array!');
}
} else {
throw new TelegramException('Keyboard field is empty!');
}
$this->resize_keyboard = isset($data['resize_keyboard']) ? $data['resize_keyboard'] : false;
$this->one_time_keyboard = isset($data['one_time_keyboard']) ? $data['one_time_keyboard'] : false;
$this->selective = isset($data['selective']) ? $data['selective'] : false;
}
}
...@@ -284,7 +284,7 @@ class Telegram ...@@ -284,7 +284,7 @@ class Telegram
* *
* @return mixed * @return mixed
*/ */
protected function executeCommand($command, Update $update) public function executeCommand($command, Update $update)
{ {
$class = $this->getCommandClass($command, $update); $class = $this->getCommandClass($command, $update);
if (empty($class)) { if (empty($class)) {
......
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