Browse Source

base

master
LIERLIER 1 year ago
commit
197a7949bd
  1. 4
      .gitignore
  2. 245
      Commands/Config/DateCommand.php
  3. 28
      Commands/Config/README.md
  4. 156
      Commands/Config/WeatherCommand.php
  5. 105
      Commands/Conversation/CancelCommand.php
  6. 85
      Commands/Conversation/GenericmessageCommand.php
  7. 9
      Commands/Conversation/README.md
  8. 258
      Commands/Conversation/SurveyCommand.php
  9. 61
      Commands/GenericCommand.php
  10. 82
      Commands/Group/GenericmessageCommand.php
  11. 58
      Commands/Group/LeftchatmemberCommand.php
  12. 67
      Commands/Group/NewchatmembersCommand.php
  13. 6
      Commands/Group/README.md
  14. 54
      Commands/InlineMode/ChoseninlineresultCommand.php
  15. 100
      Commands/InlineMode/InlinequeryCommand.php
  16. 7
      Commands/InlineMode/README.md
  17. 61
      Commands/Keyboard/CallbackqueryCommand.php
  18. 60
      Commands/Keyboard/ForcereplyCommand.php
  19. 61
      Commands/Keyboard/HidekeyboardCommand.php
  20. 72
      Commands/Keyboard/InlinekeyboardCommand.php
  21. 102
      Commands/Keyboard/KeyboardCommand.php
  22. 13
      Commands/Keyboard/README.md
  23. 53
      Commands/Message/ChannelpostCommand.php
  24. 53
      Commands/Message/EditedchannelpostCommand.php
  25. 52
      Commands/Message/EditedmessageCommand.php
  26. 82
      Commands/Message/EditmessageCommand.php
  27. 61
      Commands/Message/GenericmessageCommand.php
  28. 15
      Commands/Message/README.md
  29. 64
      Commands/Other/EchoCommand.php
  30. 133
      Commands/Other/HelpCommand.php
  31. 110
      Commands/Other/ImageCommand.php
  32. 68
      Commands/Other/MarkdownCommand.php
  33. 5
      Commands/Other/README.md
  34. 67
      Commands/Other/SlapCommand.php
  35. 115
      Commands/Other/UploadCommand.php
  36. 124
      Commands/Other/WhoamiCommand.php
  37. 60
      Commands/Payments/GenericmessageCommand.php
  38. 125
      Commands/Payments/PaymentCommand.php
  39. 57
      Commands/Payments/PrecheckoutqueryCommand.php
  40. 39
      Commands/Payments/README.md
  41. 80
      Commands/Payments/ShippingqueryCommand.php
  42. 25
      Commands/README.MD
  43. 74
      Commands/ServiceMessages/GenericmessageCommand.php
  44. 72
      Commands/StartCommand.php
  45. 13
      CustomCommands/README.md
  46. 13
      README.md
  47. 31
      composer.json
  48. 92
      config.php
  49. 50
      cron.php
  50. 53
      getUpdatesCLI.php
  51. 78
      hook.php
  52. 42
      manager.php
  53. 26
      phpcs.xml.dist
  54. 42
      set.php
  55. 34
      unset.php

4
.gitignore

@ -0,0 +1,4 @@
composer.phar
composer.lock
#config.php
vendor

245
Commands/Config/DateCommand.php

@ -0,0 +1,245 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* User "/date" command
*
* Shows the date and time of the location passed as the parameter.
*
* A Google API key is required for this command!
* You can be set in your config.php file:
* ['commands']['configs']['date'] => ['google_api_key' => 'your_google_api_key_here']
*/
namespace Longman\TelegramBot\Commands\UserCommands;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
use Longman\TelegramBot\TelegramLog;
class DateCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'date';
/**
* @var string
*/
protected $description = 'Show date/time by location';
/**
* @var string
*/
protected $usage = '/date <location>';
/**
* @var string
*/
protected $version = '1.5.0';
/**
* Guzzle Client object
*
* @var Client
*/
private $client;
/**
* Base URI for Google Maps API
*
* @var string
*/
private $google_api_base_uri = 'https://maps.googleapis.com/maps/api/';
/**
* The Google API Key from the command config
*
* @var string
*/
private $google_api_key;
/**
* Date format
*
* @var string
*/
private $date_format = 'd-m-Y H:i:s';
/**
* Get coordinates of passed location
*
* @param string $location
*
* @return array
*/
private function getCoordinates($location): array
{
$path = 'geocode/json';
$query = ['address' => urlencode($location)];
if ($this->google_api_key !== null) {
$query['key'] = $this->google_api_key;
}
try {
$response = $this->client->get($path, ['query' => $query]);
} catch (RequestException $e) {
TelegramLog::error($e->getMessage());
return [];
}
if (!($data = $this->validateResponseData($response->getBody()))) {
return [];
}
$result = $data['results'][0];
$lat = $result['geometry']['location']['lat'];
$lng = $result['geometry']['location']['lng'];
$acc = $result['geometry']['location_type'];
$types = $result['types'];
return [$lat, $lng, $acc, $types];
}
/**
* Get date for location passed via coordinates
*
* @param string $lat
* @param string $lng
*
* @return array
* @throws \Exception
*/
private function getDate($lat, $lng): array
{
$path = 'timezone/json';
$date_utc = new \DateTimeImmutable(null, new \DateTimeZone('UTC'));
$timestamp = $date_utc->format('U');
$query = [
'location' => urlencode($lat) . ',' . urlencode($lng),
'timestamp' => urlencode($timestamp),
];
if ($this->google_api_key !== null) {
$query['key'] = $this->google_api_key;
}
try {
$response = $this->client->get($path, ['query' => $query]);
} catch (RequestException $e) {
TelegramLog::error($e->getMessage());
return [];
}
if (!($data = $this->validateResponseData($response->getBody()))) {
return [];
}
$local_time = $timestamp + $data['rawOffset'] + $data['dstOffset'];
return [$local_time, $data['timeZoneId']];
}
/**
* Evaluate the response data and see if the request was successful
*
* @param string $data
*
* @return array
*/
private function validateResponseData($data): array
{
if (empty($data)) {
return [];
}
$data = json_decode($data, true);
if (empty($data)) {
return [];
}
if (isset($data['status']) && $data['status'] !== 'OK') {
return [];
}
return $data;
}
/**
* Get formatted date at the passed location
*
* @param string $location
*
* @return string
* @throws \Exception
*/
private function getFormattedDate($location): string
{
if ($location === null || $location === '') {
return 'The time in nowhere is never';
}
[$lat, $lng] = $this->getCoordinates($location);
if (empty($lat) || empty($lng)) {
return 'It seems that in "' . $location . '" they do not have a concept of time.';
}
[$local_time, $timezone_id] = $this->getDate($lat, $lng);
$date_utc = new \DateTimeImmutable(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);
}
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
// First we set up the necessary member variables.
$this->client = new Client(['base_uri' => $this->google_api_base_uri]);
if (($this->google_api_key = trim($this->getConfig('google_api_key'))) === '') {
$this->google_api_key = null;
}
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$location = $message->getText(true);
$text = 'You must specify location in format: /date <city>';
if ($location !== '') {
$text = $this->getFormattedDate($location);
}
$data = [
'chat_id' => $chat_id,
'text' => $text,
];
return Request::sendMessage($data);
}
}

28
Commands/Config/README.md

@ -0,0 +1,28 @@
# Config
Custom configurations can be passed to commands that support them.
This feature is mainly used to pass secrets or special values to the commands.
## Adding configurations to your config
It is very easy to add configurations to `config.php`:
```php
'commands' => [
'configs' => [
'yourcommand' => ['your_config_key' => 'your_config_value'],
],
],
```
Alternatively, you can set them directly via code in your `hook.php`:
```php
$telegram->setCommandConfig('yourcommand', ['your_config_key' => 'your_config_value']);
```
## Reading configurations in your command
To read any command configurations, you can fetch them from within your command like this:
```php
$my_config = $this->getConfig('your_config_key'); // 'your_config_value'
```

156
Commands/Config/WeatherCommand.php

