-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBotService.php
232 lines (206 loc) · 5.51 KB
/
BotService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
declare(strict_types=1);
namespace App\Service;
use InvalidArgumentException;
use Symfony\Contracts\Translation\TranslatorInterface;
use Telegram\Bot\Exceptions\TelegramSDKException;
use Telegram\Bot\Objects\Message as MessageObject;
use Telegram\Bot\Objects\Update as UpdateObject;
use Telegram\Bot\Api;
/**
* Bot helper service
*/
class BotService
{
/**
* Waiting time for message processing (seconds)
*/
public const MESSAGE_TIME_OUT = 10;
/**
* Exclusion for escape MarkdownV2
*/
public const ESCAPE_CHAR_EXCLUSION = ['*', '_', '~'];
/**
* @var string
*/
public readonly string $name;
/**
* Api object
*
* @var Api
*/
private Api $api;
/**
* Is dev mode
*
* @var bool
*/
private bool $isDevMode;
/**
* Dev chat identifier
*
* @var int
*/
protected int $devChatId;
/**
* Translator
*
* @var TranslatorInterface
*/
protected TranslatorInterface $translator;
/**
* Constructor
*
* @throws TelegramSDKException
*/
public function __construct(string $name,
string $token,
bool $isDevMode,
int $devChatId,
TranslatorInterface $translator)
{
$this->api = new Api($token);
$this->name = $name;
$this->isDevMode = $isDevMode;
$this->devChatId = $devChatId;
$this->translator = $translator;
}
/**
* Get bot name
*
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* Send message wrapper
*
* @param array $params
* @param int|null $senderChatId For check dev chat id
*
* @return MessageObject
* @throws TelegramSDKException
*/
public function sendMessage(array $params, ?int $senderChatId = null): MessageObject
{
if ($this->isDevMode && $senderChatId !== $this->devChatId) {
return $this->api->sendMessage([
'chat_id' => $params['chat_id'],
'text' => $this->translator->trans('maintenance', [], 'common'),
]);
}
if ($senderChatId) {
$params['chat_id'] = $senderChatId;
}
return $this->api->sendMessage($params);
}
/**
* Edit message
*
* @param string|int $chatId
* @param int $messageId
* @param string $text
* @param array $inlineKeyboardElements Array of rows and buttons
*
* @return MessageObject
* @throws TelegramSDKException
*/
public function editMessageText(string|int $chatId,
int $messageId,
string $text,
array $inlineKeyboardElements = []): MessageObject
{
$params = ['chat_id' => $chatId, 'message_id' => $messageId, 'text' => $text];
if ($inlineKeyboardElements) {
$params['reply_markup'] = json_encode(['inline_keyboard' => $inlineKeyboardElements]);
}
return $this->api->editMessageText($params);
}
/**
* Send media group
*
* @param string|int $chatId
* @param array $mediaItems Array of media elements
*
* @return MessageObject
* @throws TelegramSDKException
*/
public function sendMediaGroup(string|int $chatId, array $mediaItems): MessageObject
{
return $this->api->sendMediaGroup(['chat_id' => $chatId, 'media' => json_encode($mediaItems)]);
}
/**
* Get webhook update
*
* @return UpdateObject
*/
public function getWebhookUpdate(): UpdateObject
{
return $this->api->getWebhookUpdate();
}
/**
* Has date in message
*
* @param UpdateObject $data
*
* @return bool
*/
public function hasMessageSendDate(UpdateObject $data): bool
{
return isset($data->getMessage()?->date);
}
/**
* Is message time out
*
* @param UpdateObject $data
*
* @return bool
*/
public function isMessageTimedOut(UpdateObject $data): bool
{
if (!$this->hasMessageSendDate($data)) {
throw new InvalidArgumentException('Income message don\'t have date');
}
return (time() - $data->getMessage()->date) > self::MESSAGE_TIME_OUT;
}
/**
* Send answers to callback queries sent from inline keyboards.
*
* @param string|int $callbackQueryId
*
* @return void
* @throws TelegramSDKException
*/
public function answerCallbackQuery(string|int $callbackQueryId): void
{
$this->api->answerCallbackQuery(['callback_query_id' => $callbackQueryId]);
}
/**
* Escape char for MarkdownV2
*
* @param string $char
*
* @return string
*/
public static function escapeChar(string $char): string
{
if (in_array($char, self::ESCAPE_CHAR_EXCLUSION)) {
return $char;
}
$num = ord($char);
return $num <= 126 ? '\\' . $char : $char;
}
/**
* Escape string for MarkdownV2
*
* @param string $string
*
* @return string
*/
public static function escapeString(string $string): string
{
return implode('', array_map('\App\Service\BotService::escapeChar', str_split($string)));
}
}