Commit e797f65a authored by MBoretto's avatar MBoretto

[Fix] check if is a command before tock

parent 4eef4d40
<?php <?php
/* /*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com> * (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Longman\TelegramBot\Entities; namespace Longman\TelegramBot\Entities;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
class Message extends Entity class Message extends Entity
{ {
protected $message_id; protected $message_id;
protected $from; protected $from;
protected $date; protected $date;
protected $chat; protected $chat;
protected $forward_from; protected $forward_from;
protected $forward_date; protected $forward_date;
protected $reply_to_message; protected $reply_to_message;
protected $text; protected $text;
protected $audio; protected $audio;
protected $document; protected $document;
protected $photo; protected $photo;
protected $sticker; protected $sticker;
protected $video; protected $video;
protected $contact; protected $contact;
protected $location; protected $location;
protected $new_chat_participant; protected $new_chat_participant;
protected $left_chat_participant; protected $left_chat_participant;
protected $new_chat_title; protected $new_chat_title;
protected $new_chat_photo; protected $new_chat_photo;
protected $delete_chat_photo; protected $delete_chat_photo;
protected $group_chat_created; protected $group_chat_created;
protected $command; protected $command;
private $type; private $type;
public function __construct(array $data, $bot_name) public function __construct(array $data, $bot_name)
{ {
$this->bot_name = $bot_name; $this->bot_name = $bot_name;
$this->type = 'text'; $this->type = 'text';
$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)) {
throw new TelegramException('message_id is empty!'); throw new TelegramException('message_id is empty!');
} }
$this->from = isset($data['from']) ? $data['from'] : null; $this->from = isset($data['from']) ? $data['from'] : null;
if (empty($this->from)) { if (empty($this->from)) {
throw new TelegramException('from is empty!'); throw new TelegramException('from is empty!');
} }
$this->from = new User($this->from); $this->from = new User($this->from);
$this->date = isset($data['date']) ? $data['date'] : null; $this->date = isset($data['date']) ? $data['date'] : null;
if (empty($this->date)) { if (empty($this->date)) {
throw new TelegramException('date is empty!'); throw new TelegramException('date is empty!');
} }
$this->chat = isset($data['chat']) ? $data['chat'] : null; $this->chat = isset($data['chat']) ? $data['chat'] : null;
if (empty($this->chat)) { if (empty($this->chat)) {
throw new TelegramException('chat is empty!'); throw new TelegramException('chat is empty!');
} }
$this->chat = new Chat($this->chat); $this->chat = new Chat($this->chat);
$this->text = isset($data['text']) ? $data['text'] : null; $this->text = isset($data['text']) ? $data['text'] : null;
$command = $this->getCommand(); $command = $this->getCommand();
if (!empty($command)) { if (!empty($command)) {
$this->type = 'command'; $this->type = 'command';
} }
$this->forward_from = isset($data['forward_from']) ? $data['forward_from'] : null; $this->forward_from = isset($data['forward_from']) ? $data['forward_from'] : null;
if (!empty($this->forward_from)) { if (!empty($this->forward_from)) {
$this->forward_from = new User($this->forward_from); $this->forward_from = new User($this->forward_from);
} }
$this->forward_date = isset($data['forward_date']) ? $data['forward_date'] : null; $this->forward_date = isset($data['forward_date']) ? $data['forward_date'] : null;
$this->reply_to_message = isset($data['reply_to_message']) ? $data['reply_to_message'] : null; $this->reply_to_message = isset($data['reply_to_message']) ? $data['reply_to_message'] : null;
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; $this->new_chat_participant = isset($data['new_chat_participant']) ? $data['new_chat_participant'] : null;
if (!empty($this->new_chat_participant)) { if (!empty($this->new_chat_participant)) {
$this->new_chat_participant = new User($this->new_chat_participant); $this->new_chat_participant = new User($this->new_chat_participant);
$this->type = 'new_chat_participant'; $this->type = 'new_chat_participant';
} }
$this->left_chat_participant = isset($data['left_chat_participant']) ? $data['left_chat_participant'] : null; $this->left_chat_participant = isset($data['left_chat_participant']) ? $data['left_chat_participant'] : null;
if (!empty($this->left_chat_participant)) { if (!empty($this->left_chat_participant)) {
$this->left_chat_participant = new User($this->left_chat_participant); $this->left_chat_participant = new User($this->left_chat_participant);
$this->type = 'left_chat_participant'; $this->type = 'left_chat_participant';
} }
$this->new_chat_title = isset($data['new_chat_title']) ? $data['new_chat_title'] : null; $this->new_chat_title = isset($data['new_chat_title']) ? $data['new_chat_title'] : null;
if ($this->new_chat_title) { if ($this->new_chat_title) {
$this->type = 'new_chat_title'; $this->type = 'new_chat_title';
} }
$this->delete_chat_photo = isset($data['delete_chat_photo']) ? $data['delete_chat_photo'] : null; $this->delete_chat_photo = isset($data['delete_chat_photo']) ? $data['delete_chat_photo'] : null;
if ($this->delete_chat_photo) { if ($this->delete_chat_photo) {
$this->type = 'delete_chat_photo'; $this->type = 'delete_chat_photo';
} }
$this->group_chat_created = isset($data['group_chat_created']) ? $data['group_chat_created'] : null; $this->group_chat_created = isset($data['group_chat_created']) ? $data['group_chat_created'] : null;
if ($this->group_chat_created) { if ($this->group_chat_created) {
$this->type = 'group_chat_created'; $this->type = 'group_chat_created';
} }
} }
//return the entire command like /echo or /echo@bot1 if specified //return the entire command like /echo or /echo@bot1 if specified
public function getFullCommand() public function getFullCommand()
{ {
if (substr($this->text, 0, 1) === '/') {
return strtok($this->text, ' '); return strtok($this->text, ' ');
} }else{
return null;
public function getCommand() }
{ }
if (!empty($this->command)) {
return $this->command; public function getCommand()
} {
if (!empty($this->command)) {
$cmd = $this->getFullCommand(); return $this->command;
}
if (substr($cmd, 0, 1) === '/') {
$cmd = substr($cmd, 1); $cmd = $this->getFullCommand();
//check if command is follow by botname if (substr($cmd, 0, 1) === '/') {
$split_cmd = explode('@', $cmd); $cmd = substr($cmd, 1);
if (isset($split_cmd[1])) {
//command is followed by name check if is addressed to me //check if command is follow by botname
if (strtolower($split_cmd[1]) == strtolower($this->bot_name)) { $split_cmd = explode('@', $cmd);
return $this->command = $split_cmd[0]; if (isset($split_cmd[1])) {
} //command is followed by name check if is addressed to me
} else { if (strtolower($split_cmd[1]) == strtolower($this->bot_name)) {
//command is not followed by name return $this->command = $split_cmd[0];
return $this->command = $cmd; }
} } else {
} //command is not followed by name
return $this->command = $cmd;
return false; }
} }
public function getMessageId() return false;
{ }
return $this->message_id; public function getMessageId()
} {
public function getDate() return $this->message_id;
{ }
return $this->date; public function getDate()
} {
public function getFrom() return $this->date;
{ }
return $this->from; public function getFrom()
} {
public function getChat() return $this->from;
{ }
return $this->chat; public function getChat()
} {
public function getForwardFrom() return $this->chat;
{ }
return $this->forward_from; public function getForwardFrom()
} {
public function getForwardDate() return $this->forward_from;
{ }
return $this->forward_date; public function getForwardDate()
} {
public function getReplyToMessage() return $this->forward_date;
{ }
return $this->reply_to_message; public function getReplyToMessage()
} {
public function getNewChatParticipant() return $this->reply_to_message;
{ }
return $this->new_chat_participant; public function getNewChatParticipant()
} {
public function getLeftChatParticipant() return $this->new_chat_participant;
{ }
return $this->left_chat_participant; public function getLeftChatParticipant()
} {
public function getNewChatTitle() return $this->left_chat_participant;
{ }
return $this->new_chat_title; public function getNewChatTitle()
} {
public function getDeleteChatPhoto() return $this->new_chat_title;
{ }
return $this->delete_chat_photo; public function getDeleteChatPhoto()
} {
public function getGroupChatCreated() return $this->delete_chat_photo;
{ }
return $this->group_chat_created; public function getGroupChatCreated()
} {
public function getText($without_cmd = false) return $this->group_chat_created;
{ }
$text = $this->text;
if ($without_cmd) { public function getText($without_cmd = false)
$command = $this->getFullCommand(); {
$text = substr($text, strlen($command . ' '), strlen($text)); $text = $this->text;
} if ($without_cmd) {
$command = $this->getFullCommand();
return $text; if(!empty($command)){
} $text = substr($text, strlen($command . ' '), strlen($text));
}
}
public function botAddedInChat()
{ return $text;
if (!empty($this->new_chat_participant)) { }
if ($this->new_chat_participant->getUsername() == $this->getBotName()) {
return true;
} public function botAddedInChat()
} {
return false; if (!empty($this->new_chat_participant)) {
} if ($this->new_chat_participant->getUsername() == $this->getBotName()) {
return true;
public function getType() }
{ }
return false;
return $this->type; }
}
} public function getType()
{
return $this->type;
}
}
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