@ -0,0 +1,156 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* User "/weather" command
*
* Get weather info for the location passed as the parameter..
*
* A OpenWeatherMap.org API key is required for this command!
* You can be set in your config.php file:
* ['commands']['configs']['weather'] => ['owm_api_key' => 'your_owm_api_key_here']
*/
namespace Longman\TelegramBot\Commands\UserCommands;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\TelegramLog;
class WeatherCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'weather';
/**
* @var string
*/
protected $description = 'Show weather by location';
/**
* @var string
*/
protected $usage = '/weather <location>';
/**
* @var string
*/
protected $version = '1.3.0';
/**
* Base URI for OpenWeatherMap API
*
* @var string
*/
private $owm_api_base_uri = 'http://api.openweathermap.org/data/2.5/';
/**
* Get weather data using HTTP request
*
* @param string $location
*
* @return string
*/
private function getWeatherData($location): string
{
$client = new Client(['base_uri' => $this->owm_api_base_uri]);
$path = 'weather';
$query = [
'q' => $location,
'units' => 'metric',
'APPID' => trim($this->getConfig('owm_api_key')),
];
try {
$response = $client->get($path, ['query' => $query]);
} catch (RequestException $e) {
TelegramLog::error($e->getMessage());
return '';
}
return (string) $response->getBody();
}
/**
* Get weather string from weather data
*
* @param array $data
*
* @return string
*/
private function getWeatherString(array $data): string
{
try {
if (!(isset($data['cod']) && $data['cod'] === 200)) {
return '';
}
//http://openweathermap.org/weather-conditions
$conditions = [
'clear' => ' ☀️',
'clouds' => ' ☁️',
'rain' => ' ☔',
'drizzle' => ' ☔',
'thunderstorm' => ' ⚡️',
'snow' => ' ❄️',
];
$conditions_now = strtolower($data['weather'][0]['main']);
return sprintf(
'The temperature in %s (%s) is %s°C' . PHP_EOL .
'Current conditions are: %s%s',
$data['name'], //city
$data['sys']['country'], //country
$data['main']['temp'], //temperature
$data['weather'][0]['description'], //description of weather
$conditions[$conditions_now] ?? ''
);
} catch (Exception $e) {
TelegramLog::error($e->getMessage());
return '';
}
}
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
// Check to make sure the required OWM API key has been defined.
$owm_api_key = $this->getConfig('owm_api_key');
if (empty($owm_api_key)) {
return $this->replyToChat('OpenWeatherMap API key not defined.');
}
$location = trim($this->getMessage()->getText(true));
if ($location === '') {
return $this->replyToChat('You must specify a location as: ' . $this->getUsage());
}
$text = 'Cannot find weather for location: ' . $location;
if ($weather_data = json_decode($this->getWeatherData($location), true)) {
$text = $this->getWeatherString($weather_data);
}
return $this->replyToChat($text);
}
}

105
Commands/Conversation/CancelCommand.php

@ -0,0 +1,105 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* User "/cancel" command
*
* This command cancels the currently active conversation and
* returns a message to let the user know which conversation it was.
*
* If no conversation is active, the returned message says so.
*/
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Conversation;
use Longman\TelegramBot\Entities\Keyboard;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
class CancelCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'cancel';
/**
* @var string
*/
protected $description = 'Cancel the currently active conversation';
/**
* @var string
*/
protected $usage = '/cancel';
/**
* @var string
*/
protected $version = '0.3.0';
/**
* @var bool
*/
protected $need_mysql = true;
/**
* Main command execution if no DB connection is available
*
* @throws TelegramException
*/
public function executeNoDb(): ServerResponse
{
return $this->removeKeyboard('Nothing to cancel.');
}
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
$text = 'No active conversation!';
// Cancel current conversation if any
$conversation = new Conversation(
$this->getMessage()->getFrom()->getId(),
$this->getMessage()->getChat()->getId()
);
if ($conversation_command = $conversation->getCommand()) {
$conversation->cancel();
$text = 'Conversation "' . $conversation_command . '" cancelled!';
}
return $this->removeKeyboard($text);
}
/**
* Remove the keyboard and output a text.
*
* @param string $text
*
* @return ServerResponse
* @throws TelegramException
*/
private function removeKeyboard(string $text): ServerResponse
{
return $this->replyToChat($text, [
'reply_markup' => Keyboard::remove(['selective' => true]),
]);
}
}

85
Commands/Conversation/GenericmessageCommand.php

@ -0,0 +1,85 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Generic message command
*
* Gets executed when any type of message is sent.
*
* In this conversation-related context, we must ensure that active conversations get executed correctly.
*/
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Conversation;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
class GenericmessageCommand extends SystemCommand
{
/**
* @var string
*/
protected $name = 'genericmessage';
/**
* @var string
*/
protected $description = 'Handle generic message';
/**
* @var string
*/
protected $version = '1.0.0';
/**
* @var bool
*/
protected $need_mysql = true;
/**
* Command execute method if MySQL is required but not available
*
* @return ServerResponse
*/
public function executeNoDb(): ServerResponse
{
// Do nothing
return Request::emptyResponse();
}
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
$message = $this->getMessage();
// If a conversation is busy, execute the conversation command after handling the message.
$conversation = new Conversation(
$message->getFrom()->getId(),
$message->getChat()->getId()
);
// Fetch conversation command if it exists and execute it.
if ($conversation->exists() && $command = $conversation->getCommand()) {
return $this->telegram->executeCommand($command);
}
return Request::emptyResponse();
}
}

9
Commands/Conversation/README.md

@ -0,0 +1,9 @@
# Conversation
Conversations can be used to create dialogues with users, to collect information in a "conversational" style.
Look at the [`SurveyCommand`](SurveyCommand.php) to see how a conversation can be made.
For conversations to work, you must add the code provided in [`GenericmessageCommand.php`](GenericmessageCommand.php) at the beginning of your custom `GenericmessageCommand::execute()` method.
The [`CancelCommand`](CancelCommand.php) allows users to cancel any active conversation.

258
Commands/Conversation/SurveyCommand.php

@ -0,0 +1,258 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* User "/survey" command
*
* Example of the Conversation functionality in form of a simple survey.
*/
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Conversation;
use Longman\TelegramBot\Entities\Keyboard;
use Longman\TelegramBot\Entities\KeyboardButton;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
class SurveyCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'survey';
/**
* @var string
*/
protected $description = 'Survey for bot users';
/**
* @var string
*/
protected $usage = '/survey';
/**
* @var string
*/
protected $version = '0.4.0';
/**
* @var bool
*/
protected $need_mysql = true;
/**
* @var bool
*/
protected $private_only = true;
/**
* Conversation Object
*
* @var Conversation
*/
protected $conversation;
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
$message = $this->getMessage();
$chat = $message->getChat();
$user = $message->getFrom();
$text = trim($message->getText(true));
$chat_id = $chat->getId();
$user_id = $user->getId();
// Preparing response
$data = [
'chat_id' => $chat_id,
// Remove any keyboard by default
'reply_markup' => Keyboard::remove(['selective' => true]),
];
if ($chat->isGroupChat() || $chat->isSuperGroup()) {
// Force reply is applied by default so it can work with privacy on
$data['reply_markup'] = Keyboard::forceReply(['selective' => true]);
}
// Conversation start
$this->conversation = new Conversation($user_id, $chat_id, $this->getName());
// Load any existing notes from this conversation
$notes = &$this->conversation->notes;
!is_array($notes) && $notes = [];
// Load the current state of the conversation
$state = $notes['state'] ?? 0;
$result = Request::emptyResponse();
// State machine
// Every time a step is achieved the state is updated
switch ($state) {
case 0:
if ($text === '') {
$notes['state'] = 0;
$this->conversation->update();
$data['text'] = 'Type your name:';
$result = Request::sendMessage($data);
break;
}
$notes['name'] = $text;
$text = '';
// No break!
case 1:
if ($text === '') {
$notes['state'] = 1;
$this->conversation->update();
$data['text'] = 'Type your surname:';
$result = Request::sendMessage($data);
break;
}
$notes['surname'] = $text;
$text = '';
// No break!
case 2:
if ($text === '' || !is_numeric($text)) {
$notes['state'] = 2;
$this->conversation->update();
$data['text'] = 'Type your age:';
if ($text !== '') {
$data['text'] = 'Age must be a number';
}
$result = Request::sendMessage($data);
break;
}
$notes['age'] = $text;
$text = '';
// No break!
case 3:
if ($text === '' || !in_array($text, ['M', 'F'], true)) {
$notes['state'] = 3;
$this->conversation->update();
$data['reply_markup'] = (new Keyboard(['M', 'F']))
->setResizeKeyboard(true)
->setOneTimeKeyboard(true)
->setSelective(true);
$data['text'] = 'Select your gender:';
if ($text !== '') {
$data['text'] = 'Choose a keyboard option to select your gender';
}
$result = Request::sendMessage($data);
break;
}
$notes['gender'] = $text;
// No break!
case 4:
if ($message->getLocation() === null) {
$notes['state'] = 4;
$this->conversation->update();
$data['reply_markup'] = (new Keyboard(
(new KeyboardButton('Share Location'))->setRequestLocation(true)
))
->setOneTimeKeyboard(true)
->setResizeKeyboard(true)
->setSelective(true);
$data['text'] = 'Share your location:';
$result = Request::sendMessage($data);
break;
}
$notes['longitude'] = $message->getLocation()->getLongitude();
$notes['latitude'] = $message->getLocation()->getLatitude();
// No break!
case 5:
if ($message->getPhoto() === null) {
$notes['state'] = 5;
$this->conversation->update();
$data['text'] = 'Insert your picture:';
$result = Request::sendMessage($data);
break;
}
$photo = $message->getPhoto()[0];
$notes['photo_id'] = $photo->getFileId();
// No break!
case 6:
if ($message->getContact() === null) {
$notes['state'] = 6;
$this->conversation->update();
$data['reply_markup'] = (new Keyboard(
(new KeyboardButton('Share Contact'))->setRequestContact(true)
))
->setOneTimeKeyboard(true)
->setResizeKeyboard(true)
->setSelective(true);
$data['text'] = 'Share your contact information:';
$result = Request::sendMessage($data);
break;
}
$notes['phone_number'] = $message->getContact()->getPhoneNumber();
// No break!
case 7:
$this->conversation->update();
$out_text = '/Survey result:' . PHP_EOL;
unset($notes['state']);
foreach ($notes as $k => $v) {
$out_text .= PHP_EOL . ucfirst($k) . ': ' . $v;
}
$data['photo'] = $notes['photo_id'];
$data['caption'] = $out_text;
$this->conversation->stop();
$result = Request::sendPhoto($data);
break;
}
return $result;
}
}

