예제 #1
0
use Bot2Hook\Logger;
$config = (require __DIR__ . '/../app/bootstrap.php');
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');
$logger = new Logger($config['logger']);
$signature = new 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['bot'])) {
        throw new \Exception('No bot key in incoming web hook');
    }
    $bot = json_decode($request['bot'], true);
    if (empty($bot)) {
        $bot = $request['bot'];
    }
    Incoming::addBot($bot, $logger);
    echo json_encode(['ok' => true]);
} catch (\Exception $e) {
    $logger->err("Incoming webhook error: " . $e->getMessage() . "\n" . json_encode($request) . "\n");
    http_response_code(400);
    echo json_encode(['error' => $e->getMessage()]);
}
예제 #2
0
파일: cli.php 프로젝트: alefeuvre/bot2hook
use Bot2Hook\Consumer\Outgoing;
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']);
try {
    switch ($task) {
        case 'b2h_outgoing':
            $body = $argv[2];
            $retry = isset($argv[3]) ? $argv[3] : 0;
            $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;
            $incoming = new Incoming($config['server'], $rabbitmq, $logger);
            $incoming->process($body, $retry);
            break;
        default:
            error_log("[CLI] Unknwon task '{$task}'\n", 3, DIR_LOGS . '/error-cli.log');
            exit(1);
    }
} catch (\Exception $e) {
    $message = date("Y-m-d H:i:s") . ' - Cli ' . __FILE__ . ' PHPException: ' . $e->getCode() . '-' . $e->getMessage() . "\n" . $e->getTraceAsString();
    error_log($message . "\n", 3, DIR_LOGS . '/error-cli.log');
    $app->logger->addAlert($message);
    echo $message;
}