Rearchitect for different types of response
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
If you know, you know
|
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/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 interface for message handlers.
|
- `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.
|
- `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.
|
- `test/` is for PHPUnit tests. It is quite empty atm and only contain a placeholder.
|
||||||
@@ -2,18 +2,15 @@
|
|||||||
|
|
||||||
require_once 'MessageHandler.php';
|
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.
|
if($msgid != 0)
|
||||||
return $msgid == 0;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
public function Handle($room_id, $msgid, $message): string
|
$this->sendMessage($room_id, "uwu");
|
||||||
{
|
return true;
|
||||||
// TODO: Implement handle() method.
|
|
||||||
return "uwu";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,58 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
interface MessageHandler
|
abstract class MessageHandler
|
||||||
{
|
{
|
||||||
public function CanHandle($room_id, $msgid, $message): bool;
|
abstract public function TryHandle($room_id, $msgid, $message, $room_lang): bool;
|
||||||
public function Handle($room_id, $msgid, $message): string;
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
// Placeholder Globals
|
// Placeholder Globals
|
||||||
$userid = 0;
|
//$userid = 0;
|
||||||
$room_lang = "th";
|
//$room_lang = "th";
|
||||||
$is_admin = false;
|
//$is_admin = false;
|
||||||
define("BLUE_ROOM", 12345);
|
define("BLUE_ROOM", 12345);
|
||||||
define("RED_ROOM", 67890);
|
define("RED_ROOM", 67890);
|
||||||
define("INTER_ROOM", 111213);
|
define("INTER_ROOM", 111213);
|
||||||
@@ -14,21 +14,16 @@ require_once 'MessageHandler.php';
|
|||||||
require_once 'Handlers.php';
|
require_once 'Handlers.php';
|
||||||
|
|
||||||
$handlers = array(
|
$handlers = array(
|
||||||
|
new TestHandler(),
|
||||||
new TestHandler()
|
new TestHandler()
|
||||||
);
|
);
|
||||||
|
|
||||||
function reply_user($room_id, $msgid, $message)
|
function reply_user($room_id, $msgid, $message, $room_lang)
|
||||||
{
|
{
|
||||||
// Placeholder Globals
|
|
||||||
global $userid;
|
|
||||||
global $room_lang;
|
|
||||||
global $is_admin;
|
|
||||||
|
|
||||||
global $handlers;
|
global $handlers;
|
||||||
foreach ($handlers as $handler) {
|
foreach ($handlers as $handler) {
|
||||||
if($handler->CanHandle($room_id, $msgid, $message)) {
|
if($handler->TryHandle($room_id, $msgid, $message, $room_lang)) {
|
||||||
return $handler->Handle($room_id, $msgid, $message);
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ;
|
|
||||||
}
|
}
|
||||||
@@ -8,11 +8,15 @@ $room_id = BLUE_ROOM;
|
|||||||
$msg_id = 0;
|
$msg_id = 0;
|
||||||
|
|
||||||
echo "Starting test REPL\n";
|
echo "Starting test REPL\n";
|
||||||
|
|
||||||
|
foreach ($handlers as $handler) {
|
||||||
|
$handler->PrintToScreen = true;
|
||||||
|
}
|
||||||
do {
|
do {
|
||||||
echo $msg_id, ' > ';
|
echo $msg_id, ' > ';
|
||||||
$input = fread(STDIN, 80); // Read up to 80 characters or a newline
|
$input = fread(STDIN, 80); // Read up to 80 characters or a newline
|
||||||
$trimmed = trim($input);
|
$trimmed = trim($input);
|
||||||
|
|
||||||
echo reply_user($room_id, $msg_id, $trimmed) , "\n";
|
echo reply_user($room_id, $msg_id, $trimmed, "th") , "\n";
|
||||||
$msg_id++;
|
$msg_id++;
|
||||||
} while($trimmed !== "exit");
|
} while($trimmed !== "exit");
|
||||||
Reference in New Issue
Block a user