61
Commands/GenericCommand.php

@ -0,0 +1,61 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
/**
* Generic command
*
* Gets executed for generic commands, when no other appropriate one is found.
*/
class GenericCommand extends SystemCommand
{
/**
* @var string
*/
protected $name = 'generic';
/**
* @var string
*/
protected $description = 'Handles generic commands or is executed by default when a command is not found';
/**
* @var string
*/
protected $version = '1.1.0';
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
$message = $this->getMessage();
$user_id = $message->getFrom()->getId();
$command = $message->getCommand();
// To enable proper use of the /whois command.
// If the user is an admin and the command is in the format "/whoisXYZ", call the /whois command
if (stripos($command, 'whois') === 0 && $this->telegram->isAdmin($user_id)) {
return $this->telegram->executeCommand('whois');
}
return $this->replyToChat("Command /{$command} not found.. :(");
}
}

82
Commands/Group/GenericmessageCommand.php

@ -0,0 +1,82 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Generic message command
*
* Gets executed when any type of message is sent.
*
* In this group-related context, we can handle new and left group members.
*/
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
class GenericmessageCommand extends SystemCommand
{
/**
* @var string
*/
protected $name = 'genericmessage';
/**
* @var string
*/
protected $description = 'Handle generic message';
/**
* @var string
*/
protected $version = '1.0.0';
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
$message = $this->getMessage();
// Handle new chat members
if ($message->getNewChatMembers()) {
return $this->getTelegram()->executeCommand('newchatmembers');
}
// Handle left chat members
if ($message->getLeftChatMember()) {
return $this->getTelegram()->executeCommand('leftchatmember');
}
// The chat photo was changed
if ($new_chat_photo = $message->getNewChatPhoto()) {
// Whatever...
}
// The chat title was changed
if ($new_chat_title = $message->getNewChatTitle()) {
// Whatever...
}
// A message has been pinned
if ($pinned_message = $message->getPinnedMessage()) {
// Whatever...
}
return Request::emptyResponse();
}
}

58
Commands/Group/LeftchatmemberCommand.php

@ -0,0 +1,58 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Left chat member command
*
* Gets executed when a member leaves the chat.
*
* NOTE: This command must be called from GenericmessageCommand.php!
* It is only in a separate command file for easier code maintenance.
*/
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
class LeftchatmemberCommand extends SystemCommand
{
/**
* @var string
*/
protected $name = 'leftchatmember';
/**
* @var string
*/
protected $description = 'Left Chat Member';
/**
* @var string
*/
protected $version = '1.2.0';
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
$message = $this->getMessage();
$member = $message->getLeftChatMember();
return $this->replyToChat('Sorry to see you go, ' . $member->getFirstName());
}
}

67
Commands/Group/NewchatmembersCommand.php

@ -0,0 +1,67 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* New chat members command
*
* Gets executed when a new member joins the chat.
*
* NOTE: This command must be called from GenericmessageCommand.php!
* It is only in a separate command file for easier code maintenance.
*/
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
class NewchatmembersCommand extends SystemCommand
{
/**
* @var string
*/
protected $name = 'newchatmembers';
/**
* @var string
*/
protected $description = 'New Chat Members';
/**
* @var string
*/
protected $version = '1.3.0';
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
$message = $this->getMessage();
$members = $message->getNewChatMembers();
if ($message->botAddedInChat()) {
return $this->replyToChat('Hi there, you BOT!');
}
$member_names = [];
foreach ($members as $member) {
$member_names[] = $member->tryMention();
}
return $this->replyToChat('Hi ' . implode(', ', $member_names) . '!');
}
}

6
Commands/Group/README.md

@ -0,0 +1,6 @@
# Group or Channel
Requests specific to groups and channels all get handled in [`GenericmessageCommand.php`](GenericmessageCommand.php).
The two extra commands [`NewchatmembersCommand`](NewchatmembersCommand.php) and [`LeftchatmemberCommand`](LeftchatmemberCommand.php) are simply files that can be called as commands from within a command, not by a user.
Have a look at [`GenericmessageCommand.php`](GenericmessageCommand.php) to understand what you can do.

54
Commands/InlineMode/ChoseninlineresultCommand.php

@ -0,0 +1,54 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Chosen inline result command
*
* Gets executed when an item from an inline query is selected.
*/
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Entities\ServerResponse;
class ChoseninlineresultCommand extends SystemCommand
{
/**
* @var string
*/
protected $name = 'choseninlineresult';
/**
* @var string
*/
protected $description = 'Handle the chosen inline result';
/**
* @var string
*/
protected $version = '1.2.0';
/**
* Main command execution
*
* @return ServerResponse
*/
public function execute(): ServerResponse
{
// Information about the chosen result is returned.
$inline_query = $this->getChosenInlineResult();
$query = $inline_query->getQuery();
return parent::execute();
}
}

100
Commands/InlineMode/InlinequeryCommand.php

@ -0,0 +1,100 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Inline query command
*
* Command that handles inline queries and returns a list of results.
*/
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Entities\InlineQuery\InlineQueryResultArticle;
use Longman\TelegramBot\Entities\InlineQuery\InlineQueryResultContact;
use Longman\TelegramBot\Entities\InlineQuery\InlineQueryResultLocation;
use Longman\TelegramBot\Entities\InlineQuery\InlineQueryResultVenue;
use Longman\TelegramBot\Entities\InputMessageContent\InputTextMessageContent;
use Longman\TelegramBot\Entities\ServerResponse;
class InlinequeryCommand extends SystemCommand
{
/**
* @var string
*/
protected $name = 'inlinequery';
/**
* @var string
*/
protected $description = 'Handle inline query';
/**
* @var string
*/
protected $version = '1.2.0';
/**
* Main command execution
*
* @return ServerResponse
*/
public function execute(): ServerResponse
{
$inline_query = $this->getInlineQuery();
$query = $inline_query->getQuery();
$results = [];
if ($query !== '') {
// https://core.telegram.org/bots/api#inlinequeryresultarticle
$results[] = new InlineQueryResultArticle([
'id' => '001',
'title' => 'Simple text using InputTextMessageContent',
'description' => 'this will return Text',
// Here you can put any other Input...MessageContent you like.
// It will keep the style of an article, but post the specific message type back to the user.
'input_message_content' => new InputTextMessageContent([
'message_text' => 'The query that got you here: ' . $query,
]),
]);
// https://core.telegram.org/bots/api#inlinequeryresultcontact
$results[] = new InlineQueryResultContact([
'id' => '002',
'phone_number' => '12345678',
'first_name' => 'Best',
'last_name' => 'Friend',
]);
// https://core.telegram.org/bots/api#inlinequeryresultlocation
$results[] = new InlineQueryResultLocation([
'id' => '003',
'title' => 'The center of the world!',
'latitude' => 40.866667,
'longitude' => 34.566667,
]);
// https://core.telegram.org/bots/api#inlinequeryresultvenue
$results[] = new InlineQueryResultVenue([
'id' => '004',
'title' => 'No-Mans-Land',
'address' => 'In the middle of Nowhere',
'latitude' => 33,
'longitude' => -33,
]);
}
return $inline_query->answer($results);
}
}

7
Commands/InlineMode/README.md

