Commit 86c996dc authored by MBoretto's avatar MBoretto

Merge branch 'some_code_cleanup' into further_code_cleanup

parents a461ac76 f7b4ab67
<?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\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\DB;
use Longman\TelegramBot\Entities\Chat; use Longman\TelegramBot\Entities\Chat;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Request;
/**
* Admin "/chats" command
*/
class ChatsCommand extends Command class ChatsCommand extends Command
{ {
/**#@+
* {@inheritdoc}
*/
protected $name = 'chats'; protected $name = 'chats';
protected $description = 'List all chats stored by the bot'; protected $description = 'List all chats stored by the bot';
protected $usage = '/chats '; protected $usage = '/chats';
protected $version = '1.0.0'; protected $version = '1.0.1';
protected $enabled = true;
protected $public = true; protected $public = true;
//need Mysql protected $need_mysql = false;
protected $need_mysql = true; /**#@-*/
/**
* Execution if MySQL is required but not available
*
* @return boolean
*/
public function executeNoDB() public function executeNoDB()
{ {
//Database not setted or without connection
//Preparing message //Preparing message
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$data = [];
$data['chat_id'] = $chat_id; $data = [
$data['text'] = 'Sorry no database connection, unable to execute '.$this->name.' command.'; 'chat_id' => $chat_id,
$result = Request::sendMessage($data); 'text' => 'Sorry no database connection, unable to execute "' . $this->name . '" command.',
return $result->isOk(); ];
return Request::sendMessage($data)->isOk();
} }
/**
* Execute command
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate();
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId();
$text = $message->getText(true);
$results = DB::selectChats( $results = DB::selectChats(
true, //Send to groups (group chat) true, //Send to groups (group chat)
...@@ -60,39 +72,39 @@ class ChatsCommand extends Command ...@@ -60,39 +72,39 @@ class ChatsCommand extends Command
$user_chats = 0; $user_chats = 0;
$group_chats = 0; $group_chats = 0;
$super_group_chats = 0; $super_group_chats = 0;
$text = "List of bot chats:\n"; $text = 'List of bot chats:' . "\n";
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);
if ($chat->isPrivateChat()) { if ($chat->isPrivateChat()) {
$text .= '- P '.$chat->tryMention()."\n"; $text .= '- P ' . $chat->tryMention() . "\n";
++$user_chats; ++$user_chats;
} elseif ($chat->isGroupChat()) { } elseif ($chat->isGroupChat()) {
$text .= '- G '.$chat->getTitle()."\n"; $text .= '- G ' . $chat->getTitle() . "\n";
++$group_chats; ++$group_chats;
} elseif ($chat->isSuperGroup()) { } elseif ($chat->isSuperGroup()) {
$text .= '- S '.$chat->getTitle()."\n"; $text .= '- S ' . $chat->getTitle() . "\n";
++$super_group_chats; ++$super_group_chats;
} }
} }
if (($group_chats + $user_chats + $super_group_chats) == 0) {
$text = "No chats found.."; if (($user_chats + $group_chats + $super_group_chats) === 0) {
$text = 'No chats found..';
} else { } else {
$text .= "\nPrivate Chats: ".$user_chats; $text .= "\n" . 'Private Chats: ' . $user_chats;
$text .= "\nGroup: ".$group_chats; $text .= "\n" . 'Group: ' . $group_chats;
$text .= "\nSuper Group: ".$super_group_chats; $text .= "\n" . 'Super Group: ' . $super_group_chats;
$text .= "\nTot: ".($group_chats + $user_chats + $super_group_chats); $text .= "\n" . 'Total: ' . ($user_chats + $group_chats + $super_group_chats);
} }
$data = []; $data = [
$data['chat_id'] = $chat_id; 'chat_id' => $chat_id,
$data['text'] = $text; 'text' => $text,
$result = Request::sendMessage($data); ];
return $result->isOk();
return Request::sendMessage($data)->isOk();
} }
} }
<?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\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Request;
use Longman\TelegramBot\Exception\TelegramException;
/**
* Admin "/sendtoall" command
*/
class SendtoallCommand extends Command class SendtoallCommand extends Command
{ {
/**#@+
* {@inheritdoc}
*/
protected $name = 'sendtoall'; protected $name = 'sendtoall';
protected $description = 'Send the message to all the user\'s bot'; protected $description = 'Send the message to all the user\'s bot';
protected $usage = '/sendall <message to send>'; protected $usage = '/sendall <message to send>';
protected $version = '1.2.0'; protected $version = '1.2.1';
protected $enabled = true;
protected $public = true; protected $public = true;
//need Mysql
protected $need_mysql = true; protected $need_mysql = true;
/**#@-*/
/**
* Execution if MySQL is required but not available
*
* @return boolean
*/
public function executeNoDB() public function executeNoDB()
{ {
//Database not setted or without connection
//Preparing message //Preparing message
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$data = [];
$data['chat_id'] = $chat_id; $data = [
$data['text'] = 'Sorry no database connection, unable to execute '.$this->name.' command.'; 'chat_id' => $chat_id,
$result = Request::sendMessage($data); 'text' => 'Sorry no database connection, unable to execute "' . $this->name . '" command.',
return $result->isOk(); ];
return Request::sendMessage($data)->isOk();
} }
/**
* Execute command
*
* @todo Don't use empty, as a string of '0' is regarded to be empty
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate();
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId();
$text = $message->getText(true);
if (empty($text)) { if (empty($text)) {
$text = 'Write the message to sent: /sendall <message>'; $text = 'Write the message to send: /sendall <message>';
} else { } else {
$results = Request::sendToActiveChats( $results = Request::sendToActiveChats(
'sendMessage', //callback function to execute (see Request.php methods) 'sendMessage', //callback function to execute (see Request.php methods)
array('text'=> $text), //Param to evaluate the request ['text' => $text], //Param to evaluate the request
true, //Send to groups (group chat) true, //Send to groups (group chat)
true, //Send to super groups chats (super group chat) true, //Send to super groups chats (super group chat)
true, //Send to users (single chat) true, //Send to users (single chat)
...@@ -65,7 +77,7 @@ class SendtoallCommand extends Command ...@@ -65,7 +77,7 @@ class SendtoallCommand extends Command
$tot = 0; $tot = 0;
$fail = 0; $fail = 0;
$text = "Message sended to:\n"; $text = 'Message sent to:' . "\n";
foreach ($results as $result) { foreach ($results as $result) {
$status = ''; $status = '';
$type = ''; $type = '';
...@@ -88,19 +100,19 @@ class SendtoallCommand extends Command ...@@ -88,19 +100,19 @@ class SendtoallCommand extends Command
} }
++$tot; ++$tot;
$text .= $tot.') '.$status.' '.$type.' '.$name."\n"; $text .= $tot . ') ' . $status . ' ' . $type . ' ' . $name . "\n";
} }
$text .= "Delivered: ".($tot - $fail).'/'.$tot."\n"; $text .= 'Delivered: ' . ($tot - $fail) . '/' . $tot . "\n";
} }
if ($tot == 0) { if ($tot === 0) {
$text = "No users or chats found.."; $text = 'No users or chats found..';
} }
$data = []; $data = [
$data['chat_id'] = $chat_id; 'chat_id' => $chat_id,
$data['text'] = $text; 'text' => $text,
];
$result = Request::sendMessage($data); return Request::sendMessage($data)->isOk();
return $result->isOk();
} }
} }
<?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\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\DB;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Request;
use Longman\TelegramBot\Exception\TelegramException;
/**
* Admin "/sendtochannel" command
*/
class SendtochannelCommand extends Command class SendtochannelCommand extends Command
{ {
/**#@+
* {@inheritdoc}
*/
protected $name = 'sendtochannel'; protected $name = 'sendtochannel';
protected $description = 'Send message to a channel'; protected $description = 'Send message to a channel';
protected $usage = '/sendchannel <message to send>'; protected $usage = '/sendchannel <message to send>';
protected $version = '0.1.0'; protected $version = '0.1.1';
protected $enabled = true;
protected $public = true; protected $public = true;
//need Mysql
protected $need_mysql = false; protected $need_mysql = false;
/**#@-*/
/**
* Execute command
*
* @todo Don't use empty, as a string of '0' is regarded to be empty
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate();
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId();
$text = $message->getText(true); $text = $message->getText(true);
if (empty($text)) { if (empty($text)) {
$text_back = 'Write the message to sent: /sendtochannel <message>'; $text_back = 'Write the message to send: /sendtochannel <message>';
} else { } else {
$your_channel = $this->getConfig('your_channel'); $your_channel = $this->getConfig('your_channel');
//Send message to channel //Send message to channel
$data = []; $data = [
$data['chat_id'] = $your_channel; 'chat_id' => $your_channel,
$data['text'] = $text; 'text' => $text,
];
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
if ($result->isOk()) { if ($result->isOk()) {
$text_back = 'Message sent succesfully to: '.$your_channel; $text_back = 'Message sent succesfully to: ' . $your_channel;
} else { } else {
$text_back = 'Sorry message not sent to: '.$your_channel; $text_back = 'Sorry message not sent to: ' . $your_channel;
} }
} }
$data = []; $data = [
$data['chat_id'] = $chat_id; 'chat_id' => $chat_id,
$data['text'] = $text_back; 'text' => $text_back,
];
$result = Request::sendMessage($data); return Request::sendMessage($data)->isOk();
return $result->isOk();
} }
} }
<?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\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
/**
* Channel chat created command
*/
class ChannelchatcreatedCommand extends Command class ChannelchatcreatedCommand extends Command
{ {
/**#@+
* {@inheritdoc}
*/
protected $name = 'Channelchatcreated'; protected $name = 'Channelchatcreated';
protected $description = 'Channel chat created'; protected $description = 'Channel chat created';
protected $usage = '/'; protected $version = '1.0.1';
protected $version = '1.0.0'; /**#@-*/
protected $enabled = true;
/**
* Execute command
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate(); //$message = $this->getMessage();
$message = $this->getMessage(); //$channel_chat_created = $message->getChannelChatCreated();
$channel_chat_created = $message->getChannelChatCreated();
// temporary do nothing //System command, do nothing
return 1; return true;
} }
} }
<?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\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Entities\InlineQueryResultArticle;
use Longman\TelegramBot\Entities\Entity;
/**
* Chosen inline result command
*/
class ChoseninlineresultCommand extends Command class ChoseninlineresultCommand extends Command
{ {
/**#@+
* {@inheritdoc}
*/
protected $name = 'choseninlineresult'; protected $name = 'choseninlineresult';
protected $description = 'Chosen result query'; protected $description = 'Chosen result query';
protected $usage = ''; protected $version = '1.0.1';
protected $version = '1.0.0'; /**#@-*/
protected $enabled = true;
protected $public = false;
/**
* Execute command
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate(); //Information about chosen result is returned
$inline_query = $update->getChosenInlineResult(); //$update = $this->getUpdate();
$query = $inline_query->getQuery(); //$inline_query = $update->getChosenInlineResult();
//$query = $inline_query->getQuery();
//information about chosen result are returned
//Do nothing
return 1; //System command, do nothing
return true;
} }
} }
<?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\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
/**
* User "/date" command
*/
class DateCommand extends Command class DateCommand extends Command
{ {
/**#@+
* {@inheritdoc}
*/
protected $name = 'date'; protected $name = 'date';
protected $description = 'Show date/time by location'; protected $description = 'Show date/time by location';
protected $usage = '/date <location>'; protected $usage = '/date <location>';
protected $version = '1.2.0'; protected $version = '1.2.1';
protected $enabled = true;
protected $public = true; protected $public = true;
/**#@-*/
/**
* Base URL for Google Maps API
*
* @var string
*/
private $base_url = 'https://maps.googleapis.com/maps/api'; private $base_url = 'https://maps.googleapis.com/maps/api';
/**
* Date format
*
* @var string
*/
private $date_format = 'd-m-Y H:i:s'; private $date_format = 'd-m-Y H:i:s';
/**
* Get coordinates
*
* @param string $location
*
* @return array|boolean
*/
private function getCoordinates($location) private function getCoordinates($location)
{ {
$url = $this->base_url . '/geocode/json?'; $url = $this->base_url . '/geocode/json?';
...@@ -56,9 +79,17 @@ class DateCommand extends Command ...@@ -56,9 +79,17 @@ class DateCommand extends Command
$acc = $data['results'][0]['geometry']['location_type']; $acc = $data['results'][0]['geometry']['location_type'];
$types = $data['results'][0]['types']; $types = $data['results'][0]['types'];
return array($lat, $lng, $acc, $types); return [$lat, $lng, $acc, $types];
} }
/**
* Get date
*
* @param string $lat
* @param string $lng
*
* @return array|boolean
*/
private function getDate($lat, $lng) private function getDate($lat, $lng)
{ {
$url = $this->base_url . '/timezone/json?'; $url = $this->base_url . '/timezone/json?';
...@@ -71,7 +102,7 @@ class DateCommand extends Command ...@@ -71,7 +102,7 @@ class DateCommand extends Command
$google_api_key = $this->getConfig('google_api_key'); $google_api_key = $this->getConfig('google_api_key');
if (!empty($google_api_key)) { if (!empty($google_api_key)) {
$params.= '&key=' . $google_api_key; $params .= '&key=' . $google_api_key;
} }
$data = $this->request($url . $params); $data = $this->request($url . $params);
...@@ -90,14 +121,22 @@ class DateCommand extends Command ...@@ -90,14 +121,22 @@ class DateCommand extends Command
$local_time = $timestamp + $data['rawOffset'] + $data['dstOffset']; $local_time = $timestamp + $data['rawOffset'] + $data['dstOffset'];
return array($local_time, $data['timeZoneId']); return [$local_time, $data['timeZoneId']];
} }
/**
* Get formatted date
*
* @param string $location
*
* @return string
*/
private function getFormattedDate($location) private function getFormattedDate($location)
{ {
if (empty($location)) { if (empty($location)) {
return 'The time in nowhere is never'; return 'The time in nowhere is never';
} }
list($lat, $lng, $acc, $types) = $this->getCoordinates($location); list($lat, $lng, $acc, $types) = $this->getCoordinates($location);
if (empty($lat) || empty($lng)) { if (empty($lat) || empty($lng)) {
...@@ -108,15 +147,20 @@ class DateCommand extends Command ...@@ -108,15 +147,20 @@ class DateCommand extends Command
$date_utc = new \DateTime(gmdate('Y-m-d H:i:s', $local_time), new \DateTimeZone($timezone_id)); $date_utc = new \DateTime(gmdate('Y-m-d H:i:s', $local_time), new \DateTimeZone($timezone_id));
$return = 'The local time in ' . $timezone_id . ' is: ' . $date_utc->format($this->date_format) . ''; return 'The local time in ' . $timezone_id . ' is: ' . $date_utc->format($this->date_format);
return $return;
} }
/**
* Perform a simple cURL request
*
* @param string $url
*
* @return object
*/
private function request($url) private function request($url)
{ {
$ch = curl_init(); $ch = curl_init();
$curlConfig = array(CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true,); $curlConfig = [CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true];
curl_setopt_array($ch, $curlConfig); curl_setopt_array($ch, $curlConfig);
$response = curl_exec($ch); $response = curl_exec($ch);
...@@ -130,9 +174,13 @@ class DateCommand extends Command ...@@ -130,9 +174,13 @@ class DateCommand extends Command
return $response; return $response;
} }
/**
* Execute command
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate();
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
...@@ -150,12 +198,12 @@ class DateCommand extends Command ...@@ -150,12 +198,12 @@ class DateCommand extends Command
} }
} }
$data = []; $data = [
$data['chat_id'] = $chat_id; 'chat_id' => $chat_id,
$data['reply_to_message_id'] = $message_id; 'reply_to_message_id' => $message_id,
$data['text'] = $text; 'text' => $text,
];
$result = Request::sendMessage($data); return Request::sendMessage($data)->isOk();
return $result->isOk();
} }
} }
<?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\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
/**
* Delete chat photo command
*/
class DeletechatphotoCommand extends Command class DeletechatphotoCommand extends Command
{ {
/**#@+
* {@inheritdoc}
*/
protected $name = 'Deletechatphoto'; protected $name = 'Deletechatphoto';
protected $description = 'Delete chat photo'; protected $description = 'Delete chat photo';
protected $usage = '/'; protected $version = '1.0.1';
protected $version = '1.0.0'; /**#@-*/
protected $enabled = true;
/**
* Execute command
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate(); //$message = $this->getMessage();
$message = $this->getMessage(); //$delete_chat_photo = $message->getDeleteChatPhoto();
$delete_chat_photo = $message->getDeleteChatPhoto();
// temporary do nothing //System command, do nothing
return 1; return true;
} }
} }
<?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\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Request;
/**
* User "/echo" command
*/
class EchoCommand extends Command class EchoCommand extends Command
{ {
/**#@+
* {@inheritdoc}
*/
protected $name = 'echo'; protected $name = 'echo';
protected $description = 'Show text'; protected $description = 'Show text';
protected $usage = '/echo <text>'; protected $usage = '/echo <text>';
protected $version = '1.0.0'; protected $version = '1.0.1';
protected $enabled = true;
protected $public = true; protected $public = true;
/**#@-*/
/**
* Execute command
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate();
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$text = $message->getText(true); $text = $message->getText(true);
$data = []; $data = [
$data['chat_id'] = $chat_id; 'chat_id' => $chat_id,
$data['text'] = $text; 'text' => $text,
];
$result = Request::sendMessage($data); return Request::sendMessage($data)->isOk();
return $result->isOk();
} }
} }
<?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\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Request;
/**
* Generic command
*/
class GenericCommand extends Command class GenericCommand extends Command
{ {
/**#@+
* {@inheritdoc}
*/
protected $name = 'Generic'; protected $name = 'Generic';
protected $description = 'Handle generic commands or is executed by default when a command is not found'; protected $description = 'Handles generic commands or is executed by default when a command is not found';
protected $usage = '/'; protected $version = '1.0.1';
protected $version = '1.0.0'; /**#@-*/
protected $enabled = true;
/**
* Execute command
*
* @todo This can't be right, as it always returns "Command: xyz not found.. :("
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate();
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId();
//you can use $command as param
$command = $message->getCommand();
//You can use $command as param
$command = $message->getCommand();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$text = $message->getText(true);
$data = array(); $data = [
$data['chat_id'] = $chat_id; 'chat_id' => $chat_id,
$data['text'] = 'Command: '.$command.' not found.. :('; 'text' => 'Command: ' . $command . ' not found.. :(',
$result = Request::sendMessage($data); ];
return $result->isOk();
return Request::sendMessage($data)->isOk();
} }
} }
<?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\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
/**
* Generic message command
*/
class GenericmessageCommand extends Command class GenericmessageCommand extends Command
{ {
/**#@+
* {@inheritdoc}
*/
protected $name = 'Genericmessage'; protected $name = 'Genericmessage';
protected $description = 'Handle generic message'; protected $description = 'Handle generic message';
protected $usage = '/'; protected $version = '1.0.1';
protected $version = '1.0.0'; /**#@-*/
protected $enabled = true;
/**
* Execute command
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate(); //System command, do nothing
$message = $this->getMessage(); return true;
$chat_id = $message->getChat()->getId();
//you can use $command as param
$command = $message->getCommand();
$chat_id = $message->getChat()->getId();
$text = $message->getText(true);
//Do nothing
return 1;
} }
} }
<?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\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
/**
* Group chat created command
*/
class GroupchatcreatedCommand extends Command class GroupchatcreatedCommand extends Command
{ {
/**#@+
* {@inheritdoc}
*/
protected $name = 'Groupchatcreated'; protected $name = 'Groupchatcreated';
protected $description = 'Group chat created'; protected $description = 'Group chat created';
protected $usage = '/'; protected $version = '1.0.1';
protected $version = '1.0.0'; /**#@-*/
protected $enabled = true;
/**
* Execute command
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate(); //$message = $this->getMessage();
$message = $this->getMessage(); //$group_chat_created = $message->getGroupChatCreated();
$group_chat_created = $message->getGroupChatCreated();
// temporary do nothing //System command, do nothing
return 1; return true;
} }
} }
<?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\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Request;
/**
* User "/help" command
*/
class HelpCommand extends Command class HelpCommand extends Command
{ {
/**#@+
* {@inheritdoc}
*/
protected $name = 'help'; protected $name = 'help';
protected $description = 'Show bot commands help'; protected $description = 'Show bot commands help';
protected $usage = '/help or /help <command>'; protected $usage = '/help or /help <command>';
protected $version = '1.0.0'; protected $version = '1.0.1';
protected $enabled = true;
protected $public = true; protected $public = true;
/**#@-*/
/**
* Execute command
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate();
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
...@@ -67,12 +76,12 @@ class HelpCommand extends Command ...@@ -67,12 +76,12 @@ class HelpCommand extends Command
} }
} }
$data = []; $data = [
$data['chat_id'] = $chat_id; 'chat_id' => $chat_id,
$data['reply_to_message_id'] = $message_id; 'reply_to_message_id' => $message_id,
$data['text'] = $msg; 'text' => $msg,
];
$result = Request::sendMessage($data); return Request::sendMessage($data)->isOk();
return $result->isOk();
} }
} }
<?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\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Entities\InlineQueryResultArticle; use Longman\TelegramBot\Entities\InlineQueryResultArticle;
use Longman\TelegramBot\Entities\Entity; use Longman\TelegramBot\Request;
/**
* Inline query command
*/
class InlinequeryCommand extends Command class InlinequeryCommand extends Command
{ {
/**#@+
* {@inheritdoc}
*/
protected $name = 'inlinequery'; protected $name = 'inlinequery';
protected $description = 'Reply to inline query'; protected $description = 'Reply to inline query';
protected $usage = ''; protected $version = '1.0.1';
protected $version = '1.0.0'; /**#@-*/
protected $enabled = true;
protected $public = false;
/**
* Execute command
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate(); $update = $this->getUpdate();
$inline_query = $update->getInlineQuery(); $inline_query = $update->getInlineQuery();
$query = $inline_query->getQuery(); $query = $inline_query->getQuery();
$data = []; $data = ['inline_query_id' => $inline_query->getId()];
$data['inline_query_id']= $inline_query->getId();
$articles = []; $articles = [
$articles[] = ['id' => '001' , 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: '.$query ]; ['id' => '001', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query],
$articles[] = ['id' => '002' , 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: '.$query ]; ['id' => '002', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query],
$articles[] = ['id' => '003' , 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: '.$query ]; ['id' => '003', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query],
];
$array_article = []; $array_article = [];
foreach ($articles as $article) { foreach ($articles as $article) {
$array_article[] = new InlineQueryResultArticle($article); $array_article[] = new InlineQueryResultArticle($article);
} }
$array_json = '['.implode(',', $array_article).']'; $data['results'] = '[' . implode(',', $array_article) . ']';
$data['results'] = $array_json;
$result = Request::answerInlineQuery($data);
return $result->isOk(); return Request::answerInlineQuery($data)->isOk();
} }
} }
<?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\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
/**
* Left chat participant command
*/
class LeftchatparticipantCommand extends Command class LeftchatparticipantCommand extends Command
{ {
/**#@+
* {@inheritdoc}
*/
protected $name = 'leftchatparticipant'; protected $name = 'leftchatparticipant';
protected $description = 'Left Chat Participant'; protected $description = 'Left Chat Participant';
protected $usage = '/'; protected $version = '1.0.1';
protected $version = '1.0.0'; /**#@-*/
protected $enabled = true;
/**
* Execute command
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate(); //$message = $this->getMessage();
$message = $this->getMessage(); //$participant = $message->getLeftChatParticipant();
$participant = $message->getLeftChatParticipant();
// temporary do nothing //System command, do nothing
return 1; return true;
} }
} }
<?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\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Request;
/**
* New chat participant command
*/
class NewchatparticipantCommand extends Command class NewchatparticipantCommand extends Command
{ {
/**#@+
* {@inheritdoc}
*/
protected $name = 'Newchatparticipant'; protected $name = 'Newchatparticipant';
protected $description = 'New Chat Participant'; protected $description = 'New Chat Participant';
protected $usage = '/'; protected $version = '1.0.1';
protected $version = '1.0.0'; /**#@-*/
protected $enabled = true;
/**
* Execute command
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate();
$message = $this->getMessage(); $message = $this->getMessage();
$participant = $message->getNewChatParticipant();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$data = []; $participant = $message->getNewChatParticipant();
$data['chat_id'] = $chat_id;
if (strtolower($participant->getUsername()) == strtolower($this->getTelegram()->getBotName())) { if (strtolower($participant->getUsername()) === strtolower($this->getTelegram()->getBotName())) {
$text = 'Hi there!'; $text = 'Hi there!';
} else { } else {
$text = 'Hi '.$participant->tryMention().' !'; $text = 'Hi ' . $participant->tryMention() . '!';
} }
$data['text'] = $text; $data = [
$result = Request::sendMessage($data); 'chat_id' => $chat_id,
return $result->isOk(); 'text' => $text,
];
return Request::sendMessage($data)->isOk();
} }
} }
<?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\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
/**
* New chat title command
*/
class NewchattitleCommand extends Command class NewchattitleCommand extends Command
{ {
/**#@+
* {@inheritdoc}
*/
protected $name = 'Newchattitle'; protected $name = 'Newchattitle';
protected $description = 'New chat Title'; protected $description = 'New chat Title';
protected $usage = '/'; protected $version = '1.0.1';
protected $version = '1.0.0'; /**#@-*/
protected $enabled = true;
/**
* Execute command
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate(); //$message = $this->getMessage();
$message = $this->getMessage(); //$new_chat_title = $message->getNewChatTitle();
$new_chat_title = $message->getNewChatTitle();
// temporary do nothing //System command, do nothing
return 1; return true;
} }
} }
<?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\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Request;
/**
* User "/slap" command
*/
class SlapCommand extends Command class SlapCommand extends Command
{ {
/**#@+
* {@inheritdoc}
*/
protected $name = 'slap'; protected $name = 'slap';
protected $description = 'Slap someone with their username'; protected $description = 'Slap someone with their username';
protected $usage = '/slap <@user>'; protected $usage = '/slap <@user>';
protected $version = '1.0.0'; protected $version = '1.0.1';
protected $enabled = true; /**#@-*/
/**
* Execute command
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate();
$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();
$data = array();
$data['chat_id'] = $chat_id;
//username validation //username validation
$test=preg_match('/@[\w_]{5,}/', $text); $test = preg_match('/@[\w_]{5,}/', $text);
if ($test===0) { if ($test === 0) {
$data['text'] = $sender.' sorry no one to slap around..'; $text = $sender . ' sorry no one to slap around..';
} else { } else {
$data['text'] = $sender.' slaps '.$text.' around a bit with a large trout'; $text = $sender . ' slaps ' . $text . ' around a bit with a large trout';
} }
$result = Request::sendMessage($data); $data = [
return $result->isOk(); 'chat_id' => $chat_id,
'text' => $text,
];
return Request::sendMessage($data)->isOk();
} }
} }
<?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\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Request;
/**
* Start command
*/
class StartCommand extends Command class StartCommand extends Command
{ {
/**#@+
* {@inheritdoc}
*/
protected $name = 'start'; protected $name = 'start';
protected $description = 'Start command'; protected $description = 'Start command';
protected $usage = '/'; protected $usage = '/';
protected $version = '1.0.0'; protected $version = '1.0.1';
protected $enabled = true; /**#@-*/
protected $public = false;
/**
* Execute command
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate();
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$data = array(); $text = 'Hi there!' . "\n" . 'Type /help to see all commands!';
$data['chat_id'] = $chat_id;
$text = "Hi there!\nType /help to see all commands!"; $data = [
'chat_id' => $chat_id,
'text' => $text,
];
$data['text'] = $text; return Request::sendMessage($data)->isOk();
$result = Request::sendMessage($data);
return $result->isOk();
} }
} }
<?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\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Request;
/**
* Super group chat created command
*/
class SupergroupchatcreatedCommand extends Command class SupergroupchatcreatedCommand extends Command
{ {
/**#@+
* {@inheritdoc}
*/
protected $name = 'Supergroupchatcreated'; protected $name = 'Supergroupchatcreated';
protected $description = 'Super group chat created'; protected $description = 'Super group chat created';
protected $usage = '/'; protected $version = '1.0.1';
protected $version = '1.0.0'; /**#@-*/
protected $enabled = true;
/**
* Execute command
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate();
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$text = ''; $text = '';
if ($message->getSuperGroupChatCreated()) { if ($message->getSuperGroupChatCreated()) {
$text = "Your group has become a Supergroup!\n"; $text = 'Your group has become a Supergroup!' . "\n";
$text .= 'Chat id has changed from'.$message->getMigrateFromChatId().' to '.$message->getMigrateToChatId(); $text .= 'Chat id has changed from ' . $message->getMigrateFromChatId() . ' to ' . $message->getMigrateToChatId();
} }
$data = []; $data = [
$data['chat_id'] = $chat_id; 'chat_id' => $chat_id,
$data['text'] = $text; 'text' => $text,
];
$result = Request::sendMessage($data); return Request::sendMessage($data)->isOk();
return $result->isOk();
} }
} }
<?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\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Request;
/**
* User "/weather" command
*/
class WeatherCommand extends Command class WeatherCommand extends Command
{ {
/**#@+
* {@inheritdoc}
*/
protected $name = 'weather'; protected $name = 'weather';
protected $description = 'Show weather by location'; protected $description = 'Show weather by location';
protected $usage = '/weather <location>'; protected $usage = '/weather <location>';
protected $version = '1.0.0'; protected $version = '1.0.1';
protected $enabled = true;
protected $public = true; protected $public = true;
/**#@-*/
/**
* Get weather using cURL request
*
* @param string $location
*
* @return object
*/
private function getWeather($location) private function getWeather($location)
{ {
$url = 'http://api.openweathermap.org/data/2.5/weather?q=' . $location . '&units=metric'; $url = 'http://api.openweathermap.org/data/2.5/weather?q=' . $location . '&units=metric';
$ch = curl_init(); $ch = curl_init();
$curlConfig = array(CURLOPT_URL => $url, $curlConfig = [
CURLOPT_URL => $url,
//CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
];
//CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
//CURLOPT_POSTFIELDS => $data
//CURLOPT_VERBOSE => true,
//CURLOPT_HEADER => true,
);
curl_setopt_array($ch, $curlConfig); curl_setopt_array($ch, $curlConfig);
$response = curl_exec($ch); $response = curl_exec($ch);
...@@ -51,6 +57,13 @@ class WeatherCommand extends Command ...@@ -51,6 +57,13 @@ class WeatherCommand extends Command
return $response; return $response;
} }
/**
* Get weather string
*
* @param string $location
*
* @return bool|string
*/
private function getWeatherString($location) private function getWeatherString($location)
{ {
if (empty($location)) { if (empty($location)) {
...@@ -64,6 +77,7 @@ class WeatherCommand extends Command ...@@ -64,6 +77,7 @@ class WeatherCommand extends Command
if (empty($decode) || $decode['cod'] != 200) { if (empty($decode) || $decode['cod'] != 200) {
return false; return false;
} }
$city = $decode['name']; $city = $decode['name'];
$country = $decode['sys']['country']; $country = $decode['sys']['country'];
$temp = 'The temperature in ' . $city . ' (' . $country . ') is ' . $decode['main']['temp'] . '°C'; $temp = 'The temperature in ' . $city . ' (' . $country . ') is ' . $decode['main']['temp'] . '°C';
...@@ -71,19 +85,19 @@ class WeatherCommand extends Command ...@@ -71,19 +85,19 @@ class WeatherCommand extends Command
switch (strtolower($decode['weather'][0]['main'])) { switch (strtolower($decode['weather'][0]['main'])) {
case 'clear': case 'clear':
$conditions.= ' ☀'; $conditions .= ' ☀';
break; break;
case 'clouds': case 'clouds':
$conditions.= ' ☁☁'; $conditions .= ' ☁☁';
break; break;
case 'rain': case 'rain':
$conditions.= ' ☔'; $conditions .= ' ☔';
break; break;
case 'thunderstorm': case 'thunderstorm':
$conditions.= ' ☔☔☔☔'; $conditions .= ' ☔☔☔☔';
break; break;
} }
...@@ -91,12 +105,17 @@ class WeatherCommand extends Command ...@@ -91,12 +105,17 @@ class WeatherCommand extends Command
} catch (\Exception $e) { } catch (\Exception $e) {
$result = ''; $result = '';
} }
return $result; return $result;
} }
/**
* Execute command
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate();
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
...@@ -114,12 +133,12 @@ class WeatherCommand extends Command ...@@ -114,12 +133,12 @@ class WeatherCommand extends Command
} }
} }
$data = []; $data = [
$data['chat_id'] = $chat_id; 'chat_id' => $chat_id,
$data['reply_to_message_id'] = $message_id; 'reply_to_message_id' => $message_id,
$data['text'] = $text; 'text' => $text,
];
$result = Request::sendMessage($data); return Request::sendMessage($data)->isOk();
return $result->isOk();
} }
} }
<?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>
...@@ -9,27 +8,36 @@ ...@@ -9,27 +8,36 @@
* file that was distributed with this source code. * file that was distributed with this source code.
* *
* Written by Marco Boretto <marco.bore@gmail.com> * Written by Marco Boretto <marco.bore@gmail.com>
*/ */
namespace Longman\TelegramBot\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Entities\File; use Longman\TelegramBot\Entities\File;
use Longman\TelegramBot\Request;
/**
* User "/whoami" command
*/
class WhoamiCommand extends Command class WhoamiCommand extends Command
{ {
/**#@+
* {@inheritdoc}
*/
protected $name = 'whoami'; protected $name = 'whoami';
protected $description = 'Show your id, name and username'; protected $description = 'Show your id, name and username';
protected $usage = '/whoami'; protected $usage = '/whoami';
protected $version = '1.0.0'; protected $version = '1.0.1';
protected $enabled = true;
protected $public = true; protected $public = true;
/**#@-*/
/**
* Execute command
*
* @return boolean
*/
public function execute() public function execute()
{ {
$update = $this->getUpdate();
$message = $this->getMessage(); $message = $this->getMessage();
$user_id = $message->getFrom()->getId(); $user_id = $message->getFrom()->getId();
...@@ -37,13 +45,13 @@ class WhoamiCommand extends Command ...@@ -37,13 +45,13 @@ class WhoamiCommand extends Command
$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: ' . $message->getFrom()->getId(); $caption = 'Your Id: ' . $user_id . "\n";
$caption .= "\n" . 'Name: ' . $message->getFrom()->getFirstName() $caption .= 'Name: ' . $message->getFrom()->getFirstName()
. ' ' . $message->getFrom()->getLastName(); . ' ' . $message->getFrom()->getLastName() . "\n";
$caption .= "\n" . 'Username: ' . $message->getFrom()->getUsername(); $caption .= 'Username: ' . $message->getFrom()->getUsername();
//Fetch user profile photo //Fetch user profile photo
$limit = 10; $limit = 10;
...@@ -51,7 +59,7 @@ class WhoamiCommand extends Command ...@@ -51,7 +59,7 @@ class WhoamiCommand extends Command
$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
...@@ -62,9 +70,10 @@ class WhoamiCommand extends Command ...@@ -62,9 +70,10 @@ class WhoamiCommand extends Command
$totalcount = 0; $totalcount = 0;
} }
$data = []; $data = [
$data['chat_id'] = $chat_id; 'chat_id' => $chat_id,
$data['reply_to_message_id'] = $message_id; 'reply_to_message_id' => $message_id,
];
if ($totalcount > 0) { if ($totalcount > 0) {
$photos = $UserProfilePhoto->getPhotos(); $photos = $UserProfilePhoto->getPhotos();
...@@ -72,7 +81,6 @@ class WhoamiCommand extends Command ...@@ -72,7 +81,6 @@ class WhoamiCommand extends Command
$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;
...@@ -91,6 +99,7 @@ class WhoamiCommand extends Command ...@@ -91,6 +99,7 @@ class WhoamiCommand extends Command
$data['text'] = $caption; $data['text'] = $caption;
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
} }
return $result->isOk(); return $result->isOk();
} }
} }
<?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\Exception; namespace Longman\TelegramBot\Exception;
/**
* Main exception class used for exception handling
*/
class TelegramException extends \Exception class TelegramException extends \Exception
{ {
/**
* Exception constructor that writes the exception message to the logfile
*
* @param string $message Error message
* @param integer $code Error code
*/
public function __construct($message, $code = 0) public function __construct($message, $code = 0)
{ {
parent::__construct($message, $code); parent::__construct($message, $code);
...@@ -19,9 +28,8 @@ class TelegramException extends \Exception ...@@ -19,9 +28,8 @@ class TelegramException extends \Exception
$path = 'TelegramException.log'; $path = 'TelegramException.log';
$status = file_put_contents( $status = file_put_contents(
$path, $path,
date('Y-m-d H:i:s', time()) .' '. self::__toString() . "\n", date('Y-m-d H:i:s', time()) . ' ' . self::__toString() . "\n",
FILE_APPEND FILE_APPEND
); );
} }
} }
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