Skip to content

Commit 119c7ef

Browse files
committed
Add a new /id command, add available commands to readme.
1 parent 46b0a82 commit 119c7ef

File tree

5 files changed

+179
-3
lines changed

5 files changed

+179
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
66
## [Unreleased]
77
### Added
88
- Description for commands.
9+
- `/id` command, to help users find their user and chat information.
910
### Changed
1011
### Deprecated
1112
### Removed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Assistant bot for the [PHP Telegram Bot Support] group.
55
## Commands
66

77
- `/help`: A short description of the bot and a list of all available commands.
8+
- `/id`: Display the user and chat information. Also, try forwarding any message from a channel to display the channel information.
89
- `/rules`: Show the rules that apply in the support group.
910

1011
[PHP Telegram Bot Support]: https://t.me/PHP_Telegram_Bot_Support "@PHP_Telegram_Bot_Support"

commands/ChannelpostCommand.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the PHP Telegram Support Bot.
5+
*
6+
* (c) PHP Telegram Bot Team (https://github.com/php-telegram-bot)
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Longman\TelegramBot\Commands\SystemCommands;
15+
16+
use Longman\TelegramBot\Commands\SystemCommand;
17+
use Longman\TelegramBot\Entities\ServerResponse;
18+
use Longman\TelegramBot\Exception\TelegramException;
19+
use Longman\TelegramBot\Request;
20+
21+
/**
22+
* Handle post sent from channel.
23+
*/
24+
class ChannelpostCommand extends SystemCommand
25+
{
26+
/**
27+
* @var string
28+
*/
29+
protected $name = 'channelpost';
30+
31+
/**
32+
* @var string
33+
*/
34+
protected $description = 'Handle Channel Post';
35+
36+
/**
37+
* @var string
38+
*/
39+
protected $version = '0.1.0';
40+
41+
/**
42+
* @inheritdoc
43+
* @throws TelegramException
44+
*/
45+
public function execute(): ServerResponse
46+
{
47+
if ($this->getChannelPost()->getCommand() === 'id') {
48+
return $this->getTelegram()->executeCommand('id');
49+
}
50+
51+
return Request::emptyResponse();
52+
}
53+
}

commands/GenericmessageCommand.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Longman\TelegramBot\Commands\SystemCommand;
1717
use Longman\TelegramBot\Entities\ServerResponse;
1818
use Longman\TelegramBot\Exception\TelegramException;
19-
use Longman\TelegramBot\Request;
2019

2120
/**
2221
* Generic message command
@@ -36,7 +35,7 @@ class GenericmessageCommand extends SystemCommand
3635
/**
3736
* @var string
3837
*/
39-
protected $version = '0.1.0';
38+
protected $version = '0.2.0';
4039

4140
/**
4241
* Execute command
@@ -51,6 +50,11 @@ public function execute(): ServerResponse
5150
return $this->getTelegram()->executeCommand('newchatmembers');
5251
}
5352

54-
return Request::emptyResponse();
53+
// Handle posts forwarded from channels.
54+
if ($this->getMessage()->getForwardFrom()) {
55+
return $this->getTelegram()->executeCommand('id');
56+
}
57+
58+
return parent::execute();
5559
}
5660
}

commands/IdCommand.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the PHP Telegram Support Bot.
5+
*
6+
* (c) PHP Telegram Bot Team (https://github.com/php-telegram-bot)
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Longman\TelegramBot\Commands\UserCommands;
15+
16+
use Longman\TelegramBot\Commands\UserCommand;
17+
use Longman\TelegramBot\Entities\ServerResponse;
18+
use Longman\TelegramBot\Exception\TelegramException;
19+
use Longman\TelegramBot\Request;
20+
21+
/**
22+
* Display user and chat information.
23+
*/
24+
class IdCommand extends UserCommand
25+
{
26+
/**
27+
* @var string
28+
*/
29+
protected $name = 'id';
30+
31+
/**
32+
* @var string
33+
*/
34+
protected $description = 'Get all identifying information about the current user and chat';
35+
36+
/**
37+
* @var string
38+
*/
39+
protected $version = '0.1.0';
40+
41+
/**
42+
* @var bool
43+
*/
44+
protected $private_only = true;
45+
46+
/**
47+
* @return ServerResponse
48+
* @throws TelegramException
49+
*/
50+
public function preExecute(): ServerResponse
51+
{
52+
$this->isPrivateOnly() && $this->removeNonPrivateMessage();
53+
54+
// Make sure we only reply to messages.
55+
if (!$this->getMessage()) {
56+
return Request::emptyResponse();
57+
}
58+
59+
return $this->execute();
60+
}
61+
62+
/**
63+
* Execute command
64+
*
65+
* @return ServerResponse
66+
* @throws TelegramException
67+
*/
68+
public function execute(): ServerResponse
69+
{
70+
$user_info = '👤 *User Info*' . PHP_EOL . $this->getUserInfo();
71+
$chat_info = '🗣 *Chat Info*' . PHP_EOL . $this->getChatInfo();
72+
73+
return $this->replyToUser($user_info . PHP_EOL . PHP_EOL . $chat_info, ['parse_mode' => 'markdown']);
74+
}
75+
76+
/**
77+
* Get the information of the user.
78+
*
79+
* @return string
80+
*/
81+
protected function getUserInfo(): string
82+
{
83+
$user = $this->getMessage()->getFrom();
84+
85+
return implode(PHP_EOL, [
86+
"User Id: `{$user->getId()}`",
87+
'First Name: ' . (($first_name = $user->getFirstName()) ? "`{$first_name}`" : '_n/a_'),
88+
'Last Name: ' . (($last_name = $user->getLastName()) ? "`{$last_name}`" : '_n/a_'),
89+
'Username: ' . (($username = $user->getUsername()) ? "`{$username}`" : '_n/a_'),
90+
'Language Code: ' . (($language_code = $user->getLanguageCode()) ? "`{$language_code}`" : '_n/a_'),
91+
]);
92+
}
93+
94+
/**
95+
* Get the information of the chat.
96+
*
97+
* @return string
98+
*/
99+
protected function getChatInfo(): string
100+
{
101+
$message = $this->getMessage();
102+
$chat = $message->getForwardFromChat() ?? $message->getChat();
103+
104+
if (!$chat || $chat->isPrivateChat()) {
105+
return '`Private chat`';
106+
}
107+
108+
return implode(PHP_EOL, [
109+
"Type: `{$chat->getType()}`",
110+
"Chat Id: `{$chat->getId()}`",
111+
'Title: ' . (($title = $chat->getTitle()) ? "`{$title}`" : '_n/a_'),
112+
'First Name: ' . (($first_name = $chat->getFirstName()) ? "`{$first_name}`" : '_n/a_'),
113+
'Last Name: ' . (($last_name = $chat->getLastName()) ? "`{$last_name}`" : '_n/a_'),
114+
'Username: ' . (($username = $chat->getUsername()) ? "`{$username}`" : '_n/a_'),
115+
]);
116+
}
117+
}

0 commit comments

Comments
 (0)