Exemplo n.º 1
0
<?php

require __DIR__ . '/../vendor/autoload.php';
use Rde\Telegram\Connection;
use Rde\Bot;
use Rde\DataChain;
$config = (include_once '../Config/config.php');
$app = new Silex\Application();
$app->post('/hook/{hook}', function ($hook) use($app, $config) {
    $botName = $config[$hook]['bot'];
    $token = $config[$hook]['token'];
    $post = file_get_contents('php://input');
    $postData = new DataChain(json_decode($post));
    $chatId = $postData->message->chat->id->value();
    $bot = Bot::make($botName);
    $message = $bot->parse($postData);
    if (!isset($chatId) || !isset($message)) {
        return 'OK';
    }
    $conn = new Connection($token);
    $conn->sendMessage(['chat_id' => $chatId, 'text' => $message]);
    return 'OK';
});
$app->run();
#!/usr/bin/env php

<?php 
$config = (require __DIR__ . '/Config/config.php');
if (!isset($argv[1])) {
    fwrite(STDERR, "使用方式:./activate-hook.php <hook> <host>,請輸入Host網址\n");
    die;
}
if (!isset($argv[2])) {
    fwrite(STDERR, "使用方式:./activate-hook.php <hook> <host>,請輸入Host網址\n");
    die;
}
if (!array_key_exists($argv[1], $config)) {
    fwrite(STDERR, "尚未設定[{$argv[1]}],請先設定config檔\n");
    die;
}
require __DIR__ . '/vendor/autoload.php';
use Rde\Telegram\Connection;
$hook = $argv[1];
$host = $argv[2];
$token = $config[$hook]['token'];
$conn = new Connection($token);
$res = $conn->setWebhook(['url' => $host . '/hook/' . $hook]);
$res ? fwrite(STDERR, "{$hook} Set OK\n") : fwrite(STDERR, "{$hook} Set failure\n");