예제 #1
0
 public function __construct($options = null)
 {
     parent::__construct($options);
     if ($options instanceof \Traversable) {
         $options = ArrayUtils::iteratorToArray($options);
     }
     if (is_array($options)) {
         if (!empty($options['stream'])) {
             if (!is_array($options['stream'])) {
                 $options['stream'] = ['uri' => $options['stream']];
             }
             if (!empty($options['stream']['uri'])) {
                 $writer = new Stream($options['stream']['uri']);
                 if (!empty($options['stream']['priority'])) {
                     $filter = new Priority($options['stream']['priority']);
                     $writer->addFilter($filter);
                 }
                 $this->addWriter($writer);
             }
         }
         if (!empty($options['slack'])) {
             $writer = new SlackWriter($options['slack']);
             $this->addWriter($writer);
         }
         if (!empty($options['register_error_handler'])) {
             Logger::registerErrorHandler($this);
         }
         if (!empty($options['register_exception_handler'])) {
             Logger::registerExceptionHandler($this);
         }
     }
 }
예제 #2
0
 public static function addBot($bot, Logger $logger)
 {
     if (is_array($bot) && !isset($bot['bot_token'])) {
         throw new \Exception('no_bot_token');
     }
     $loop = Factory::create();
     $client = new WebSocketClient('ws://0.0.0.0:12345/', $loop, $logger);
     $client->on("error", function () use($loop, $logger) {
         $logger->err("Add team incoming webhook : can't connect to websocket server");
         $loop->stop();
     });
     $client->on("connect", function () use($client, $loop, $logger, $bot) {
         $logger->notice("Add team incoming webhook : " . json_encode($bot));
         $loop->addTimer(2, function () use($client, $loop, $bot) {
             $message = ['type' => 'add_bot', 'bot' => $bot];
             $client->send(json_encode($message));
             $loop->stop();
         });
     });
     $client->open();
     $loop->run();
 }
예제 #3
0
 protected function retry(\Exception $e, $queue, $data, $retry)
 {
     $this->logger->err($e->getMessage());
     echo $e->getMessage() . "\n";
     echo $e->getTraceAsString() . "\n";
     if (is_object($data)) {
         $data->retry = $retry + 1;
     } elseif (is_array($data)) {
         $data['retry'] = $retry + 1;
     } else {
         $temp_data = json_decode($data);
         if (!empty($temp_data)) {
             if (is_object($data)) {
                 $temp_data->retry = $retry + 1;
             } elseif (is_array($data)) {
                 $temp_data['retry'] = $retry + 1;
             }
             $data = json_encode($temp_data);
         }
     }
     try {
         if ($retry <= 5) {
             $delay = 30;
         } elseif ($retry <= 10) {
             $delay = 2 * 60;
         } elseif ($retry <= 15) {
             $delay = 30 * 60;
         } elseif ($retry <= 20) {
             $delay = 4 * 60 * 60;
         } elseif ($retry <= 25) {
             $delay = 24 * 60 * 60;
         } else {
             throw $e;
         }
         $this->rabbitmq->publishStringDelayed($delay, $queue, json_encode($data));
     } catch (\Exception $e) {
         $this->logger->err('Stop retry for consumer queue ' . $queue . ': ' . print_r(json_encode($data), true));
     }
 }
예제 #4
0
 protected function responseHistory(Bot $bot, $room_id, $token, $response)
 {
     if ($response->ok) {
         $latest = null;
         usort($response->messages, function ($a, $b) {
             if ($a->ts == $b->ts) {
                 return 0;
             }
             return $a->ts < $b->ts ? -1 : 1;
         });
         foreach ($response->messages as $message) {
             $message->channel = $room_id;
             $this->publish($bot, ['type' => 'message', 'from_recovery' => true, 'last_from_recovery' => true, 'event' => $message]);
             $latest = $message->ts;
         }
         $bot->rooms[$room_id]->latest = $latest;
         $this->updateTeamBot($bot);
     } else {
         $this->logger->err("Fail room history for bot " . $bot->id . ' and room ' . $room_id . ' with token ' . $token . ' error:' . $response->error);
         if ($this->isTokenError($response->error)) {
             throw new InvalidTokenException($response->error);
         }
     }
 }
예제 #5
0
<?php