@ -0,0 +1,7 @@
# Inline Mode
The files in this folder demonstrate how to use the [inline mode](https://core.telegram.org/bots/api#inline-mode) of your bot.
The [`InlinequeryCommand.php`](InlinequeryCommand.php) catches any inline queries and answers with a set of results.
When a result is selected, this selection is then handled by [`ChoseninlineresultCommand.php`](ChoseninlineresultCommand.php).

61
Commands/Keyboard/CallbackqueryCommand.php

@ -0,0 +1,61 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Callback query command
*
* This command handles all callback queries sent via inline keyboard buttons.
*
* @see InlinekeyboardCommand.php
*/
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Entities\ServerResponse;
class CallbackqueryCommand extends SystemCommand
{
/**
* @var string
*/
protected $name = 'callbackquery';
/**
* @var string
*/
protected $description = 'Handle the callback query';
/**
* @var string
*/
protected $version = '1.2.0';
/**
* Main command execution
*
* @return ServerResponse
* @throws \Exception
*/
public function execute(): ServerResponse
{
// Callback query data can be fetched and handled accordingly.
$callback_query = $this->getCallbackQuery();
$callback_data = $callback_query->getData();
return $callback_query->answer([
'text' => 'Content of the callback data: ' . $callback_data,
'show_alert' => (bool) random_int(0, 1), // Randomly show (or not) as an alert.
'cache_time' => 5,
]);
}
}

60
Commands/Keyboard/ForcereplyCommand.php

@ -0,0 +1,60 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\Keyboard;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
/**
* User "/forcereply" command
*
* Force a reply to a message.
*/
class ForcereplyCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'forcereply';
/**
* @var string
*/
protected $description = 'Force reply with reply markup';
/**
* @var string
*/
protected $usage = '/forcereply';
/**
* @var string
*/
protected $version = '0.2.0';
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
// Force a reply to the sent message
return $this->replyToChat('Write something in reply:', [
'reply_markup' => Keyboard::forceReply(),
]);
}
}

61
Commands/Keyboard/HidekeyboardCommand.php

@ -0,0 +1,61 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands\UserCommands;
/**
* User "/hidekeyboard" command
*
* Command to hide the keyboard.
*/
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\Keyboard;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
class HidekeyboardCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'hidekeyboard';
/**
* @var string
*/
protected $description = 'Hide the custom keyboard';
/**
* @var string
*/
protected $usage = '/hidekeyboard';
/**
* @var string
*/
protected $version = '0.2.0';
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
// Remove the keyboard and send a message
return $this->replyToChat('Keyboard Hidden', [
'reply_markup' => Keyboard::remove(),
]);
}
}

72
Commands/Keyboard/InlinekeyboardCommand.php

@ -0,0 +1,72 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands\UserCommands;
/**
* User "/inlinekeyboard" command
*
* Display an inline keyboard with a few buttons.
*
* This command requires CallbackqueryCommand to work!
*
* @see CallbackqueryCommand.php
*/
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\InlineKeyboard;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
class InlinekeyboardCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'inlinekeyboard';
/**
* @var string
*/
protected $description = 'Show inline keyboard';
/**
* @var string
*/
protected $usage = '/inlinekeyboard';
/**
* @var string
*/
protected $version = '0.2.0';
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
$inline_keyboard = new InlineKeyboard([
['text' => 'Inline Query (current chat)', 'switch_inline_query_current_chat' => 'inline query...'],
['text' => 'Inline Query (other chat)', 'switch_inline_query' => 'inline query...'],
], [
['text' => 'Callback', 'callback_data' => 'identifier'],
['text' => 'Open URL', 'url' => 'https://github.com/php-telegram-bot/example-bot'],
]);
return $this->replyToChat('Inline Keyboard', [
'reply_markup' => $inline_keyboard,
]);
}
}

102
Commands/Keyboard/KeyboardCommand.php

@ -0,0 +1,102 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands\UserCommands;
/**
* User "/keyboard" command
*
* Display a keyboard with a few buttons.
*/
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\Keyboard;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
class KeyboardCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'keyboard';
/**
* @var string
*/
protected $description = 'Show a custom keyboard with reply markup';
/**
* @var string
*/
protected $usage = '/keyboard';
/**
* @var string
*/
protected $version = '0.3.0';
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
/** @var Keyboard[] $keyboards */
$keyboards = [];
// Simple digits
$keyboards[] = new Keyboard(
['7', '8', '9'],
['4', '5', '6'],
['1', '2', '3'],
[' ', '0', ' ']
);
// Digits with operations
$keyboards[] = new Keyboard(
['7', '8', '9', '+'],
['4', '5', '6', '-'],
['1', '2', '3', '*'],
[' ', '0', ' ', '/']
);
// Short version with 1 button per row
$keyboards[] = new Keyboard('A', 'B', 'C');
// Some different ways of creating rows and buttons
$keyboards[] = new Keyboard(
['text' => 'A'],
'B',
['C', 'D']
);
// Buttons to perform Contact or Location sharing
$keyboards[] = new Keyboard([
['text' => 'Send my contact', 'request_contact' => true],
['text' => 'Send my location', 'request_location' => true],
]);
// Shuffle our example keyboards and return a random one
shuffle($keyboards);
$keyboard = end($keyboards)
->setResizeKeyboard(true)
->setOneTimeKeyboard(true)
->setSelective(false);
return $this->replyToChat('Press a Button!', [
'reply_markup' => $keyboard,
]);
}
}

13
Commands/Keyboard/README.md

@ -0,0 +1,13 @@
# Keyboards
The files in this folder demonstrate how to create normal and inline keyboards.
## Normal Keyboard
Have a look at [`KeyboardCommand.php`](KeyboardCommand.php) for usage examples.
## Inline Keyboard
Have a look at [`InlinekeyboardCommand.php`](InlinekeyboardCommand.php) for usage examples.
To handle inline keyboard buttons, you need to handle all callbacks inside [`CallbackqueryCommand.php`](CallbackqueryCommand.php).

53
Commands/Message/ChannelpostCommand.php

@ -0,0 +1,53 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Channel post command
*
* Gets executed when a new post is created in a channel.
*/
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Entities\ServerResponse;
class ChannelpostCommand extends SystemCommand
{
/**
* @var string
*/
protected $name = 'channelpost';
/**
* @var string
*/
protected $description = 'Handle channel post';
/**
* @var string
*/
protected $version = '1.1.0';
/**
* Main command execution
*
* @return ServerResponse
*/
public function execute(): ServerResponse
{
// Get the channel post
$channel_post = $this->getChannelPost();
return parent::execute();
}
}

53
Commands/Message/EditedchannelpostCommand.php

@ -0,0 +1,53 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Edited channel post command
*
* Gets executed when a post in a channel is edited.
*/
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Entities\ServerResponse;
class EditedchannelpostCommand extends SystemCommand
{
/**
* @var string
*/
protected $name = 'editedchannelpost';
/**
* @var string
*/
protected $description = 'Handle edited channel post';
/**
* @var string
*/
protected $version = '1.1.0';
/**
* Main command execution
*
* @return ServerResponse
*/
public function execute(): ServerResponse
{
// Get the edited channel post
$edited_channel_post = $this->getEditedChannelPost();
return parent::execute();
}
}

52
Commands/Message/EditedmessageCommand.php

@ -0,0 +1,52 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Entities\ServerResponse;
/**
* Edited message command
*
* Gets executed when a user message is edited.
*/
class EditedmessageCommand extends SystemCommand
{
/**
* @var string
*/
protected $name = 'editedmessage';
/**
* @var string
*/
protected $description = 'Handle edited message';
/**
* @var string
*/
protected $version = '1.2.0';
/**
* Main command execution
*
* @return ServerResponse
*/
public function execute(): ServerResponse
{
// Get the edited message
$edited_message = $this->getEditedMessage();
return parent::execute();
}
}

82
Commands/Message/EditmessageCommand.php

@ -0,0 +1,82 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* User "/editmessage" command
*
* Command to edit a message sent by the bot.
*/
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
class EditmessageCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'editmessage';
/**
* @var string
*/
protected $description = 'Edit a message sent by the bot';
/**
* @var string
*/
protected $usage = '/editmessage';
/**
* @var string
*/
protected $version = '1.1.0';
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$reply_to_message = $message->getReplyToMessage();
$text = $message->getText(true);
if ($reply_to_message && $message_to_edit = $reply_to_message->getMessageId()) {
// Try to edit the selected message.
$result = Request::editMessageText([
'chat_id' => $chat_id,
'message_id' => $message_to_edit,
'text' => $text ?: 'Edited message',
]);
// If successful, delete this editing reply message.
if ($result->isOk()) {
Request::deleteMessage([
'chat_id' => $chat_id,
'message_id' => $message->getMessageId(),
]);
}
return $result;
}
return $this->replyToChat(sprintf("Reply to any bots' message and use /%s <your text> to edit it.", $this->getName()));
}
}

61
Commands/Message/GenericmessageCommand.php

