diff --git a/README.md b/README.md index 8409d48..336b2bc 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ If you know, you know -- `src/entryPoint.php` is the main entry-point function. It will look through all registered implementation of MessageHandler and take the first one that can handle the message. **Register new message handler here.** -- `src/MessageHandler.php` is the interface for message handlers. +- `src/entryPoint.php` is the main entry-point function. It will look through all registered implementations of MessageHandler and take the first one that successfully handled the message. **Register new message handler here.** +- `src/MessageHandler.php` is the abstract class for message handlers. It has a pre-implemented functions to send responses. - `src/repl.php` is a REPL to send test messages to the entryPoint, intended for quick tests, using PHP CLI. - `test/` is for PHPUnit tests. It is quite empty atm and only contain a placeholder. \ No newline at end of file diff --git a/src/Handlers.php b/src/Handlers.php index f28c1d6..f27e856 100644 --- a/src/Handlers.php +++ b/src/Handlers.php @@ -2,18 +2,15 @@ require_once 'MessageHandler.php'; -class TestHandler implements MessageHandler +class TestHandler extends MessageHandler { - public function CanHandle($room_id, $msgid, $message): bool + public function TryHandle($room_id, $msgid, $message, $room_lang): bool { - // TODO: Implement canHandle() method. - return $msgid == 0; - } + if($msgid != 0) + return false; - public function Handle($room_id, $msgid, $message): string - { - // TODO: Implement handle() method. - return "uwu"; + $this->sendMessage($room_id, "uwu"); + return true; } } \ No newline at end of file diff --git a/src/MessageHandler.php b/src/MessageHandler.php index 43eac5e..04bc13d 100644 --- a/src/MessageHandler.php +++ b/src/MessageHandler.php @@ -1,7 +1,58 @@ 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); + } } \ No newline at end of file diff --git a/src/entryPoint.php b/src/entryPoint.php index 3a75d92..bdfe533 100644 --- a/src/entryPoint.php +++ b/src/entryPoint.php @@ -1,9 +1,9 @@ CanHandle($room_id, $msgid, $message)) { - return $handler->Handle($room_id, $msgid, $message); + if($handler->TryHandle($room_id, $msgid, $message, $room_lang)) { + break; } } - return ; } \ No newline at end of file diff --git a/src/repl.php b/src/repl.php index 31a0e41..57159bf 100644 --- a/src/repl.php +++ b/src/repl.php @@ -8,11 +8,15 @@ $room_id = BLUE_ROOM; $msg_id = 0; echo "Starting test REPL\n"; + +foreach ($handlers as $handler) { + $handler->PrintToScreen = true; +} do { echo $msg_id, ' > '; $input = fread(STDIN, 80); // Read up to 80 characters or a newline $trimmed = trim($input); - echo reply_user($room_id, $msg_id, $trimmed) , "\n"; + echo reply_user($room_id, $msg_id, $trimmed, "th") , "\n"; $msg_id++; } while($trimmed !== "exit"); \ No newline at end of file