2024-08-05 23:33:27 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Shikiryu\Bot;
|
|
|
|
|
|
|
|
class Bot
|
|
|
|
{
|
2024-08-06 16:06:07 +02:00
|
|
|
protected Request $request;
|
|
|
|
protected array $events = [];
|
|
|
|
protected array $config = [];
|
|
|
|
protected static array $masters = [];
|
2024-08-05 23:33:27 +02:00
|
|
|
|
2024-08-06 16:06:07 +02:00
|
|
|
protected function getMaster(): string
|
2024-08-05 23:33:27 +02:00
|
|
|
{
|
|
|
|
$masters = array_merge(self::$masters, $this->getConfig()['masters']);
|
2024-08-06 16:06:07 +02:00
|
|
|
return $masters[array_rand($masters)];
|
2024-08-05 23:33:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __construct(array $config = [])
|
|
|
|
{
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
|
|
|
|
2024-08-06 16:06:07 +02:00
|
|
|
public function isValid(Request $request): bool
|
2024-08-05 23:33:27 +02:00
|
|
|
{
|
|
|
|
$this->request = $request;
|
|
|
|
return $this->config['token'] === $request->getToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-08-06 16:06:07 +02:00
|
|
|
* @param string $pattern the pattern to listen for
|
2024-08-05 23:33:27 +02:00
|
|
|
* @param \Closure|string $callback the callback to execute. Either a closure or a Class@method notation
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2024-08-06 16:06:07 +02:00
|
|
|
public function hears(string $pattern, \Closure|string $callback): static
|
2024-08-05 23:33:27 +02:00
|
|
|
{
|
|
|
|
if (!array_key_exists($pattern, $this->events)) {
|
|
|
|
$this->events[$pattern] = $callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to match messages with the ones we should
|
|
|
|
* listen to.
|
|
|
|
*/
|
2024-08-06 16:06:07 +02:00
|
|
|
public function listen($sentence): void
|
2024-08-05 23:33:27 +02:00
|
|
|
{
|
|
|
|
foreach ($this->events as $pattern => $command) {
|
|
|
|
$preg_pattern = sprintf('#%s#i', $pattern);
|
|
|
|
if (preg_match($preg_pattern, $sentence, $matches)) {
|
|
|
|
if (is_callable($command)) {
|
2024-08-06 16:42:16 +02:00
|
|
|
call_user_func($command, $this);
|
2024-08-05 23:33:27 +02:00
|
|
|
} else {
|
2024-08-06 16:42:16 +02:00
|
|
|
call_user_func([$command, 'getMessage'], $this, $matches);
|
2024-08-05 23:33:27 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2024-08-06 16:06:07 +02:00
|
|
|
|
2024-08-05 23:33:27 +02:00
|
|
|
$this->replyPolitely('Je n\'ai pas compris');
|
|
|
|
}
|
|
|
|
|
2024-08-06 16:06:07 +02:00
|
|
|
public function replyPolitely(string $message): void
|
2024-08-05 23:33:27 +02:00
|
|
|
{
|
|
|
|
$message .= ', '.$this->getMaster().'.';
|
|
|
|
$this->reply($message);
|
|
|
|
}
|
|
|
|
|
2024-08-06 16:06:07 +02:00
|
|
|
public function sendChatMessage(string $referenceId, string $message): void {
|
2024-08-05 23:33:27 +02:00
|
|
|
$body = [
|
|
|
|
'message' => $message,
|
|
|
|
'referenceId' => $referenceId,
|
|
|
|
];
|
|
|
|
|
|
|
|
$jsonBody = json_encode($body, JSON_THROW_ON_ERROR);
|
|
|
|
|
|
|
|
$random = bin2hex(random_bytes(32));
|
|
|
|
$hash = hash_hmac('sha256', $random . $message, $this->config['secret']);
|
|
|
|
|
|
|
|
$ch = curl_init(rtrim($this->config['url'], '/') . '/ocs/v2.php/apps/spreed/api/v1/bot/' . $this->config['conversation'] . '/message');
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonBody);
|
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
|
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
2024-08-06 16:06:07 +02:00
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, 'alfred/1.0');
|
2024-08-05 23:33:27 +02:00
|
|
|
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
|
|
'OCS-APIRequest: true',
|
|
|
|
'Content-Type: application/json',
|
|
|
|
'X-Nextcloud-Talk-Bot-Random: ' . $random,
|
|
|
|
'X-Nextcloud-Talk-Bot-Signature: ' . $hash,
|
|
|
|
]);
|
2024-08-06 16:06:07 +02:00
|
|
|
curl_exec($ch);
|
2024-08-05 23:33:27 +02:00
|
|
|
curl_close($ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function sanitizeStringForTalkMessage(string $text): string {
|
|
|
|
return str_replace(['@', 'http://', 'https://'], ['π€', 'π', 'ππ'], $text);
|
|
|
|
}
|
|
|
|
|
2024-08-06 16:06:07 +02:00
|
|
|
public function reply(string $message): void
|
2024-08-05 23:33:27 +02:00
|
|
|
{
|
|
|
|
$this->sendChatMessage('', $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getConfig()
|
|
|
|
{
|
|
|
|
return $this->config;
|
|
|
|
}
|
|
|
|
}
|