예제 #1
0
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;
    }
    $id = $messages->addMessage($message);
    $result[] = [$message->getMessageId(), $id];
    // execute command controller
    if ($id) {
        $controller = new \CommandModule\Enter();
        $controller->home($id);
    }
}
echo json_encode($result, true);
예제 #2
0
<?php

/**
 *  該程式只能在 production 環境使用
 *      - 必須使用 web hook
 *
 */
$basePath = dirname(__DIR__);
require_once $basePath . '/app/bootstrap.php';
initialize($basePath, 'home');
$input = file_get_contents("php://input");
$data = json_decode($input, true);
if (isTraining()) {
    $content = print_r($data, true);
    if ($content) {
        di('log')->write('telegram-bot-hook.log', $content);
    }
}
if (!is_array($data)) {
    exit;
}
$message = MessageHelper::makeMessageByArray($data);
if (!$message) {
    exit;
}
$messages = new Messages();
$messageId = $messages->addMessage($message);
// execute command controller
$controller = new CommandModule\Enter();
$controller->home($messageId);