Commit 84cd8f50 authored by Avtandil Kikabidze's avatar Avtandil Kikabidze

Improved code and doc-blocks in admin and user commands

parent e986a12d
...@@ -10,9 +10,6 @@ ...@@ -10,9 +10,6 @@
namespace Longman\TelegramBot\Commands; namespace Longman\TelegramBot\Commands;
/**
* Abstract Admin Command Class
*/
abstract class AdminCommand extends Command abstract class AdminCommand extends Command
{ {
......
...@@ -15,30 +15,44 @@ use Longman\TelegramBot\DB; ...@@ -15,30 +15,44 @@ use Longman\TelegramBot\DB;
use Longman\TelegramBot\Entities\Chat; use Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Request; use Longman\TelegramBot\Request;
/**
* Admin "/chats" command
*/
class ChatsCommand extends AdminCommand class ChatsCommand extends AdminCommand
{ {
/**#@+ /**
* {@inheritdoc} * @var string
*/ */
protected $name = 'chats'; protected $name = 'chats';
/**
* @var string
*/
protected $description = 'List or search all chats stored by the bot'; protected $description = 'List or search all chats stored by the bot';
/**
* @var string
*/
protected $usage = '/chats, /chats * or /chats <search string>'; protected $usage = '/chats, /chats * or /chats <search string>';
/**
* @var string
*/
protected $version = '1.0.2'; protected $version = '1.0.2';
/**
* @var bool
*/
protected $need_mysql = true; protected $need_mysql = true;
/**#@-*/
/** /**
* {@inheritdoc} * Command execute method
*
* @return mixed
*/ */
public function execute() public function execute()
{ {
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$text = trim($message->getText(true)); $text = trim($message->getText(true));
$results = DB::selectChats( $results = DB::selectChats(
true, //Select groups (group chat) true, //Select groups (group chat)
...@@ -50,8 +64,8 @@ class ChatsCommand extends AdminCommand ...@@ -50,8 +64,8 @@ class ChatsCommand extends AdminCommand
($text === '' || $text == '*') ? null : $text //Text to search in user/group name ($text === '' || $text == '*') ? null : $text //Text to search in user/group name
); );
$user_chats = 0; $user_chats = 0;
$group_chats = 0; $group_chats = 0;
$super_group_chats = 0; $super_group_chats = 0;
if ($text === '') { if ($text === '') {
...@@ -65,11 +79,12 @@ class ChatsCommand extends AdminCommand ...@@ -65,11 +79,12 @@ class ChatsCommand extends AdminCommand
foreach ($results as $result) { foreach ($results as $result) {
//Initialize a chat object //Initialize a chat object
$result['id'] = $result['chat_id']; $result['id'] = $result['chat_id'];
$chat = new Chat($result); $chat = new Chat($result);
$whois = $chat->getId(); $whois = $chat->getId();
if ($this->telegram->getCommandObject('whois')) { if ($this->telegram->getCommandObject('whois')) {
$whois = '/whois' . str_replace('-', 'g', $chat->getId()); //We can't use '-' in command because part of it will become unclickable $whois = '/whois' . str_replace('-', 'g',
$chat->getId()); //We can't use '-' in command because part of it will become unclickable
} }
if ($chat->isPrivateChat()) { if ($chat->isPrivateChat()) {
...@@ -102,7 +117,7 @@ class ChatsCommand extends AdminCommand ...@@ -102,7 +117,7 @@ class ChatsCommand extends AdminCommand
$text_back .= "\n" . 'Total: ' . ($user_chats + $group_chats + $super_group_chats); $text_back .= "\n" . 'Total: ' . ($user_chats + $group_chats + $super_group_chats);
if ($text === '') { if ($text === '') {
$text_back .= "\n\n" . 'List all chats: /' . $this->name .' *' . "\n" . 'Search for chats: /' . $this->name .' <search string>'; $text_back .= "\n\n" . 'List all chats: /' . $this->name . ' *' . "\n" . 'Search for chats: /' . $this->name . ' <search string>';
} }
} }
......
...@@ -18,15 +18,30 @@ use Longman\TelegramBot\Request; ...@@ -18,15 +18,30 @@ use Longman\TelegramBot\Request;
*/ */
class SendtoallCommand extends AdminCommand class SendtoallCommand extends AdminCommand
{ {
/**#@+ /**
* {@inheritdoc} * @var string
*/ */
protected $name = 'sendtoall'; protected $name = 'sendtoall';
/**
* @var string
*/
protected $description = 'Send the message to all the user\'s bot'; protected $description = 'Send the message to all the user\'s bot';
/**
* @var string
*/
protected $usage = '/sendtoall <message to send>'; protected $usage = '/sendtoall <message to send>';
/**
* @var string
*/
protected $version = '1.2.1'; protected $version = '1.2.1';
/**
* @var bool
*/
protected $need_mysql = true; protected $need_mysql = true;
/**#@-*/
/** /**
* Execute command * Execute command
...@@ -38,7 +53,7 @@ class SendtoallCommand extends AdminCommand ...@@ -38,7 +53,7 @@ class SendtoallCommand extends AdminCommand
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$text = $message->getText(true); $text = $message->getText(true);
if ($text === '') { if ($text === '') {
$text = 'Write the message to send: /sendtoall <message>'; $text = 'Write the message to send: /sendtoall <message>';
...@@ -53,18 +68,18 @@ class SendtoallCommand extends AdminCommand ...@@ -53,18 +68,18 @@ class SendtoallCommand extends AdminCommand
null //'yyyy-mm-dd hh:mm:ss' date range to null //'yyyy-mm-dd hh:mm:ss' date range to
); );
$tot = 0; $tot = 0;
$fail = 0; $fail = 0;
$text = 'Message sent to:' . "\n"; $text = 'Message sent to:' . "\n";
foreach ($results as $result) { foreach ($results as $result) {
$status = ''; $status = '';
$type = ''; $type = '';
if ($result->isOk()) { if ($result->isOk()) {
$status = '✔️'; $status = '✔️';
$ServerResponse = $result->getResult(); $ServerResponse = $result->getResult();
$chat = $ServerResponse->getChat(); $chat = $ServerResponse->getChat();
if ($chat->isPrivateChat()) { if ($chat->isPrivateChat()) {
$name = $chat->getFirstName(); $name = $chat->getFirstName();
$type = 'user'; $type = 'user';
......
...@@ -22,18 +22,35 @@ use Longman\TelegramBot\Request; ...@@ -22,18 +22,35 @@ use Longman\TelegramBot\Request;
*/ */
class WhoisCommand extends AdminCommand class WhoisCommand extends AdminCommand
{ {
/**#@+ /**
* {@inheritdoc} * @var string
*/ */
protected $name = 'whois'; protected $name = 'whois';
/**
* @var string
*/
protected $description = 'Lookup user or group info'; protected $description = 'Lookup user or group info';
/**
* @var string
*/
protected $usage = '/whois <id> or /whois <search string>'; protected $usage = '/whois <id> or /whois <search string>';
/**
* @var string
*/
protected $version = '1.1.0'; protected $version = '1.1.0';
/**
* @var bool
*/
protected $need_mysql = true; protected $need_mysql = true;
/**#@-*/
/** /**
* {@inheritdoc} * Command execute method
*
* @return mixed
*/ */
public function execute() public function execute()
{ {
...@@ -41,9 +58,9 @@ class WhoisCommand extends AdminCommand ...@@ -41,9 +58,9 @@ class WhoisCommand extends AdminCommand
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$command = $message->getCommand(); $command = $message->getCommand();
$text = trim($message->getText(true)); $text = trim($message->getText(true));
$data = [ 'chat_id' => $chat_id ]; $data = ['chat_id' => $chat_id];
//No point in replying to messages in private chats //No point in replying to messages in private chats
if (!$message->getChat()->isPrivateChat()) { if (!$message->getChat()->isPrivateChat()) {
...@@ -93,12 +110,12 @@ class WhoisCommand extends AdminCommand ...@@ -93,12 +110,12 @@ class WhoisCommand extends AdminCommand
if (is_array($result)) { if (is_array($result)) {
$result['id'] = $result['chat_id']; $result['id'] = $result['chat_id'];
$chat = new Chat($result); $chat = new Chat($result);
$user_id = $result['id']; $user_id = $result['id'];
$created_at = $result['chat_created_at']; $created_at = $result['chat_created_at'];
$updated_at = $result['chat_updated_at']; $updated_at = $result['chat_updated_at'];
$old_id = $result['old_id']; $old_id = $result['old_id'];
} }
if ($chat != null) { if ($chat != null) {
...@@ -114,33 +131,33 @@ class WhoisCommand extends AdminCommand ...@@ -114,33 +131,33 @@ class WhoisCommand extends AdminCommand
$text .= 'Last activity: ' . $updated_at . "\n"; $text .= 'Last activity: ' . $updated_at . "\n";
//Code from Whoami command //Code from Whoami command
$limit = 10; $limit = 10;
$offset = null; $offset = null;
$ServerResponse = Request::getUserProfilePhotos([ $ServerResponse = Request::getUserProfilePhotos([
'user_id' => $user_id , 'user_id' => $user_id,
'limit' => $limit, 'limit' => $limit,
'offset' => $offset, 'offset' => $offset,
]); ]);
if ($ServerResponse->isOk()) { if ($ServerResponse->isOk()) {
$UserProfilePhoto = $ServerResponse->getResult(); $UserProfilePhoto = $ServerResponse->getResult();
$totalcount = $UserProfilePhoto->getTotalCount(); $totalcount = $UserProfilePhoto->getTotalCount();
} else { } else {
$totalcount = 0; $totalcount = 0;
} }
if ($totalcount > 0) { if ($totalcount > 0) {
$photos = $UserProfilePhoto->getPhotos(); $photos = $UserProfilePhoto->getPhotos();
$photo = $photos[0][2]; $photo = $photos[0][2];
$file_id = $photo->getFileId(); $file_id = $photo->getFileId();
$data['photo'] = $file_id; $data['photo'] = $file_id;
$data['caption'] = $text; $data['caption'] = $text;
return Request::sendPhoto($data); return Request::sendPhoto($data);
} }
} elseif ($chat->isGroupChat()) { } elseif ($chat->isGroupChat()) {
$text = 'Chat ID: ' . $user_id . (!empty($old_id) ? ' (previously: '.$old_id.')' : ''). "\n"; $text = 'Chat ID: ' . $user_id . (!empty($old_id) ? ' (previously: ' . $old_id . ')' : '') . "\n";
$text .= 'Type: ' . ucfirst($chat->getType()) . "\n"; $text .= 'Type: ' . ucfirst($chat->getType()) . "\n";
$text .= 'Title: ' . $chat->getTitle() . "\n"; $text .= 'Title: ' . $chat->getTitle() . "\n";
$text .= 'First time added to group: ' . $created_at . "\n"; $text .= 'First time added to group: ' . $created_at . "\n";
......
...@@ -15,9 +15,6 @@ use Longman\TelegramBot\Request; ...@@ -15,9 +15,6 @@ use Longman\TelegramBot\Request;
use Longman\TelegramBot\Telegram; use Longman\TelegramBot\Telegram;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Entities\Update;
/**
* Abstract Command Class
*/
abstract class Command abstract class Command
{ {
/** /**
...@@ -93,7 +90,7 @@ abstract class Command ...@@ -93,7 +90,7 @@ abstract class Command
/** /**
* Constructor * Constructor
* *
* @param Telegram $telegram * @param Telegram $telegram
* @param \Longman\TelegramBot\Entities\Update $update * @param \Longman\TelegramBot\Entities\Update $update
*/ */
public function __construct(Telegram $telegram, Update $update = null) public function __construct(Telegram $telegram, Update $update = null)
...@@ -112,7 +109,7 @@ abstract class Command ...@@ -112,7 +109,7 @@ abstract class Command
public function setUpdate(Update $update = null) public function setUpdate(Update $update = null)
{ {
if (!empty($update)) { if (!empty($update)) {
$this->update = $update; $this->update = $update;
$this->message = $this->update->getMessage(); $this->message = $this->update->getMessage();
} }
return $this; return $this;
......
...@@ -12,9 +12,6 @@ namespace Longman\TelegramBot\Commands; ...@@ -12,9 +12,6 @@ namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request; use Longman\TelegramBot\Request;
/**
* Abstract System Command Class
*/
abstract class SystemCommand extends Command abstract class SystemCommand extends Command
{ {
/** /**
...@@ -23,7 +20,7 @@ abstract class SystemCommand extends Command ...@@ -23,7 +20,7 @@ abstract class SystemCommand extends Command
* Although system commands should just work and return a successful ServerResponse, * Although system commands should just work and return a successful ServerResponse,
* each system command can override this method to add custom functionality. * each system command can override this method to add custom functionality.
* *
* @return Entities\ServerResponse * @return \Longman\TelegramBot\Entities\ServerResponse
*/ */
public function execute() public function execute()
{ {
......
...@@ -10,9 +10,6 @@ ...@@ -10,9 +10,6 @@
namespace Longman\TelegramBot\Commands; namespace Longman\TelegramBot\Commands;
/**
* Abstract User Command Class
*/
abstract class UserCommand extends Command abstract class UserCommand extends Command
{ {
......
...@@ -24,18 +24,35 @@ use Longman\TelegramBot\Request; ...@@ -24,18 +24,35 @@ use Longman\TelegramBot\Request;
*/ */
class CancelCommand extends UserCommand class CancelCommand extends UserCommand
{ {
/**#@+ /**
* {@inheritdoc} * @var string
*/ */
protected $name = 'cancel'; protected $name = 'cancel';
/**
* @var string
*/
protected $description = 'Cancel the currently active conversation'; protected $description = 'Cancel the currently active conversation';
/**
* @var string
*/
protected $usage = '/cancel'; protected $usage = '/cancel';
/**
* @var string
*/
protected $version = '0.1.1'; protected $version = '0.1.1';
/**
* @var bool
*/
protected $need_mysql = true; protected $need_mysql = true;
/**#@-*/
/** /**
* {@inheritdoc} * Command execute method
*
* @return \Longman\TelegramBot\Entities\ServerResponse
*/ */
public function execute() public function execute()
{ {
...@@ -56,7 +73,9 @@ class CancelCommand extends UserCommand ...@@ -56,7 +73,9 @@ class CancelCommand extends UserCommand
} }
/** /**
* {@inheritdoc} * Execute no db
*
* @return \Longman\TelegramBot\Entities\ServerResponse
*/ */
public function executeNoDb() public function executeNoDb()
{ {
...@@ -72,10 +91,12 @@ class CancelCommand extends UserCommand ...@@ -72,10 +91,12 @@ class CancelCommand extends UserCommand
*/ */
private function hideKeyboard($text) private function hideKeyboard($text)
{ {
return Request::sendMessage([ return Request::sendMessage(
'reply_markup' => new ReplyKeyboardHide(['selective' => true]), [
'chat_id' => $this->getMessage()->getChat()->getId(), 'reply_markup' => new ReplyKeyboardHide(['selective' => true]),
'text' => $text, 'chat_id' => $this->getMessage()->getChat()->getId(),
]); 'text' => $text,
]
);
} }
} }
...@@ -21,14 +21,25 @@ use Longman\TelegramBot\Request; ...@@ -21,14 +21,25 @@ use Longman\TelegramBot\Request;
*/ */
class DateCommand extends UserCommand class DateCommand extends UserCommand
{ {
/**#@+ /**
* {@inheritdoc} * @var string
*/ */
protected $name = 'date'; protected $name = 'date';
/**
* @var string
*/
protected $description = 'Show date/time by location'; protected $description = 'Show date/time by location';
/**
* @var string
*/
protected $usage = '/date <location>'; protected $usage = '/date <location>';
/**
* @var string
*/
protected $version = '1.3.0'; protected $version = '1.3.0';
/**#@-*/
/** /**
* Guzzle Client object * Guzzle Client object
...@@ -64,10 +75,11 @@ class DateCommand extends UserCommand ...@@ -64,10 +75,11 @@ class DateCommand extends UserCommand
* @param string $location * @param string $location
* *
* @return array|boolean * @return array|boolean
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
private function getCoordinates($location) private function getCoordinates($location)
{ {
$path = 'geocode/json'; $path = 'geocode/json';
$query = ['address' => urlencode($location)]; $query = ['address' => urlencode($location)];
if ($this->google_api_key !== null) { if ($this->google_api_key !== null) {
...@@ -85,10 +97,10 @@ class DateCommand extends UserCommand ...@@ -85,10 +97,10 @@ class DateCommand extends UserCommand
} }
$result = $result['results'][0]; $result = $result['results'][0];
$lat = $result['geometry']['location']['lat']; $lat = $result['geometry']['location']['lat'];
$lng = $result['geometry']['location']['lng']; $lng = $result['geometry']['location']['lng'];
$acc = $result['geometry']['location_type']; $acc = $result['geometry']['location_type'];
$types = $result['types']; $types = $result['types'];
return [$lat, $lng, $acc, $types]; return [$lat, $lng, $acc, $types];
} }
...@@ -100,12 +112,13 @@ class DateCommand extends UserCommand ...@@ -100,12 +112,13 @@ class DateCommand extends UserCommand
* @param string $lng * @param string $lng
* *
* @return array|boolean * @return array|boolean
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
private function getDate($lat, $lng) private function getDate($lat, $lng)
{ {
$path = 'timezone/json'; $path = 'timezone/json';
$date_utc = new \DateTime(null, new \DateTimeZone('UTC')); $date_utc = new \DateTime(null, new \DateTimeZone('UTC'));
$timestamp = $date_utc->format('U'); $timestamp = $date_utc->format('U');
$query = [ $query = [
...@@ -184,7 +197,9 @@ class DateCommand extends UserCommand ...@@ -184,7 +197,9 @@ class DateCommand extends UserCommand
} }
/** /**
* {@inheritdoc} * Command execute method
*
* @return mixed
*/ */
public function execute() public function execute()
{ {
...@@ -196,7 +211,7 @@ class DateCommand extends UserCommand ...@@ -196,7 +211,7 @@ class DateCommand extends UserCommand
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$location = $message->getText(true); $location = $message->getText(true);
if (empty($location)) { if (empty($location)) {
......
...@@ -18,23 +18,36 @@ use Longman\TelegramBot\Request; ...@@ -18,23 +18,36 @@ use Longman\TelegramBot\Request;
*/ */
class EchoCommand extends UserCommand class EchoCommand extends UserCommand
{ {
/**#@+ /**
* {@inheritdoc} * @var string
*/ */
protected $name = 'echo'; protected $name = 'echo';
/**
* @var string
*/
protected $description = 'Show text'; protected $description = 'Show text';
/**
* @var string
*/
protected $usage = '/echo <text>'; protected $usage = '/echo <text>';
/**
* @var string
*/
protected $version = '1.0.1'; protected $version = '1.0.1';
/**#@-*/
/** /**
* {@inheritdoc} * Command execute method
*
* @return mixed
*/ */
public function execute() public function execute()
{ {
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$text = trim($message->getText(true)); $text = trim($message->getText(true));
if ($text === '') { if ($text === '') {
$text = 'Command usage: ' . $this->getUsage(); $text = 'Command usage: ' . $this->getUsage();
......
...@@ -18,17 +18,30 @@ use Longman\TelegramBot\Request; ...@@ -18,17 +18,30 @@ use Longman\TelegramBot\Request;
*/ */
class HelpCommand extends UserCommand class HelpCommand extends UserCommand
{ {
/**#@+ /**
* {@inheritdoc} * @var string
*/ */
protected $name = 'help'; protected $name = 'help';
/**
* @var string
*/
protected $description = 'Show bot commands help'; protected $description = 'Show bot commands help';
/**
* @var string
*/
protected $usage = '/help or /help <command>'; protected $usage = '/help or /help <command>';
/**
* @var string
*/
protected $version = '1.0.1'; protected $version = '1.0.1';
/**#@-*/
/** /**
* {@inheritdoc} * Command execute method
*
* @return mixed
*/ */
public function execute() public function execute()
{ {
...@@ -36,7 +49,7 @@ class HelpCommand extends UserCommand ...@@ -36,7 +49,7 @@ class HelpCommand extends UserCommand
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId(); $message_id = $message->getMessageId();
$command = trim($message->getText(true)); $command = trim($message->getText(true));
//Only get enabled Admin and User commands //Only get enabled Admin and User commands
$commands = array_filter($this->telegram->getCommandsList(), function ($command) { $commands = array_filter($this->telegram->getCommandsList(), function ($command) {
...@@ -56,7 +69,7 @@ class HelpCommand extends UserCommand ...@@ -56,7 +69,7 @@ class HelpCommand extends UserCommand
$command = str_replace('/', '', $command); $command = str_replace('/', '', $command);
if (isset($commands[$command])) { if (isset($commands[$command])) {
$command = $commands[$command]; $command = $commands[$command];
$text = 'Command: ' . $command->getName() . ' v' . $command->getVersion() . "\n"; $text = 'Command: ' . $command->getName() . ' v' . $command->getVersion() . "\n";
$text .= 'Description: ' . $command->getDescription() . "\n"; $text .= 'Description: ' . $command->getDescription() . "\n";
$text .= 'Usage: ' . $command->getUsage(); $text .= 'Usage: ' . $command->getUsage();
} else { } else {
......
...@@ -18,25 +18,38 @@ use Longman\TelegramBot\Request; ...@@ -18,25 +18,38 @@ use Longman\TelegramBot\Request;
*/ */
class SlapCommand extends UserCommand class SlapCommand extends UserCommand
{ {
/**#@+ /**
* {@inheritdoc} * @var string
*/ */
protected $name = 'slap'; protected $name = 'slap';
/**
* @var string
*/
protected $description = 'Slap someone with their username'; protected $description = 'Slap someone with their username';
/**
* @var string
*/
protected $usage = '/slap <@user>'; protected $usage = '/slap <@user>';
/**
* @var string
*/
protected $version = '1.0.1'; protected $version = '1.0.1';
/**#@-*/
/** /**
* {@inheritdoc} * Command execute method
*
* @return mixed
*/ */
public function execute() public function execute()
{ {
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId(); $message_id = $message->getMessageId();
$text = $message->getText(true); $text = $message->getText(true);
$sender = '@' . $message->getFrom()->getUsername(); $sender = '@' . $message->getFrom()->getUsername();
......
...@@ -22,15 +22,30 @@ use Longman\TelegramBot\Entities\ReplyKeyboardMarkup; ...@@ -22,15 +22,30 @@ use Longman\TelegramBot\Entities\ReplyKeyboardMarkup;
*/ */
class SurveyCommand extends UserCommand class SurveyCommand extends UserCommand
{ {
/**#@+ /**
* {@inheritdoc} * @var string
*/ */
protected $name = 'survey'; protected $name = 'survey';
/**
* @var string
*/
protected $description = 'Survery for bot users'; protected $description = 'Survery for bot users';
/**
* @var string
*/
protected $usage = '/survey'; protected $usage = '/survey';
/**
* @var string
*/
protected $version = '0.2.0'; protected $version = '0.2.0';
/**
* @var bool
*/
protected $need_mysql = true; protected $need_mysql = true;
/**#@-*/
/** /**
* Conversation Object * Conversation Object
...@@ -40,7 +55,9 @@ class SurveyCommand extends UserCommand ...@@ -40,7 +55,9 @@ class SurveyCommand extends UserCommand
protected $conversation; protected $conversation;
/** /**
* {@inheritdoc} * Command execute method
*
* @return mixed
*/ */
public function execute() public function execute()
{ {
...@@ -58,7 +75,7 @@ class SurveyCommand extends UserCommand ...@@ -58,7 +75,7 @@ class SurveyCommand extends UserCommand
if ($chat->isGroupChat() || $chat->isSuperGroup()) { if ($chat->isGroupChat() || $chat->isSuperGroup()) {
//reply to message id is applied by default //reply to message id is applied by default
//Force reply is applied by default to so can work with privacy on //Force reply is applied by default to so can work with privacy on
$data['reply_markup'] = new ForceReply([ 'selective' => true]); $data['reply_markup'] = new ForceReply(['selective' => true]);
} }
$data['chat_id'] = $chat_id; $data['chat_id'] = $chat_id;
...@@ -80,29 +97,29 @@ class SurveyCommand extends UserCommand ...@@ -80,29 +97,29 @@ class SurveyCommand extends UserCommand
if (empty($text)) { if (empty($text)) {
$this->conversation->notes['state'] = 0; $this->conversation->notes['state'] = 0;
$this->conversation->update(); $this->conversation->update();
$data['text'] = 'Type your name:'; $data['text'] = 'Type your name:';
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]);
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break; break;
} }
$this->conversation->notes['name'] = $text; $this->conversation->notes['name'] = $text;
$text = ''; $text = '';
// no break // no break
case 1: case 1:
if (empty($text)) { if (empty($text)) {
$this->conversation->notes['state'] = 1; $this->conversation->notes['state'] = 1;
$this->conversation->update(); $this->conversation->update();
$data['text'] = 'Type your surname:'; $data['text'] = 'Type your surname:';
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break; break;
} }
$this->conversation->notes['surname'] = $text; $this->conversation->notes['surname'] = $text;
++$state; ++$state;
$text = ''; $text = '';
// no break // no break
case 2: case 2:
if (empty($text) || !is_numeric($text)) { if (empty($text) || !is_numeric($text)) {
$this->conversation->notes['state'] = 2; $this->conversation->notes['state'] = 2;
...@@ -116,25 +133,25 @@ class SurveyCommand extends UserCommand ...@@ -116,25 +133,25 @@ class SurveyCommand extends UserCommand
break; break;
} }
$this->conversation->notes['age'] = $text; $this->conversation->notes['age'] = $text;
$text = ''; $text = '';
// no break // no break
case 3: case 3:
if (empty($text) || !($text == 'M' || $text == 'F')) { if (empty($text) || !($text == 'M' || $text == 'F')) {
$this->conversation->notes['state'] = 3; $this->conversation->notes['state'] = 3;
$this->conversation->update(); $this->conversation->update();
$keyboard = [['M','F']]; $keyboard = [['M', 'F']];
$reply_keyboard_markup = new ReplyKeyboardMarkup( $reply_keyboard_markup = new ReplyKeyboardMarkup(
[ [
'keyboard' => $keyboard , 'keyboard' => $keyboard,
'resize_keyboard' => true, 'resize_keyboard' => true,
'one_time_keyboard' => true, 'one_time_keyboard' => true,
'selective' => true 'selective' => true
] ]
); );
$data['reply_markup'] = $reply_keyboard_markup; $data['reply_markup'] = $reply_keyboard_markup;
$data['text'] = 'Select your gender:'; $data['text'] = 'Select your gender:';
if (!empty($text) && !($text == 'M' || $text == 'F')) { if (!empty($text) && !($text == 'M' || $text == 'F')) {
$data['text'] = 'Select your gender, choose a keyboard option:'; $data['text'] = 'Select your gender, choose a keyboard option:';
} }
...@@ -142,74 +159,88 @@ class SurveyCommand extends UserCommand ...@@ -142,74 +159,88 @@ class SurveyCommand extends UserCommand
break; break;
} }
$this->conversation->notes['gender'] = $text; $this->conversation->notes['gender'] = $text;
$text = ''; $text = '';
// no break // no break
case 4: case 4:
if (is_null($message->getLocation())) { if (is_null($message->getLocation())) {
$this->conversation->notes['state'] = 4; $this->conversation->notes['state'] = 4;
$this->conversation->update(); $this->conversation->update();
$data['reply_markup'] = new ReplyKeyboardMarkup([ $data['reply_markup'] = new ReplyKeyboardMarkup(
'keyboard' => [[ [
[ 'text' => 'Share Location', 'request_location' => true ], 'keyboard' => [
]], [
'resize_keyboard' => true, [
'one_time_keyboard' => true, 'text' => 'Share Location',
'selective' => true, 'request_location' => true
]); ],
$data['text'] = 'Share your location:'; ]
$result = Request::sendMessage($data); ],
'resize_keyboard' => true,
'one_time_keyboard' => true,
'selective' => true,
]
);
$data['text'] = 'Share your location:';
$result = Request::sendMessage($data);
break; break;
} }
$this->conversation->notes['longitude'] = $message->getLocation()->getLongitude(); $this->conversation->notes['longitude'] = $message->getLocation()->getLongitude();
$this->conversation->notes['latitude'] = $message->getLocation()->getLatitude(); $this->conversation->notes['latitude'] = $message->getLocation()->getLatitude();
// no break // no break
case 5: case 5:
if (is_null($message->getPhoto())) { if (is_null($message->getPhoto())) {
$this->conversation->notes['state'] = 5; $this->conversation->notes['state'] = 5;
$this->conversation->update(); $this->conversation->update();
$data['text'] = 'Insert your picture:'; $data['text'] = 'Insert your picture:';
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]);
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
break; break;
} }
$this->conversation->notes['photo_id'] = $message->getPhoto()[0]->getFileId(); $this->conversation->notes['photo_id'] = $message->getPhoto()[0]->getFileId();
// no break // no break
case 6: case 6:
if (is_null($message->getContact())) { if (is_null($message->getContact())) {
$this->conversation->notes['state'] = 6; $this->conversation->notes['state'] = 6;
$this->conversation->update(); $this->conversation->update();
$data['text'] = 'Share your contact information:'; $data['text'] = 'Share your contact information:';
$data['reply_markup'] = new ReplyKeyboardMarkup([ $data['reply_markup'] = new ReplyKeyboardMarkup(
'keyboard' => [[ [
[ 'text' => 'Share Contact', 'request_contact' => true ], 'keyboard' => [
]], [
'resize_keyboard' => true, [
'one_time_keyboard' => true, 'text' => 'Share Contact',
'selective' => true, 'request_contact' => true
]); ],
$result = Request::sendMessage($data); ]
],
'resize_keyboard' => true,
'one_time_keyboard' => true,
'selective' => true,
]
);
$result = Request::sendMessage($data);
break; break;
} }
$this->conversation->notes['phone_number'] = $message->getContact()->getPhoneNumber(); $this->conversation->notes['phone_number'] = $message->getContact()->getPhoneNumber();
// no break // no break
case 7: case 7:
$this->conversation->update(); $this->conversation->update();
$out_text = '/Survey result:' . "\n"; $out_text = '/Survey result:' . "\n";
unset($this->conversation->notes['state']); unset($this->conversation->notes['state']);
foreach ($this->conversation->notes as $k => $v) { foreach ($this->conversation->notes as $k => $v) {
$out_text .= "\n" . ucfirst($k).': ' . $v; $out_text .= "\n" . ucfirst($k) . ': ' . $v;
} }
$data['photo'] = $this->conversation->notes['photo_id']; $data['photo'] = $this->conversation->notes['photo_id'];
$data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]); $data['reply_markup'] = new ReplyKeyBoardHide(['selective' => true]);
$data['caption'] = $out_text; $data['caption'] = $out_text;
$this->conversation->stop(); $this->conversation->stop();
$result = Request::sendPhoto($data); $result = Request::sendPhoto($data);
break; break;
......
...@@ -21,14 +21,25 @@ use Longman\TelegramBot\Request; ...@@ -21,14 +21,25 @@ use Longman\TelegramBot\Request;
*/ */
class WeatherCommand extends UserCommand class WeatherCommand extends UserCommand
{ {
/**#@+ /**
* {@inheritdoc} * @var string
*/ */
protected $name = 'weather'; protected $name = 'weather';
/**
* @var string
*/
protected $description = 'Show weather by location'; protected $description = 'Show weather by location';
/**
* @var string
*/
protected $usage = '/weather <location>'; protected $usage = '/weather <location>';
/**
* @var string
*/
protected $version = '1.1.0'; protected $version = '1.1.0';
/**#@-*/
/** /**
* Base URI for OpenWeatherMap API * Base URI for OpenWeatherMap API
...@@ -43,12 +54,13 @@ class WeatherCommand extends UserCommand ...@@ -43,12 +54,13 @@ class WeatherCommand extends UserCommand
* @param string $location * @param string $location
* *
* @return string * @return string
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
private function getWeatherData($location) private function getWeatherData($location)
{ {
$client = new Client(['base_uri' => $this->owm_api_base_uri]); $client = new Client(['base_uri' => $this->owm_api_base_uri]);
$path = 'weather'; $path = 'weather';
$query = [ $query = [
'q' => $location, 'q' => $location,
'units' => 'metric', 'units' => 'metric',
'APPID' => trim($this->getConfig('owm_api_key')), 'APPID' => trim($this->getConfig('owm_api_key')),
...@@ -60,7 +72,7 @@ class WeatherCommand extends UserCommand ...@@ -60,7 +72,7 @@ class WeatherCommand extends UserCommand
throw new TelegramException($e->getMessage()); throw new TelegramException($e->getMessage());
} }
return (string) $response->getBody(); return (string)$response->getBody();
} }
/** /**
...@@ -78,7 +90,7 @@ class WeatherCommand extends UserCommand ...@@ -78,7 +90,7 @@ class WeatherCommand extends UserCommand
} }
//http://openweathermap.org/weather-conditions //http://openweathermap.org/weather-conditions
$conditions = [ $conditions = [
'clear' => ' ☀️', 'clear' => ' ☀️',
'clouds' => ' ☁️', 'clouds' => ' ☁️',
'rain' => ' ☔', 'rain' => ' ☔',
...@@ -103,13 +115,15 @@ class WeatherCommand extends UserCommand ...@@ -103,13 +115,15 @@ class WeatherCommand extends UserCommand
} }
/** /**
* {@inheritdoc} * Command execute method
*
* @return mixed
*/ */
public function execute() public function execute()
{ {
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$text = ''; $text = '';
if (trim($this->getConfig('owm_api_key'))) { if (trim($this->getConfig('owm_api_key'))) {
if ($location = trim($message->getText(true))) { if ($location = trim($message->getText(true))) {
......
...@@ -21,49 +21,66 @@ use Longman\TelegramBot\Request; ...@@ -21,49 +21,66 @@ use Longman\TelegramBot\Request;
*/ */
class WhoamiCommand extends UserCommand class WhoamiCommand extends UserCommand
{ {
/**#@+ /**
* {@inheritdoc} * @var string
*/ */
protected $name = 'whoami'; protected $name = 'whoami';
/**
* @var string
*/
protected $description = 'Show your id, name and username'; protected $description = 'Show your id, name and username';
/**
* @var string
*/
protected $usage = '/whoami'; protected $usage = '/whoami';
/**
* @var string
*/
protected $version = '1.0.1'; protected $version = '1.0.1';
/**
* @var bool
*/
protected $public = true; protected $public = true;
/**#@-*/
/** /**
* {@inheritdoc} * Command execute method
*
* @return mixed
*/ */
public function execute() public function execute()
{ {
$message = $this->getMessage(); $message = $this->getMessage();
$user_id = $message->getFrom()->getId(); $user_id = $message->getFrom()->getId();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId(); $message_id = $message->getMessageId();
$text = $message->getText(true); $text = $message->getText(true);
//Send chat action //Send chat action
Request::sendChatAction(['chat_id' => $chat_id, 'action' => 'typing']); Request::sendChatAction(['chat_id' => $chat_id, 'action' => 'typing']);
$caption = 'Your Id: ' . $user_id . "\n"; $caption = 'Your Id: ' . $user_id . "\n";
$caption .= 'Name: ' . $message->getFrom()->getFirstName() $caption .= 'Name: ' . $message->getFrom()->getFirstName()
. ' ' . $message->getFrom()->getLastName() . "\n"; . ' ' . $message->getFrom()->getLastName() . "\n";
$caption .= 'Username: ' . $message->getFrom()->getUsername(); $caption .= 'Username: ' . $message->getFrom()->getUsername();
//Fetch user profile photo //Fetch user profile photo
$limit = 10; $limit = 10;
$offset = null; $offset = null;
$ServerResponse = Request::getUserProfilePhotos([ $ServerResponse = Request::getUserProfilePhotos([
'user_id' => $user_id , 'user_id' => $user_id,
'limit' => $limit, 'limit' => $limit,
'offset' => $offset, 'offset' => $offset,
]); ]);
//Check if the request isOK //Check if the request isOK
if ($ServerResponse->isOk()) { if ($ServerResponse->isOk()) {
$UserProfilePhoto = $ServerResponse->getResult(); $UserProfilePhoto = $ServerResponse->getResult();
$totalcount = $UserProfilePhoto->getTotalCount(); $totalcount = $UserProfilePhoto->getTotalCount();
} else { } else {
$totalcount = 0; $totalcount = 0;
} }
...@@ -76,17 +93,17 @@ class WhoamiCommand extends UserCommand ...@@ -76,17 +93,17 @@ class WhoamiCommand extends UserCommand
if ($totalcount > 0) { if ($totalcount > 0) {
$photos = $UserProfilePhoto->getPhotos(); $photos = $UserProfilePhoto->getPhotos();
//I pick the latest photo with the hight definition //I pick the latest photo with the hight definition
$photo = $photos[0][2]; $photo = $photos[0][2];
$file_id = $photo->getFileId(); $file_id = $photo->getFileId();
$data['photo'] = $file_id; $data['photo'] = $file_id;
$data['caption'] = $caption; $data['caption'] = $caption;
$result = Request::sendPhoto($data); $result = Request::sendPhoto($data);
//Download the image pictures //Download the image pictures
//Download after send message response to speedup response //Download after send message response to speedup response
$file_id = $photo->getFileId(); $file_id = $photo->getFileId();
$ServerResponse = Request::getFile(['file_id' => $file_id]); $ServerResponse = Request::getFile(['file_id' => $file_id]);
if ($ServerResponse->isOk()) { if ($ServerResponse->isOk()) {
Request::downloadFile($ServerResponse->getResult()); Request::downloadFile($ServerResponse->getResult());
...@@ -94,7 +111,7 @@ class WhoamiCommand extends UserCommand ...@@ -94,7 +111,7 @@ class WhoamiCommand extends UserCommand
} else { } else {
//No Photo just send text //No Photo just send text
$data['text'] = $caption; $data['text'] = $caption;
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
} }
return $result; return $result;
......
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