๐ Hello world
This commit is contained in:
143
bot/Bot.php
Normal file
143
bot/Bot.php
Normal file
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\Bot;
|
||||
|
||||
class Bot
|
||||
{
|
||||
/**
|
||||
* @var Request
|
||||
*/
|
||||
protected $request;
|
||||
protected $events = [];
|
||||
protected $config = [];
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected static $masters = [];
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getMaster()
|
||||
{
|
||||
$masters = array_merge(self::$masters, $this->getConfig()['masters']);
|
||||
return $masters[array_rand($masters, 1)];
|
||||
}
|
||||
|
||||
/**
|
||||
* Bot constructor.
|
||||
* @param array $config
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid(Request $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
return $this->config['token'] === $request->getToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $pattern the pattern to listen for
|
||||
* @param \Closure|string $callback the callback to execute. Either a closure or a Class@method notation
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function hears($pattern, $callback)
|
||||
{
|
||||
if (!array_key_exists($pattern, $this->events)) {
|
||||
$this->events[$pattern] = $callback;
|
||||
} else {
|
||||
error_log(sprintf('Event %s dรฉjร en place', $pattern));
|
||||
}
|
||||
|
||||
// $this->events[$pattern][] = $callback;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to match messages with the ones we should
|
||||
* listen to.
|
||||
*/
|
||||
public function listen($sentence)
|
||||
{
|
||||
foreach ($this->events as $pattern => $command) {
|
||||
$preg_pattern = sprintf('#%s#i', $pattern);
|
||||
if (preg_match($preg_pattern, $sentence, $matches)) {
|
||||
if (is_callable($command)) {
|
||||
call_user_func($command, $this);
|
||||
} else {
|
||||
call_user_func([$command, 'getMessage'], $this, $matches);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
$this->replyPolitely('Je n\'ai pas compris');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
*/
|
||||
public function replyPolitely($message)
|
||||
{
|
||||
$message .= ', '.$this->getMaster().'.';
|
||||
$this->reply($message);
|
||||
}
|
||||
|
||||
public function sendChatMessage(string $referenceId, string $message): void {
|
||||
$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);
|
||||
curl_setopt($ch, CURLOPT_USERAGENT, 'nextcloud-talk-hackerone-adapter/1.0');
|
||||
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,
|
||||
]);
|
||||
$var = curl_exec($ch);
|
||||
$error = curl_error($ch);
|
||||
curl_close($ch);
|
||||
}
|
||||
|
||||
private function sanitizeStringForTalkMessage(string $text): string {
|
||||
return str_replace(['@', 'http://', 'https://'], ['๐ค', '๐', '๐๐'], $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
*/
|
||||
public function reply($message)
|
||||
{
|
||||
// header('Content-type: application/json');
|
||||
// die('{"text": "'.$message.'"}');
|
||||
$this->sendChatMessage('', $message);
|
||||
}
|
||||
|
||||
public function getConfig()
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user