Commit 86c996dc authored by MBoretto's avatar MBoretto

Merge branch 'some_code_cleanup' into further_code_cleanup

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