Add Request and validation

This commit is contained in:
2024-08-07 17:46:01 +02:00
parent 1aa26c836a
commit e25502f4f8
4 changed files with 72 additions and 33 deletions

View File

@@ -1,24 +1,22 @@
<?php
use Shikiryu\Bot\Bot;
use Shikiryu\Bot\Request;
require 'vendor/autoload.php';
error_reporting(E_ALL);
ini_set('log_errors', 1);
$config = include 'config.php';
$config = include __DIR__.'/config.php';
$bot = new Bot($config);
$request = new Request();
$bot = new Bot($request, $config);
$signature = $_SERVER['HTTP_X_NEXTCLOUD_TALK_SIGNATURE'] ?? '';
$random = $_SERVER['HTTP_X_NEXTCLOUD_TALK_RANDOM'] ?? '';
$server = $_SERVER['HTTP_X_NEXTCLOUD_TALK_BACKEND'] ?? '';
$delivery = $_SERVER['HTTP_X_H1_DELIVERY'] ?? '';
$event = $_SERVER['HTTP_X_H1_EVENT'] ?? '';
$signature = $_SERVER['HTTP_X_H1_SIGNATURE'] ?? '';
if (!$bot->isRequestValid()) {
$bot->reply('I received an invalid request');
exit;
}
// Give the bot something to listen for.
foreach ($bot->listCommands() as $command) {
@@ -29,22 +27,11 @@ $bot->hears('(hello|hi|bonjour|salut)', function (Bot $bot) {
$bot->replyPolitely('Bonjour');
});
$body = file_get_contents('php://input');
try {
$data = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
$data = [];
}
// Only on message, not on event or whatever
if ($data['type'] !== 'Create' || $data['object']['name'] !== 'message') {
if (!$request->isMessage()) {
return ;
}
try {
$message = json_decode($data['object']['content'], true, 512, JSON_THROW_ON_ERROR)['message'];
} catch (JsonException $e) {
return;
}
$message = $request->getMessage();
$bot->listen($message);