@ -0,0 +1,61 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Generic message command
*
* Gets executed when any type of message is sent.
*
* In this message-related context, we can handle any kind of message.
*/
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Request;
class GenericmessageCommand extends SystemCommand
{
/**
* @var string
*/
protected $name = 'genericmessage';
/**
* @var string
*/
protected $description = 'Handle generic message';
/**
* @var string
*/
protected $version = '1.0.0';
/**
* Main command execution
*
* @return ServerResponse
*/
public function execute(): ServerResponse
{
$message = $this->getMessage();
/**
* Handle any kind of message here
*/
$message_text = $message->getText(true);
return Request::emptyResponse();
}
}

15
Commands/Message/README.md

@ -0,0 +1,15 @@
# Message
You bot can handle all types of messages.
## Private and Group chats
Messages in private and group chats get handled by [`GenericmessageCommand.php`](GenericmessageCommand.php).
When a message gets edited, it gets handled by [`EditedmessageCommand.php`](EditedmessageCommand.php)
(Have a look at [`EditmessageCommand.php`](EditmessageCommand.php) for an example of how to edit messages via your bot)
## Channels
For channels, the messages (or posts) get handled by [`ChannelpostCommand.php`](ChannelpostCommand.php).
When a channel post gets edited, it gets handled by [`EditedchannelpostCommand.php`](EditedchannelpostCommand.php)

64
Commands/Other/EchoCommand.php

@ -0,0 +1,64 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* User "/echo" command
*
* Simply echo the input back to the user.
*/
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
class EchoCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'echo';
/**
* @var string
*/
protected $description = 'Show text';
/**
* @var string
*/
protected $usage = '/echo <text>';
/**
* @var string
*/
protected $version = '1.2.0';
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
$message = $this->getMessage();
$text = $message->getText(true);
if ($text === '') {
return $this->replyToChat('Command usage: ' . $this->getUsage());
}
return $this->replyToChat($text);
}
}

133
Commands/Other/HelpCommand.php

@ -0,0 +1,133 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* User "/help" command
*
* Command that lists all available commands and displays them in User and Admin sections.
*/
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Commands\Command;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
class HelpCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'help';
/**
* @var string
*/
protected $description = 'Show bot commands help';
/**
* @var string
*/
protected $usage = '/help or /help <command>';
/**
* @var string
*/
protected $version = '1.4.0';
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
$message = $this->getMessage();
$command_str = trim($message->getText(true));
// Admin commands shouldn't be shown in group chats
$safe_to_show = $message->getChat()->isPrivateChat();
[$all_commands, $user_commands, $admin_commands] = $this->getUserAndAdminCommands();
// If no command parameter is passed, show the list.
if ($command_str === '') {
$text = '*Commands List*:' . PHP_EOL;
foreach ($user_commands as $user_command) {
$text .= '/' . $user_command->getName() . ' - ' . $user_command->getDescription() . PHP_EOL;
}
if ($safe_to_show && count($admin_commands) > 0) {
$text .= PHP_EOL . '*Admin Commands List*:' . PHP_EOL;
foreach ($admin_commands as $admin_command) {
$text .= '/' . $admin_command->getName() . ' - ' . $admin_command->getDescription() . PHP_EOL;
}
}
$text .= PHP_EOL . 'For exact command help type: /help <command>';
return $this->replyToChat($text, ['parse_mode' => 'markdown']);
}
$command_str = str_replace('/', '', $command_str);
if (isset($all_commands[$command_str]) && ($safe_to_show || !$all_commands[$command_str]->isAdminCommand())) {
$command = $all_commands[$command_str];
return $this->replyToChat(sprintf(
'Command: %s (v%s)' . PHP_EOL .
'Description: %s' . PHP_EOL .
'Usage: %s',
$command->getName(),
$command->getVersion(),
$command->getDescription(),
$command->getUsage()
), ['parse_mode' => 'markdown']);
}
return $this->replyToChat('No help available: Command `/' . $command_str . '` not found', ['parse_mode' => 'markdown']);
}
/**
* Get all available User and Admin commands to display in the help list.
*
* @return Command[][]
* @throws TelegramException
*/
protected function getUserAndAdminCommands(): array
{
/** @var Command[] $all_commands */
$all_commands = $this->telegram->getCommandsList();
// Only get enabled Admin and User commands that are allowed to be shown.
$commands = array_filter($all_commands, function ($command): bool {
return !$command->isSystemCommand() && $command->showInHelp() && $command->isEnabled();
});
// Filter out all User commands
$user_commands = array_filter($commands, function ($command): bool {
return $command->isUserCommand();
});
// Filter out all Admin commands
$admin_commands = array_filter($commands, function ($command): bool {
return $command->isAdminCommand();
});
ksort($commands);
ksort($user_commands);
ksort($admin_commands);
return [$commands, $user_commands, $admin_commands];
}
}

110
Commands/Other/ImageCommand.php

@ -0,0 +1,110 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* User "/image" command
*
* Randomly fetch any uploaded image from the Uploads path and send it to the user.
*/
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
class ImageCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'image';
/**
* @var string
*/
protected $description = 'Randomly fetch any uploaded image';
/**
* @var string
*/
protected $usage = '/image';
/**
* @var string
*/
protected $version = '1.2.0';
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
$message = $this->getMessage();
// Use any extra parameters as the caption text.
$caption = trim($message->getText(true));
// Make sure the Upload path has been defined and exists.
$upload_path = $this->telegram->getUploadPath();
if (!is_dir($upload_path)) {
return $this->replyToChat('Upload path has not been defined or does not exist.');
}
// Get a random picture from the Upload path.
$random_image = $this->getRandomImagePath($upload_path);
if ('' === $random_image) {
return $this->replyToChat('No image found!');
}
// If no caption is set, use the filename.
if ('' === $caption) {
$caption = basename($random_image);
}
return Request::sendPhoto([
'chat_id' => $message->getFrom()->getId(),
'caption' => $caption,
'photo' => $random_image,
]);
}
/**
* Return the path to a random image in the passed directory.
*
* @param string $dir
*
* @return string
*/
private function getRandomImagePath($dir): string
{
if (!is_dir($dir)) {
return '';
}
// Filter the file list to only return images.
$image_list = array_filter(scandir($dir), function ($file) {
$extension = pathinfo($file, PATHINFO_EXTENSION);
return in_array($extension, ['png', 'jpg', 'jpeg', 'gif']);
});
if (!empty($image_list)) {
shuffle($image_list);
return $dir . '/' . $image_list[0];
}
return '';
}
}

68
Commands/Other/MarkdownCommand.php

@ -0,0 +1,68 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* User "/markdown" command
*
* Print some text formatted with markdown.
*/
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\ReplyKeyboardMarkup;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
class MarkdownCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'markdown';
/**
* @var string
*/
protected $description = 'Print Markdown Text';
/**
* @var string
*/
protected $usage = '/markdown';
/**
* @var string
*/
protected $version = '1.1.0';
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
return $this->replyToChat('
*bold* _italic_ `inline fixed width code`
```
preformatted code block
code block
```
[Best Telegram bot api!!](https://github.com/php-telegram-bot/core)', [
'parse_mode' => 'markdown',
]);
}
}

5
Commands/Other/README.md

@ -0,0 +1,5 @@
# Other Commands
In this folder you can find a few example commands, that demonstrate how to use different features of the library.
Best just take a look at them and see what they do!

67
Commands/Other/SlapCommand.php

@ -0,0 +1,67 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* User "/slap" command
*
* Slap a user around with a big trout!
*/
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
class SlapCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'slap';
/**
* @var string
*/
protected $description = 'Slap someone with their username';
/**
* @var string
*/
protected $usage = '/slap <@user>';
/**
* @var string
*/
protected $version = '1.2.0';
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
$message = $this->getMessage();
$text = $message->getText(true);
$sender = '@' . $message->getFrom()->getUsername();
// Username validation (simply checking for `@something` in the text)
if (0 === preg_match('/@[\w_]{5,}/', $text)) {
return $this->replyToChat('Sorry, no one to slap around...');
}
return $this->replyToChat($sender . ' slaps ' . $text . ' around a bit with a large trout');
}
}

115
Commands/Other/UploadCommand.php

