Exemple #1
0
 public function process($body, $retry = 0)
 {
     try {
         $data = ['webhook_event' => $body];
         $signature = new Signature($this->signature_key);
         $curl = new Curl();
         $curl->setHeader('X-BOT2HOOK-SIGNATURE', $signature->generate($this->config['webhook_url'], $data));
         $response = $curl->post($this->config['webhook_url'], $data);
         if (empty($response) || !is_object($response) || empty($response->ok)) {
             $error = "Outgoing webhook error. ";
             if (empty($response)) {
                 $error .= 'Curl: ' . $curl->curlErrorCode . ' - ' . $curl->curlErrorMessage . "\n";
             } else {
                 $error .= 'Response: ' . print_r($response, true) . "\n";
             }
             throw new \Exception($error);
         }
     } catch (\Exception $e) {
         $this->retry($e, 'b2h_outgoing', $body, $retry);
     }
 }
Exemple #2
0
<?php

use Bot2Hook\Consumer\Incoming;
use Bot2Hook\Signature;
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) {