<?php /** * 該程式僅供於 training 環境使用 * - 不使用 web hook * - 資料直接取得 https://api.telegram.org/bot???:??????/getUpdates * */ $basePath = dirname(__DIR__); require_once $basePath . '/app/bootstrap.php'; initialize($basePath, 'home'); if (!isTraining()) { echo ErrorSupportHelper::getJson('4002'); exit; } $telegram = BotHelper::getTelegram(); try { $updates = $telegram->getUpdates(); } catch (\Telegram\Bot\Exceptions\TelegramResponseException $e) { put($e->getMessage()); return; } $result = []; $messages = new Messages(); foreach ($updates as $update) { $message = MessageHelper::makeMessageByTelegramUpdate($update); // 從 updates 來的資料會有許多重覆資料 // 該確認之後再寫入 $existMessage = $messages->getMessageByMessageId($message->getMessageId()); if ($existMessage) { continue;
$app->abort(404); } return require __DIR__ . '/../config/bots/' . $config['bots'][$bot] . '.php'; }); $app->get('/set_webhook/{bot}', function ($bot, Request $request) use($app, $config, $get_bot_config) { $bot_config = $get_bot_config($bot); //Let's make a new guzzle connection and set the hook: $client = new GuzzleHttp\Client(); $res = $client->get('https://api.telegram.org/bot' . $bot_config['token'] . '/setWebhook?url=https://' . $request->getHttpHost() . '/hook/' . $bot); return $res->getBody(); }); $app->post('/hook/{route}', function ($route) use($app, $config, $get_bot_config) { $bot_config = $get_bot_config($route); $response = file_get_contents('php://input'); $response_data = json_decode($response, true); $helper = new BotHelper($config, $route); $response = $helper->fetchMessage($bot_config, $response_data); //If its markup is like a command, just ignore it. //TODO this feature will be enabled in future versions. if (!$response) { return 'OK'; } //now let's reply! //https://core.telegram.org/bots/api#sendmessage $client = new GuzzleHttp\Client(); if ($config['source'] == 'file') { //try { $client->get('https://api.telegram.org/bot' . $bot_config['token'] . '/sendMessage?' . http_build_query($response['data'])); /*} catch(Exception $e) { //You can catch exceptions and log etc here by uncommenting try/catch statement. }*/