@ -0,0 +1,115 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* User "/upload" command
*
* A command that allows users to upload files to your bot, saving them to the bot's "Download" folder.
*
* IMPORTANT NOTICE
* This is a "demo", do NOT use this as-is in your bot!
* Know the security implications of allowing users to upload arbitrary files to your server!
*/
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Conversation;
use Longman\TelegramBot\Entities\Keyboard;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
class UploadCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'upload';
/**
* @var string
*/
protected $description = 'Upload and save files';
/**
* @var string
*/
protected $usage = '/upload';
/**
* @var string
*/
protected $version = '0.2.0';
/**
* @var bool
*/
protected $need_mysql = true;
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
$message = $this->getMessage();
$chat = $message->getChat();
$chat_id = $chat->getId();
$user_id = $message->getFrom()->getId();
// Make sure the Download path has been defined and exists
$download_path = $this->telegram->getDownloadPath();
if (!is_dir($download_path)) {
return $this->replyToChat('Download path has not been defined or does not exist.');
}
// Initialise the data array for the response
$data = ['chat_id' => $chat_id];
if ($chat->isGroupChat() || $chat->isSuperGroup()) {
// Reply to message id is applied by default
$data['reply_to_message_id'] = $message->getMessageId();
// Force reply is applied by default to work with privacy on
$data['reply_markup'] = Keyboard::forceReply(['selective' => true]);
}
// Start conversation
$conversation = new Conversation($user_id, $chat_id, $this->getName());
$message_type = $message->getType();
if (in_array($message_type, ['audio', 'document', 'photo', 'video', 'voice'], true)) {
$doc = $message->{'get' . ucfirst($message_type)}();
// For photos, get the best quality!
($message_type === 'photo') && $doc = end($doc);
$file_id = $doc->getFileId();
$file = Request::getFile(['file_id' => $file_id]);
if ($file->isOk() && Request::downloadFile($file->getResult())) {
$data['text'] = $message_type . ' file is located at: ' . $download_path . '/' . $file->getResult()->getFilePath();
} else {
$data['text'] = 'Failed to download.';
}
$conversation->notes['file_id'] = $file_id;
$conversation->update();
$conversation->stop();
} else {
$data['text'] = 'Please upload the file now';
}
return Request::sendMessage($data);
}
}

124
Commands/Other/WhoamiCommand.php

@ -0,0 +1,124 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* User "/whoami" command
*
* Simple command that returns info about the current user.
*/
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\ChatAction;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Entities\UserProfilePhotos;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
class WhoamiCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'whoami';
/**
* @var string
*/
protected $description = 'Show your id, name and username';
/**
* @var string
*/
protected $usage = '/whoami';
/**
* @var string
*/
protected $version = '1.2.0';
/**
* @var bool
*/
protected $private_only = true;
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
$message = $this->getMessage();
$from = $message->getFrom();
$user_id = $from->getId();
$chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId();
$data = [
'chat_id' => $chat_id,
'reply_to_message_id' => $message_id,
];
// Send chat action "typing..."
Request::sendChatAction([
'chat_id' => $chat_id,
'action' => ChatAction::TYPING,
]);
$caption = sprintf(
'Your Id: %d' . PHP_EOL .
'Name: %s %s' . PHP_EOL .
'Username: %s',
$user_id,
$from->getFirstName(),
$from->getLastName(),
$from->getUsername()
);
// Fetch the most recent user profile photo
$limit = 1;
$offset = null;
$user_profile_photos_response = Request::getUserProfilePhotos([
'user_id' => $user_id,
'limit' => $limit,
'offset' => $offset,
]);
if ($user_profile_photos_response->isOk()) {
/** @var UserProfilePhotos $user_profile_photos */
$user_profile_photos = $user_profile_photos_response->getResult();
if ($user_profile_photos->getTotalCount() > 0) {
$photos = $user_profile_photos->getPhotos();
// Get the best quality of the profile photo
$photo = end($photos[0]);
$file_id = $photo->getFileId();
$data['photo'] = $file_id;
$data['caption'] = $caption;
return Request::sendPhoto($data);
}
}
// No Photo just send text
$data['text'] = $caption;
return Request::sendMessage($data);
}
}

60
Commands/Payments/GenericmessageCommand.php

@ -0,0 +1,60 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Generic message command
*
* Gets executed when any type of message is sent.
*/
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Commands\UserCommands\PaymentCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Request;
class GenericmessageCommand extends SystemCommand
{
/**
* @var string
*/
protected $name = 'genericmessage';
/**
* @var string
*/
protected $description = 'Handle generic message';
/**
* @var string
*/
protected $version = '0.1.0';
/**
* Main command execution
*
* @return ServerResponse
*/
public function execute(): ServerResponse
{
$message = $this->getMessage();
$user_id = $message->getFrom()->getId();
// Handle successful payment
if ($payment = $message->getSuccessfulPayment()) {
return PaymentCommand::handleSuccessfulPayment($payment, $user_id);
}
return Request::emptyResponse();
}
}

125
Commands/Payments/PaymentCommand.php

@ -0,0 +1,125 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* User "/payment" command
*
* This command creates an invoice for the user using the Telegram Payments.
*
* You will have to set up a payment provider with @BotFather
* Select your bot and then "Payments". Then choose the provider of your choice.
*
* @BotFather will then present you with a payment provider token.
*
* Copy this token and set it in your config.php file:
* ['commands']['configs']['payment'] => ['payment_provider_token' => 'your_payment_provider_token_here']
*
* You will also need to copy the `Precheckoutquerycommand.php` file.
*/
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\Payments\LabeledPrice;
use Longman\TelegramBot\Entities\Payments\SuccessfulPayment;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request;
class PaymentCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'payment';
/**
* @var string
*/
protected $description = 'Create an invoice for the user using Telegram Payments';
/**
* @var string
*/
protected $usage = '/payment';
/**
* @var string
*/
protected $version = '0.1.0';
/**
* Main command execution
*
* @return ServerResponse
*/
public function execute(): ServerResponse
{
// Who to send this invoice to. (Use the current user.)
$chat_id = $this->getMessage()->getFrom()->getId();
// The currency of this invoice.
// Supported currencies: https://core.telegram.org/bots/payments#supported-currencies
$currency = 'EUR';
// List all items that will be shown on your invoice.
// Amounts are in cents. So 1 Euro would be put as 100.
$prices = [
new LabeledPrice(['label' => 'Small thing', 'amount' => 100]), // 1€
new LabeledPrice(['label' => 'Bigger thing', 'amount' => 2000]), // 20€
new LabeledPrice(['label' => 'Huge thing', 'amount' => 50000]), // 500€
];
// Request a shipping address if necessary.
$need_shipping_address = false;
// If you have flexible pricing, depending on the shipping method chosen, set this to true.
// You will also need to copy and adapt the `ShippingqueryCommand.php` file.
$is_flexible = false;
// Send the actual invoice!
// Adjust any parameters to your needs.
return Request::sendInvoice([
'chat_id' => $chat_id,
'title' => 'Payment with PHP Telegram Bot',
'description' => 'A simple invoice to test Telegram Payments',
'payload' => 'payment_demo',
'start_parameter' => 'payment_demo',
'provider_token' => $this->getConfig('payment_provider_token'),
'currency' => $currency,
'prices' => $prices,
'need_shipping_address' => $need_shipping_address,
'is_flexible' => $is_flexible,
]);
}
/**
* Send "Thank you" message to user who paid
*
* You will need to add some code to your custom `GenericmessageCommand::execute()` method.
* Check the `GenericmessageCommand.php` file included in this folder.
*
* @param SuccessfulPayment $payment
* @param int $user_id
*
* @return ServerResponse
* @throws TelegramException
*/
public static function handleSuccessfulPayment($payment, $user_id): ServerResponse
{
// Send a message to the user after they have completed the payment.
return Request::sendMessage([
'chat_id' => $user_id,
'text' => 'Thank you for your order!',
]);
}
}

57
Commands/Payments/PrecheckoutqueryCommand.php

@ -0,0 +1,57 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Pre-checkout query required for "/payment" command
*
* In this command you can perform any necessary verifications and checks
* to allow or disallow the final checkout and payment of the invoice.
*/
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Entities\ServerResponse;
class PrecheckoutqueryCommand extends SystemCommand
{
/**
* @var string
*/
protected $name = 'precheckoutquery';
/**
* @var string
*/
protected $description = 'Pre-Checkout Query Handler';
/**
* @var string
*/
protected $version = '0.1.0';
/**
* Main command execution
*
* @return ServerResponse
*/
public function execute(): ServerResponse
{
// Simply approve, no need for any checks at this point.
return $this->getPreCheckoutQuery()->answer(true);
// If we do make certain checks, you can define the error message displayed to the user like this.
// return $this->getPreCheckoutQuery()->answer(false, [
// 'error_message' => 'Registration (or whatever) required...',
// ]);
}
}

39
Commands/Payments/README.md

@ -0,0 +1,39 @@
# Telegram Payments
With the files in this folder, you can create and send invoices to your users, require their shipping details, define custom / flexible shipping methods and send a confirmation message after payment.
## Enable payments for your bot
Read all about Telegram Payments and follow the setup guide here:
https://core.telegram.org/bots/payments
## Configuring the `/payment` command
First of all, as a bare minimum, you need to copy the [`PaymentCommand.php`](PaymentCommand.php) and [`PrecheckoutqueryCommand.php`](PrecheckoutqueryCommand.php) files in this folder to your custom commands folder.
If you want to allow flexible shipping options, you will also need to copy [`ShippingqueryCommand.php`](ShippingqueryCommand.php) to your custom commands folder.
Should you want to send a message on a successful payment, you will need to copy the [`GenericmessageCommand.php`](GenericmessageCommand.php) file as well.
If you already have a `GenericmessageCommand.php` file, you'll need to copy the code from the `execute` method into your file.
Next, you will need to add the Payment Provider Token (that you received in the previous step when linking your bot), to your `hook.php` or `manager.php` config.
For `hook.php`:
```php
$telegram->setCommandConfig('payment', ['payment_provider_token' => 'your_payment_provider_token_here']);
```
For `manager.php` or using a general `config.php`, in the config array add:
```php
...
'commands' => [
'configs' => [
'payment' => ['payment_provider_token' => 'your_payment_provider_token_here'],
],
],
...
```
Now, when sending the `/payment` command to your bot, you should receive an invoice.
Have fun with Telegram Payments!