use Bot2Hook\Consumer;
use Bot2Hook\Logger;
use Bot2Hook\Rabbitmq;
$config = (require __DIR__ . '/../app/bootstrap.php');
$logger = new Logger($config['logger']);
$logger->notice('Boot2Hook consumer starting');
$rabbitmq = new Rabbitmq($config['rabbitmq']);
$consumer = new Consumer($config['server'], $rabbitmq, $config['cmd_php']);
$consumer->launch();
예제 #6
0
<?php

use Bot2Hook\Logger;
$config = (require __DIR__ . '/../../app/bootstrap.php');
if (!$config['debug']) {
    exit('Only in debug');
}
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');
$logger = new Logger($config['logger']);
$signature = new \Bot2Hook\Signature($config['signature_key']);
$uri = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'];
if ($_SERVER['SERVER_PORT'] != '80') {
    $uri .= ':' . $_SERVER['SERVER_PORT'];
}
$uri .= $_SERVER['SCRIPT_NAME'];
$b2h_signature = empty($_SERVER['HTTP_X_BOT2HOOK_SIGNATURE']) ? '' : $_SERVER['HTTP_X_BOT2HOOK_SIGNATURE'];
$request = $_SERVER['REQUEST_METHOD'] == 'GET' ? $_GET : $_POST;
try {
    if (!$signature->isValid($b2h_signature, $uri, $request)) {
        throw new \Exception('not_valid_signature');
    }
    if (!isset($request['webhook_event'])) {
        throw new \Exception('No webhook_event in incoming web hook');
    }
    $logger->debug("Sample webhook event received: \n" . json_encode(json_decode($request['webhook_event']), JSON_PRETTY_PRINT));
    echo json_encode(['ok' => true]);
} catch (\Exception $e) {
    $logger->err("Sample webhook error: " . $e->getMessage() . "\n" . json_encode($request) . "\n");
    $logger->err("Sample webhook error trace: " . $e->getTraceAsString());
    http_response_code(400);
예제 #7
0
<?php

use Bot2Hook\Logger;
use Bot2Hook\Rabbitmq;
use Bot2Hook\Server;
$config = (require __DIR__ . '/../app/bootstrap.php');
$logger = new Logger($config['logger']);
$logger->notice('Boot2Hook server starting');
$rabbitmq = new Rabbitmq($config['rabbitmq']);
$server = new Server($config['server'], $rabbitmq, $logger);
$server->launch();
예제 #8
0
<?php

use Bot2Hook\Logger;
use Bot2Hook\Rabbitmq;
use Bot2Hook\Server;
$config = (require __DIR__ . '/../app/bootstrap.php');
$logger = new Logger($config['logger']);
$logger->notice('Boot2Hook server starting', $config['server']);
$rabbitmq = new Rabbitmq($config['rabbitmq']);
$server = new Server($config['server'], $rabbitmq, $logger);
$server->launch();
예제 #9
0
파일: cli.php 프로젝트: Mailclark/bot2hook
<?php

$task = isset($argv[1]) ? $argv[1] : null;
if (empty($task)) {
    $caller = implode(' ', $argv);
    error_log("[CLI] Empty task provided in '{$caller}'\n", 3, DIR_LOGS . '/error-cli.log');
    exit(1);
}
use Bot2Hook\Consumer\Incoming;
use Bot2Hook\Consumer\Outgoing;
use Bot2Hook\Logger;
use Bot2Hook\Rabbitmq;
$config = (require __DIR__ . '/../app/bootstrap.php');
$logger = new Logger($config['logger']);
$rabbitmq = new Rabbitmq($config['rabbitmq']);
try {
    switch ($task) {
        case 'b2h_outgoing':
            $body = $argv[2];
            $retry = isset($argv[3]) ? $argv[3] : 0;
            $logger->debug('Boot2Hook consume outgoing queue');
            $outgoing = new Outgoing($config['server'], $rabbitmq, $logger, $config['signature_key']);
            $outgoing->process($body, $retry);
            break;
        case 'b2h_incoming':
            $body = $argv[2];
            $retry = isset($argv[3]) ? $argv[3] : 0;
            $logger->debug('Boot2Hook consume incoming queue');
            $incoming = new Incoming($config['server'], $rabbitmq, $logger);
            $incoming->process($body, $retry);
            break;