Files
tangmo_fxtwitter_bot/src/Commands/GenericmessageCommand.php

55 lines
1.7 KiB
PHP
Executable File

<?php
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Request;
// This class name is magic
class GenericmessageCommand extends SystemCommand
{
/** @var string */
protected $name = 'genericmessage';
/** @var string */
protected $description = 'Handle generic message';
/** @var string */
protected $version = '1.0.0';
public function execute(): ServerResponse
{
global $allowed_chat_ids;
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
if(count($allowed_chat_ids) > 0 && !in_array($chat_id, $allowed_chat_ids)) {
return $this->replyToChat("Chat ID $chat_id not allowed");
}
$text = $message->getText(true);
if ($message->getMediaGroupId() || empty($text)) {
return Request::emptyResponse();
}
preg_match('/https?:\/\/(?:mobile\.)?twitter\.com\/(\w+)\/status\/(\d+)(\S*)/', $text, $matches);
if (count($matches) < 3) {
return Request::emptyResponse();
}
$author = $matches[1];
$tweetId = $matches[2];
$photoNo = '';
if (!empty($matches[3])) {
preg_match('/(\/\d)$/', $matches[3], $photoNoMatches);
if (isset($photoNoMatches[1])) {
$photoNo = '/photo' . $photoNoMatches[1];
}
}
return $this->replyToChat(
"https://fxtwitter.com/$author/status/$tweetId$photoNo",
[
'reply_to_message_id' => $message->getMessageId(),
'disable_notification' => true,
]
);
}
}