80
Commands/Payments/ShippingqueryCommand.php

@ -0,0 +1,80 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Shipping query required for "/payment" command with flexible shipping method.
*
* In this command, you can perform any necessary verifications and checks
* to adjust the available shipping options of the payment.
*
* For example, if the user has a "Free Delivery" subscription or something like that.
*/
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Entities\Payments\LabeledPrice;
use Longman\TelegramBot\Entities\Payments\ShippingOption;
use Longman\TelegramBot\Entities\ServerResponse;
class ShippingqueryCommand extends SystemCommand
{
/**
* @var string
*/
protected $name = 'shippingquery';
/**
* @var string
*/
protected $description = 'Shipping Query Handler';
/**
* @var string
*/
protected $version = '0.1.0';
/**
* Main command execution
*
* @return ServerResponse
*/
public function execute(): ServerResponse
{
// Here you can check the shipping details and adjust the Shipping Options accordingly.
// For this demo, let's simply define some fixed shipping options, a "Basic" and "Premium" shipping method.
return $this->getShippingQuery()->answer(true, [
'shipping_options' => [
new ShippingOption([
'id' => 'basic',
'title' => 'Basic Shipping',
'prices' => [
new LabeledPrice(['label' => 'Basic Shipping', 'amount' => 800]),
],
]),
new ShippingOption([
'id' => 'premium',
'title' => 'Premium Shipping',
'prices' => [
new LabeledPrice(['label' => 'Premium Shipping', 'amount' => 1500]),
new LabeledPrice(['label' => 'Extra speedy', 'amount' => 300]),
],
]),
],
]);
// If we do make certain checks, you can define the error message displayed to the user like this.
// return $this->getShippingQuery()->answer(false, [
// 'error_message' => 'We do not ship to your location :-(',
// ]);
}
}

25
Commands/README.MD

@ -0,0 +1,25 @@
This Folder contains some examples how to use the Telegram Bot API with the Telegram PHP Bot library
**:exclamation: DO NOT USE THE COMMANDS FOLDER**
As the title says, do not include the Commands folder into your config to use it as it is.
Why? This folder contains some files multiple times which is an issue for the bot software, since every file can only exist once!
For example: GenericmessageCommand.php
This file exist in the following folder:
- Conversation
- Group
- Message
- Payments
Having any Command file more than once will cause conflicts between those file, causing only one file to be executed and the others ignored.
Please copy each file and/or folder that you need into the CustomCommans folder.
If you want to create your own Command file, please do it so in the CustomCommands folder as well.
If you need for example the GenericmessageCommand.php from the Conversation and Group folder, you will need to compara and merge those files.

74
Commands/ServiceMessages/GenericmessageCommand.php

@ -0,0 +1,74 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Generic message command
*
* Gets executed when any type of message is sent.
*
* In this service-message-related context, we can handle any incoming service-messages.
*/
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Request;
class GenericmessageCommand extends SystemCommand
{
/**
* @var string
*/
protected $name = 'genericmessage';
/**
* @var string
*/
protected $description = 'Handle generic message';
/**
* @var string
*/
protected $version = '1.0.0';
/**
* Main command execution
*
* @return ServerResponse
*/
public function execute(): ServerResponse
{
$message = $this->getMessage();
/**
* Catch and handle any service messages here.
*/
// The chat photo was deleted
$delete_chat_photo = $message->getDeleteChatPhoto();
// The group has been created
$group_chat_created = $message->getGroupChatCreated();
// The supergroup has been created
$supergroup_chat_created = $message->getSupergroupChatCreated();
// The channel has been created
$channel_chat_created = $message->getChannelChatCreated();
// Information about the payment
$successful_payment = $message->getSuccessfulPayment();
return Request::emptyResponse();
}
}

72
Commands/StartCommand.php

@ -0,0 +1,72 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Start command
*
* Gets executed when a user first starts using the bot.
*
* When using deep-linking, the parameter can be accessed by getting the command text.
*
* @see https://core.telegram.org/bots#deep-linking
*/
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
class StartCommand extends SystemCommand
{
/**
* @var string
*/
protected $name = 'start';
/**
* @var string
*/
protected $description = 'Start command';
/**
* @var string
*/
protected $usage = '/start';
/**
* @var string
*/
protected $version = '1.2.0';
/**
* @var bool
*/
protected $private_only = true;
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
// If you use deep-linking, get the parameter like this:
// $deep_linking_parameter = $this->getMessage()->getText(true);
return $this->replyToChat(
'Hi there!' . PHP_EOL .
'Type /help to see all commands!'
);
}
}

13
CustomCommands/README.md

@ -0,0 +1,13 @@
# CustomCommands
Uncomment line 47 in your config.php from
`// __DIR__ . '/CustomCommands',`
to
`__DIR__ . '/CustomCommands',`
to enable this Folder.
You then can copy example Commands from the Command folder.
**:exclamation: Important!**
DO NOT COPY THE ENTIRE COMMAND FOLDER CONTENT!!
Some Command Files are Duplicated and may interfere with each other.

13
README.md

@ -0,0 +1,13 @@
# telegram-bot
### 创建 Bot
发送 `/newbot`到`@BotFather`,根据提示完成创建机器人。
### 开发环境
- PHP >= 7.3
- composer
- 境外服务器 (https)
- MySQL(非必要)
- 依赖:longman/telegram-bot

31
composer.json

@ -0,0 +1,31 @@
{
"name": "telegram-bot/example-bot",
"type": "project",
"description": "PHP Telegram Bot Example",
"keywords": ["telegram", "bot", "example"],
"license": "MIT",
"homepage": "https://github.com/php-telegram-bot/example-bot",
"support": {
"issues": "https://github.com/php-telegram-bot/example-bot/issues",
"source": "https://github.com/php-telegram-bot/example-bot"
},
"authors": [
{
"name": "PHP Telegram Bot Team",
"homepage": "https://github.com/php-telegram-bot/example-bot/graphs/contributors",
"role": "Developer"
}
],
"require": {
"longman/telegram-bot": "*"
},
"require-dev": {
"php-parallel-lint/php-parallel-lint": "^1.2",
"squizlabs/php_codesniffer": "^3.5"
},
"scripts": {
"check-code": [
"\"vendor/bin/phpcs\""
]
}
}

92
config.php

@ -0,0 +1,92 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* This file contains all the configuration options for the PHP Telegram Bot.
*
* It is based on the configuration array of the PHP Telegram Bot Manager project.
*
* Simply adjust all the values that you need and extend where necessary.
*
* Options marked as [Manager Only] are only required if you use `manager.php`.
*
* For a full list of all options, check the Manager Readme:
* https://github.com/php-telegram-bot/telegram-bot-manager#set-extra-bot-parameters
*/
return [
// Add you bot's API key and name
'api_key' => 'your:bot_api_key',
'bot_username' => 'username_bot', // Without "@"
// [Manager Only] Secret key required to access the webhook
'secret' => 'super_secret',
// When using the getUpdates method, this can be commented out
'webhook' => [
'url' => 'https://your-domain/path/to/hook-or-manager.php',
// Use self-signed certificate
// 'certificate' => __DIR__ . '/path/to/your/certificate.crt',
// Limit maximum number of connections
// 'max_connections' => 5,
],
// All command related configs go here
'commands' => [
// Define all paths for your custom commands
// DO NOT PUT THE COMMAND FOLDER THERE. IT WILL NOT WORK.
// Copy each needed Commandfile into the CustomCommand folder and uncommend the Line 49 below
'paths' => [
// __DIR__ . '/CustomCommands',
],
// Here you can set any command-specific parameters
'configs' => [
// - Google geocode/timezone API key for /date command (see DateCommand.php)
// 'date' => ['google_api_key' => 'your_google_api_key_here'],
// - OpenWeatherMap.org API key for /weather command (see WeatherCommand.php)
// 'weather' => ['owm_api_key' => 'your_owm_api_key_here'],
// - Payment Provider Token for /payment command (see Payments/PaymentCommand.php)
// 'payment' => ['payment_provider_token' => 'your_payment_provider_token_here'],
],
],
// Define all IDs of admin users
'admins' => [
// 123,
],
// Enter your MySQL database credentials
// 'mysql' => [
// 'host' => '127.0.0.1',
// 'user' => 'root',
// 'password' => 'root',
// 'database' => 'telegram_bot',
// ],
// Logging (Debug, Error and Raw Updates)
// 'logging' => [
// 'debug' => __DIR__ . '/php-telegram-bot-debug.log',
// 'error' => __DIR__ . '/php-telegram-bot-error.log',
// 'update' => __DIR__ . '/php-telegram-bot-update.log',
// ],
// Set custom Upload and Download paths
'paths' => [
'download' => __DIR__ . '/Download',
'upload' => __DIR__ . '/Upload',
],
// Requests Limiter (tries to prevent reaching Telegram API limits)
'limiter' => [
'enabled' => true,
],
];

