Commit 9ccfccd4 authored by LONGMAN's avatar LONGMAN

Added new system commands

parent ffe04b29
...@@ -19,6 +19,7 @@ abstract class Command ...@@ -19,6 +19,7 @@ abstract class Command
protected $message; protected $message;
protected $command; protected $command;
protected $public = false;
protected $description = 'Command help'; protected $description = 'Command help';
protected $usage = 'Command usage'; protected $usage = 'Command usage';
protected $version = '1.0.0'; protected $version = '1.0.0';
...@@ -99,4 +100,9 @@ abstract class Command ...@@ -99,4 +100,9 @@ abstract class Command
{ {
return $this->enabled; return $this->enabled;
} }
public function isPublic()
{
return $this->public;
}
} }
...@@ -23,6 +23,7 @@ class CalcCommand extends Command ...@@ -23,6 +23,7 @@ class CalcCommand extends Command
protected $usage = '/calc <expression>'; protected $usage = '/calc <expression>';
protected $version = '1.0.0'; protected $version = '1.0.0';
protected $enabled = true; protected $enabled = true;
protected $public = true;
public function execute() public function execute()
{ {
......
...@@ -22,6 +22,7 @@ class DateCommand extends Command ...@@ -22,6 +22,7 @@ class DateCommand extends Command
protected $usage = '/date <location>'; protected $usage = '/date <location>';
protected $version = '1.2.0'; protected $version = '1.2.0';
protected $enabled = true; protected $enabled = true;
protected $public = true;
private $base_url = 'https://maps.googleapis.com/maps/api'; private $base_url = 'https://maps.googleapis.com/maps/api';
private $date_format = 'd-m-Y H:i:s'; private $date_format = 'd-m-Y H:i:s';
......
...@@ -21,6 +21,7 @@ class EchoCommand extends Command ...@@ -21,6 +21,7 @@ class EchoCommand extends Command
protected $usage = '/echo <text>'; protected $usage = '/echo <text>';
protected $version = '1.0.0'; protected $version = '1.0.0';
protected $enabled = true; protected $enabled = true;
protected $public = true;
public function execute() public function execute()
{ {
......
...@@ -21,6 +21,7 @@ class HelpCommand extends Command ...@@ -21,6 +21,7 @@ class HelpCommand extends Command
protected $usage = '/help or /help <command>'; protected $usage = '/help or /help <command>';
protected $version = '1.0.0'; protected $version = '1.0.0';
protected $enabled = true; protected $enabled = true;
protected $public = true;
public function execute() public function execute()
{ {
...@@ -34,22 +35,30 @@ class HelpCommand extends Command ...@@ -34,22 +35,30 @@ class HelpCommand extends Command
$commands = $this->telegram->getCommandsList(); $commands = $this->telegram->getCommandsList();
if (empty($text)) { if (empty($text)) {
$msg = 'GeoBot v. ' . $this->telegram->getVersion() . "\n\n"; $msg = 'GeoBot v. ' . $this->telegram->getVersion() . "\n\n";
$msg.= 'Commands List:' . "\n"; $msg .= 'Commands List:' . "\n";
foreach ($commands as $command) { foreach ($commands as $command) {
if (!$command->isEnabled()) { if (!$command->isEnabled()) {
continue; continue;
} }
$msg.= '/' . $command->getName() . ' - ' . $command->getDescription() . "\n"; if (!$command->isPublic()) {
continue;
}
$msg .= '/' . $command->getName() . ' - ' . $command->getDescription() . "\n";
} }
$msg.= "\n" . 'For exact command help type: /help <command>'; $msg .= "\n" . 'For exact command help type: /help <command>';
} else { } else {
$text = str_replace('/', '', $text); $text = str_replace('/', '', $text);
if (isset($commands[$text])) { if (isset($commands[$text])) {
$command = $commands[$text]; $command = $commands[$text];
if (!$command->isEnabled() || !$command->isPublic()) {
$msg = 'Command ' . $text . ' not found';
} else {
$msg = 'Command: ' . $command->getName() . ' v' . $command->getVersion() . "\n"; $msg = 'Command: ' . $command->getName() . ' v' . $command->getVersion() . "\n";
$msg.= 'Description: ' . $command->getDescription() . "\n"; $msg .= 'Description: ' . $command->getDescription() . "\n";
$msg.= 'Usage: ' . $command->getUsage(); $msg .= 'Usage: ' . $command->getUsage();
}
} else { } else {
$msg = 'Command ' . $text . ' not found'; $msg = 'Command ' . $text . ' not found';
} }
......
<?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 LeftChatParticipantCommand extends Command
{
protected $name = 'left_chat_participant';
protected $description = 'Left Chat Participant';
protected $usage = '/';
protected $version = '1.0.0';
protected $enabled = true;
public function execute()
{
$update = $this->getUpdate();
$message = $this->getMessage();
$participant = $message->getLeftChatParticipant();
// 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 NewChatParticipantCommand extends Command
{
protected $name = 'new_chat_participant';
protected $description = 'New Chat Participant';
protected $usage = '/';
protected $version = '1.0.0';
protected $enabled = true;
public function execute()
{
$update = $this->getUpdate();
$message = $this->getMessage();
$participant = $message->getNewChatParticipant();
$chat_id = $message->getChat()->getId();
$data = array();
$data['chat_id'] = $chat_id;
if ($participant->getUsername() == $this->getTelegram()->getBotName()) {
$text = 'Hi there';
} else {
if ($participant->getUsername()) {
$text = 'Hi @'.$participant->getUsername();
} else {
$text = 'Hi '.$participant->getFirstName();
}
}
$data['text'] = $text;
$result = Request::sendMessage($data);
return $result;
}
}
...@@ -21,6 +21,7 @@ class WeatherCommand extends Command ...@@ -21,6 +21,7 @@ class WeatherCommand extends Command
protected $usage = '/weather <location>'; protected $usage = '/weather <location>';
protected $version = '1.0.0'; protected $version = '1.0.0';
protected $enabled = true; protected $enabled = true;
protected $public = true;
private function getWeather($location) private function getWeather($location)
{ {
......
...@@ -24,6 +24,7 @@ class WhoamiCommand extends Command ...@@ -24,6 +24,7 @@ class WhoamiCommand extends Command
protected $usage = '/whoami'; protected $usage = '/whoami';
protected $version = '1.0.0'; protected $version = '1.0.0';
protected $enabled = true; protected $enabled = true;
protected $public = true;
public function execute() public function execute()
{ {
......
...@@ -12,6 +12,16 @@ namespace Longman\TelegramBot\Entities; ...@@ -12,6 +12,16 @@ namespace Longman\TelegramBot\Entities;
class Entity class Entity
{ {
protected $bot_name;
public function getBotName()
{
return $this->bot_name;
}
public function toJSON() public function toJSON()
{ {
......
...@@ -58,12 +58,12 @@ class Message extends Entity ...@@ -58,12 +58,12 @@ class Message extends Entity
protected $command; protected $command;
protected $bot_name; 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->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)) {
...@@ -89,6 +89,11 @@ class Message extends Entity ...@@ -89,6 +89,11 @@ class Message extends Entity
$this->text = isset($data['text']) ? $data['text'] : null; $this->text = isset($data['text']) ? $data['text'] : null;
$command = $this->getCommand();
if (!empty($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);
...@@ -104,19 +109,29 @@ class Message extends Entity ...@@ -104,19 +109,29 @@ class Message extends Entity
$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->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->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) {
$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) {
$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) {
$this->type = 'group_chat_created';
}
...@@ -238,4 +253,21 @@ class Message extends Entity ...@@ -238,4 +253,21 @@ class Message extends Entity
return $text; return $text;
} }
public function botAddedInChat()
{
if (!empty($this->new_chat_participant)) {
if ($this->new_chat_participant->getUsername() == $this->getBotName()) {
return true;
}
}
return false;
}
public function getType()
{
return $this->type;
}
} }
...@@ -17,7 +17,6 @@ class Update extends Entity ...@@ -17,7 +17,6 @@ class Update extends Entity
protected $update_id; protected $update_id;
protected $message; protected $message;
protected $bot_name;
public function __construct(array $data, $bot_name) public function __construct(array $data, $bot_name)
{ {
......
...@@ -30,7 +30,7 @@ class Telegram ...@@ -30,7 +30,7 @@ class Telegram
* *
* @var string * @var string
*/ */
protected $version = '0.0.8'; protected $version = '0.0.10';
/** /**
* Telegram API key * Telegram API key
...@@ -112,6 +112,17 @@ class Telegram ...@@ -112,6 +112,17 @@ class Telegram
/**
* Commands config
*
* @var array
*/
protected $message_types = array('text', 'command', 'new_chat_participant',
'left_chat_participant', 'new_chat_title', 'delete_chat_photo', 'group_chat_created'
);
/** /**
...@@ -273,12 +284,95 @@ class Telegram ...@@ -273,12 +284,95 @@ class Telegram
$this->insertRequest($update); $this->insertRequest($update);
$command = $update->getMessage()->getCommand(); $message = $update->getMessage();
if (!empty($command)) {
// check type
$type = $message->getType();
switch ($type) {
default:
case 'text':
// do nothing
break;
case 'command':
// execute command
$command = $message->getCommand();
return $this->executeCommand($command, $update); return $this->executeCommand($command, $update);
break;
case 'new_chat_participant':
// trigger new participant
$command = 'new_chat_participant';
return $this->executeCommand($command, $update);
break;
case 'left_chat_participant':
// trigger left chat participant
$command = 'left_chat_participant';
return $this->executeCommand($command, $update);
break;
case 'new_chat_title':
// trigger new_chat_title
break;
case 'delete_chat_photo':
// trigger delete_chat_photo
break;
case 'group_chat_created':
// trigger group_chat_created
break;
}
}
public function eventUserAddedToChat(Update $update)
{
$message = $update->getMessage();
$participant = $message->getNewChatParticipant();
if (!empty($participant)) {
$chat_id = $message->getChat()->getId();
$data = array();
$data['chat_id'] = $chat_id;
if ($participant->getUsername() == $this->getBotName()) {
$text = 'Hi there';
} else {
if ($participant->getUsername()) {
$text = 'Hi @'.$participant->getUsername();
} else {
$text = 'Hi '.$participant->getFirstName();
}
}
$data['text'] = $text;
$result = Request::sendMessage($data);
return $result;
} }
} }
/** /**
* Execute /command * Execute /command
* *
...@@ -291,6 +385,11 @@ class Telegram ...@@ -291,6 +385,11 @@ class Telegram
return false; return false;
} }
if (!$class->isEnabled()) {
return false;
}
return $class->execute(); return $class->execute();
} }
...@@ -303,12 +402,18 @@ class Telegram ...@@ -303,12 +402,18 @@ class Telegram
{ {
$this->commands_dir = array_unique($this->commands_dir); $this->commands_dir = array_unique($this->commands_dir);
$this->commands_dir = array_reverse($this->commands_dir); $this->commands_dir = array_reverse($this->commands_dir);
$command = $this->sanitizeCommand($command);
$class_name = ucfirst($command) . 'Command'; $class_name = ucfirst($command) . 'Command';
foreach ($this->commands_dir as $dir) { foreach ($this->commands_dir as $dir) {
if (is_file($dir . '/' . $class_name . '.php')) { if (is_file($dir . '/' . $class_name . '.php')) {
require_once($dir . '/' . $class_name . '.php'); require_once($dir . '/' . $class_name . '.php');
if (!class_exists($class_name)) {
continue;
}
$class = new $class_name($this); $class = new $class_name($this);
if (!empty($update)) { if (!empty($update)) {
$class->setUpdate($update); $class->setUpdate($update);
} }
...@@ -318,10 +423,7 @@ class Telegram ...@@ -318,10 +423,7 @@ class Telegram
} }
$class_name = __NAMESPACE__ . '\\Commands\\' . $class_name; $class_name = __NAMESPACE__ . '\\Commands\\' . $class_name;
if (!class_exists($class_name)) { if (class_exists($class_name)) {
return false;
}
$class = new $class_name($this); $class = new $class_name($this);
if (!empty($update)) { if (!empty($update)) {
$class->setUpdate($update); $class->setUpdate($update);
...@@ -330,10 +432,22 @@ class Telegram ...@@ -330,10 +432,22 @@ class Telegram
if (is_object($class)) { if (is_object($class)) {
return $class; return $class;
} }
}
return false; return false;
} }
protected function sanitizeCommand($string, $capitalizeFirstCharacter = false)
{
$str = str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
//$str[0] = strtolower($str[0]);
return $str;
}
/** /**
* Insert request in db * Insert request in db
* *
...@@ -345,19 +459,47 @@ class Telegram ...@@ -345,19 +459,47 @@ class Telegram
return false; return false;
} }
$message = $update->getMessage();
try { try {
$sth = $this->pdo->prepare('INSERT INTO `messages` $sth1 = $this->pdo->prepare('INSERT INTO `users`
( (
`update_id`, `message_id`, `from`, `date`, `chat`, `forward_from`, `id`, `username`, `first_name`, `last_name`
`forward_date`, `reply_to_message`, `text`
) )
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)'); VALUES (:id, :username, :first_name, :last_name)
ON DUPLICATE KEY UPDATE `username`=:username, `first_name`=:first_name, `last_name`=:last_name
');
$from = $message->getFrom();
$user_id = $from->getId();
$username = $from->getUsername();
$first_name = $from->getFirstName();
$last_name = $from->getLastName();
$message = $update->getMessage();
$sth1->bindParam(':id', $user_id, \PDO::PARAM_INT);
$sth1->bindParam(':username', $username, \PDO::PARAM_STR, 255);
$sth1->bindParam(':first_name', $first_name, \PDO::PARAM_STR, 255);
$sth1->bindParam(':last_name', $last_name, \PDO::PARAM_STR, 255);
$status = $sth1->execute();
$sth = $this->pdo->prepare('INSERT IGNORE INTO `messages`
(
`update_id`, `message_id`, `user_id`, `date`, `chat`, `forward_from`,
`forward_date`, `reply_to_message`, `text`
)
VALUES (:update_id, :message_id, :user_id,
:date, :chat, :forward_from,
:forward_date, :reply_to_message, :text)');
$update_id = $update->getUpdateId(); $update_id = $update->getUpdateId();
$message_id = $message->getMessageId(); $message_id = $message->getMessageId();
$from = $message->getFrom()->toJSON(); $from = $message->getFrom()->toJSON();
$user_id = $message->getFrom()->getId();
$date = $message->getDate(); $date = $message->getDate();
$chat = $message->getChat()->toJSON(); $chat = $message->getChat()->toJSON();
$forward_from = $message->getForwardFrom(); $forward_from = $message->getForwardFrom();
...@@ -368,15 +510,15 @@ class Telegram ...@@ -368,15 +510,15 @@ class Telegram
} }
$text = $message->getText(); $text = $message->getText();
$sth->bindParam(1, $update_id, \PDO::PARAM_INT); $sth->bindParam(':update_id', $update_id, \PDO::PARAM_INT);
$sth->bindParam(2, $message_id, \PDO::PARAM_INT); $sth->bindParam(':message_id', $message_id, \PDO::PARAM_INT);
$sth->bindParam(3, $from, \PDO::PARAM_STR, 255); $sth->bindParam(':user_id', $user_id, \PDO::PARAM_INT);
$sth->bindParam(4, $date, \PDO::PARAM_INT); $sth->bindParam(':date', $date, \PDO::PARAM_INT);
$sth->bindParam(5, $chat, \PDO::PARAM_STR); $sth->bindParam(':chat', $chat, \PDO::PARAM_STR);
$sth->bindParam(6, $forward_from, \PDO::PARAM_STR); $sth->bindParam(':forward_from', $forward_from, \PDO::PARAM_STR);
$sth->bindParam(7, $forward_date, \PDO::PARAM_INT); $sth->bindParam(':forward_date', $forward_date, \PDO::PARAM_INT);
$sth->bindParam(8, $reply_to_message, \PDO::PARAM_STR); $sth->bindParam(':reply_to_message', $reply_to_message, \PDO::PARAM_STR);
$sth->bindParam(9, $text, \PDO::PARAM_STR); $sth->bindParam(':text', $text, \PDO::PARAM_STR);
$status = $sth->execute(); $status = $sth->execute();
...@@ -387,6 +529,8 @@ class Telegram ...@@ -387,6 +529,8 @@ class Telegram
return true; return true;
} }
/** /**
* Add custom commands path * Add custom commands path
* *
...@@ -501,4 +645,14 @@ class Telegram ...@@ -501,4 +645,14 @@ class Telegram
return $this; return $this;
} }
/**
* Get available message types
*
* @return array
*/
public function getMessageTypes()
{
return $this->message_types;
}
} }
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