Rearchitect for different types of response
This commit is contained in:
@@ -1,7 +1,58 @@
|
||||
<?php
|
||||
|
||||
interface MessageHandler
|
||||
abstract class MessageHandler
|
||||
{
|
||||
public function CanHandle($room_id, $msgid, $message): bool;
|
||||
public function Handle($room_id, $msgid, $message): string;
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user