50
cron.php

@ -0,0 +1,50 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* This file is used to run a list of commands with crontab.
*/
// Your command(s) to run, pass them just like in a message (arguments supported)
$commands = [
'/whoami',
"/echo I'm a bot!",
];
// Load composer
require_once __DIR__ . '/vendor/autoload.php';
// Load all configuration options
/** @var array $config */
$config = require __DIR__ . '/config.php';
try {
// Create Telegram API object
$telegram = new Longman\TelegramBot\Telegram($config['api_key'], $config['bot_username']);
/**
* Check `hook.php` for configuration code to be added here.
*/
// Run user selected commands
$telegram->runCommands($commands);
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
// Log telegram errors
Longman\TelegramBot\TelegramLog::error($e);
// Uncomment this to output any errors (ONLY FOR DEVELOPMENT!)
// echo $e;
} catch (Longman\TelegramBot\Exception\TelegramLogException $e) {
// Uncomment this to output log initialisation errors (ONLY FOR DEVELOPMENT!)
// echo $e;
}

53
getUpdatesCLI.php

@ -0,0 +1,53 @@
#!/usr/bin/env php
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* This file is used to run the bot with the getUpdates method.
*/
// Load composer
require_once __DIR__ . '/vendor/autoload.php';
// Load all configuration options
/** @var array $config */
$config = require __DIR__ . '/config.php';
try {
// Create Telegram API object
$telegram = new Longman\TelegramBot\Telegram($config['api_key'], $config['bot_username']);
/**
* Check `hook.php` for configuration code to be added here.
*/
// Handle telegram getUpdates request
$server_response = $telegram->handleGetUpdates();
if ($server_response->isOk()) {
$update_count = count($server_response->getResult());
echo date('Y-m-d H:i:s') . ' - Processed ' . $update_count . ' updates';
} else {
echo date('Y-m-d H:i:s') . ' - Failed to fetch updates' . PHP_EOL;
echo $server_response->printError();
}
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
// Log telegram errors
Longman\TelegramBot\TelegramLog::error($e);
// Uncomment this to output any errors (ONLY FOR DEVELOPMENT!)
// echo $e;
} catch (Longman\TelegramBot\Exception\TelegramLogException $e) {
// Uncomment this to output log initialisation errors (ONLY FOR DEVELOPMENT!)
// echo $e;
}

78
hook.php

@ -0,0 +1,78 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* This configuration file is used to run the bot with the webhook method.
*
* Please note that if you open this file with your browser you'll get the "Input is empty!" Exception.
* This is perfectly normal and expected, because the hook URL has to be reached only by the Telegram servers.
*/
// Load composer
require_once __DIR__ . '/vendor/autoload.php';
// Load all configuration options
/** @var array $config */
$config = require __DIR__ . '/config.php';
try {
// Create Telegram API object
$telegram = new Longman\TelegramBot\Telegram($config['api_key'], $config['bot_username']);
// Enable admin users
$telegram->enableAdmins($config['admins']);
// Add commands paths containing your custom commands
$telegram->addCommandsPaths($config['commands']['paths']);
// Enable MySQL if required
// $telegram->enableMySql($config['mysql']);
// Logging (Error, Debug and Raw Updates)
// https://github.com/php-telegram-bot/core/blob/master/doc/01-utils.md#logging
//
// (this example requires Monolog: composer require monolog/monolog)
// Longman\TelegramBot\TelegramLog::initialize(
// new Monolog\Logger('telegram_bot', [
// (new Monolog\Handler\StreamHandler($config['logging']['debug'], Monolog\Logger::DEBUG))->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true)),
// (new Monolog\Handler\StreamHandler($config['logging']['error'], Monolog\Logger::ERROR))->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true)),
// ]),
// new Monolog\Logger('telegram_bot_updates', [
// (new Monolog\Handler\StreamHandler($config['logging']['update'], Monolog\Logger::INFO))->setFormatter(new Monolog\Formatter\LineFormatter('%message%' . PHP_EOL)),
// ])
// );
// Set custom Download and Upload paths
// $telegram->setDownloadPath($config['paths']['download']);
// $telegram->setUploadPath($config['paths']['upload']);
// Load all command-specific configurations
// foreach ($config['commands']['configs'] as $command_name => $command_config) {
// $telegram->setCommandConfig($command_name, $command_config);
// }
// Requests Limiter (tries to prevent reaching Telegram API limits)
$telegram->enableLimiter($config['limiter']);
// Handle telegram webhook request
$telegram->handle();
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
// Log telegram errors
Longman\TelegramBot\TelegramLog::error($e);
// Uncomment this to output any errors (ONLY FOR DEVELOPMENT!)
// echo $e;
} catch (Longman\TelegramBot\Exception\TelegramLogException $e) {
// Uncomment this to output log initialisation errors (ONLY FOR DEVELOPMENT!)
// echo $e;
}

42
manager.php

@ -0,0 +1,42 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* This configuration file is used as the main script for the PHP Telegram Bot Manager.
*
* For the full list of options, go to:
* https://github.com/php-telegram-bot/telegram-bot-manager#set-extra-bot-parameters
*/
// Load composer
require_once __DIR__ . '/vendor/autoload.php';
// Load all configuration options
/** @var array $config */
$config = require __DIR__ . '/config.php';
try {
$bot = new TelegramBot\TelegramBotManager\BotManager($config);
// Run the bot!
$bot->run();
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
// Log telegram errors
Longman\TelegramBot\TelegramLog::error($e);
// Uncomment this to output any errors (ONLY FOR DEVELOPMENT!)
// echo $e;
} catch (Longman\TelegramBot\Exception\TelegramLogException $e) {
// Uncomment this to output log initialisation errors (ONLY FOR DEVELOPMENT!)
// echo $e;
}

26
phpcs.xml.dist

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="php-telegram-bot-example-bot-ruleset">
<description>PHP Code Sniffer</description>
<arg value="snp"/>
<arg name="colors"/>
<arg name="parallel" value="8"/>
<arg name="encoding" value="utf-8"/>
<arg name="report-width" value="150"/>
<arg name="extensions" value="php"/>
<file>.</file>
<exclude-pattern>*/vendor/*</exclude-pattern>
<rule ref="PSR12"/>
<rule ref="Generic.CodeAnalysis.EmptyStatement">
<!-- Allow empty statements for explanation comments -->
<exclude name="Generic.CodeAnalysis.EmptyStatement"/>
</rule>
<rule ref="Squiz.WhiteSpace.ControlStructureSpacing">
<!-- Allow the empty line to improve readability -->
<exclude name="Squiz.WhiteSpace.ControlStructureSpacing.SpacingBeforeClose"/>
</rule>
</ruleset>

42
set.php

@ -0,0 +1,42 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* This file is used to set the webhook.
*/
// Load composer
require_once __DIR__ . '/vendor/autoload.php';
// Load all configuration options
/** @var array $config */
$config = require __DIR__ . '/config.php';
try {
// Create Telegram API object
$telegram = new Longman\TelegramBot\Telegram($config['api_key'], $config['bot_username']);
/**
* REMEMBER to define the URL to your hook.php file in:
* config.php: ['webhook']['url'] => 'https://your-domain/path/to/hook.php'
*/
// Set the webhook
$result = $telegram->setWebhook($config['webhook']['url']);
// To use a self-signed certificate, use this line instead
// $result = $telegram->setWebhook($config['webhook']['url'], ['certificate' => $config['webhook']['certificate']]);
echo $result->getDescription();
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
echo $e->getMessage();
}

34
unset.php

@ -0,0 +1,34 @@
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* This file is used to unset / delete the webhook.
*/
// Load composer
require_once __DIR__ . '/vendor/autoload.php';
// Load all configuration options
/** @var array $config */
$config = require __DIR__ . '/config.php';
try {
// Create Telegram API object
$telegram = new Longman\TelegramBot\Telegram($config['api_key'], $config['bot_username']);
// Unset / delete the webhook
$result = $telegram->deleteWebhook();
echo $result->getDescription();
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
echo $e->getMessage();
}
Loading…
Cancel
Save