You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.7 KiB
PHP
48 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Telegram;
|
|
|
|
use TelegramBot\Api\BotApi;
|
|
|
|
/** One-shot Telegram transport. It deliberately performs no retry. */
|
|
final class TelegramBotApiTransport implements AdministratorTelegramTransport
|
|
{
|
|
public function sendMessage(
|
|
string $botToken,
|
|
string $chatId,
|
|
string $text,
|
|
array $options = []
|
|
): int {
|
|
try {
|
|
$result = (new BotApi($botToken))->call('sendMessage', array_merge([
|
|
'chat_id' => $chatId,
|
|
'text' => $text,
|
|
], $options), 10);
|
|
} catch (\Throwable $exception) {
|
|
// SDK exception text can contain the bot token through its request URL.
|
|
throw new AdministratorTelegramRemoteEffectUnknown(
|
|
'Administrator Telegram remote effect is unknown'
|
|
);
|
|
}
|
|
|
|
$messageId = is_array($result) ? ($result['message_id'] ?? null) : null;
|
|
if (is_object($result)) {
|
|
if (method_exists($result, 'getMessageId')) {
|
|
$messageId = $result->getMessageId();
|
|
} elseif (isset($result->message_id)) {
|
|
$messageId = $result->message_id;
|
|
}
|
|
}
|
|
if ((!is_int($messageId) && !is_string($messageId))
|
|
|| preg_match('/^[1-9][0-9]{0,18}$/D', (string) $messageId) !== 1
|
|
|| strlen((string) $messageId) > strlen((string) PHP_INT_MAX)
|
|
|| (strlen((string) $messageId) === strlen((string) PHP_INT_MAX)
|
|
&& strcmp((string) $messageId, (string) PHP_INT_MAX) > 0)) {
|
|
throw new AdministratorTelegramRemoteEffectUnknown(
|
|
'Administrator Telegram remote effect is unknown'
|
|
);
|
|
}
|
|
return (int) $messageId;
|
|
}
|
|
}
|