Files
tg-bot/src/MessageHandler.php

58 lines
1.8 KiB
PHP

<?php
abstract class MessageHandler
{
abstract public function TryHandle($room_id, $msgid, $message, $room_lang): bool;
protected $token = 'CHANGEME';
public $PrintToScreen = false;
protected function sendMessage($chat, $content, $reply = null, $keyboard = null) {
return $this->makeRequest('sendMessage', array(
'chat_id' => $chat,
'text' => $content,
'reply_to_message_id' => $reply,
'reply_markup' => $keyboard
));
}
protected function sendSticker($chat, $sticker_id, $reply = null) {
return $this->makeRequest('sendSticker', array(
'chat_id' => $chat,
'sticker' => $sticker_id,
'reply_to_message_id' => $reply
));
}
protected function sendPhoto($chat, $file_id, $reply = null) {
return $this->makeRequest('sendSticker', array(
'chat_id' => $chat,
'photo' => $file_id,
'reply_to_message_id' => $reply
));
}
private function makeRequest($method, $data, $post = true) {
if($this->PrintToScreen){
echo get_class($this), ' ', $method, ' ', json_encode($data, JSON_PRETTY_PRINT), "\n";
return ;
}
$url = 'https://api.telegram.org/bot' . $this->token . '/' . $method;
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => ($post)? 'GET':'POST'
)
);
if ($post) { // POST request
$options['http']['content'] = http_build_query($data);
} else { // GET Request
$url .= '?' . http_build_query($data);
}
$context = stream_context_create($options);
return file_get_contents($url, false, $context);
}
}