Example #1
0
 /**
  *  validate message
  */
 protected function _validateMessage($message)
 {
     if (!$message) {
         di('log')->record("message not found at " . date('Y-m-d H:i:s'));
         exit;
     }
     // 已處理過的 message 將不再處理
     if ($message->getIsUsed()) {
         di('log')->record("message {$id} is used");
         exit;
     }
     // 回應的 chat_id 必須在白名單之內
     $chatId = $message->getChatId();
     $allowIds = conf('bot.allow_chat_ids');
     if (!in_array($chatId, $allowIds)) {
         di('log')->record("message can not allow send to {$chatId} ({$message->getName()})");
         // debug -> 如果不在予許的名單內, 發送警告訊息
         if (isTraining()) {
             $userId = $message->getUserId();
             $text = '您不在白名單之內 by BOT';
             BotHelper::sendMessage($userId, $text);
         }
         exit;
     }
 }
Example #2
0
function getDefaultSlimConfig()
{
    $container = new \Slim\Container();
    if (isTraining()) {
        $container['settings']['displayErrorDetails'] = true;
    }
    // Override the default Not Found Handler
    $container['notFoundHandler'] = function ($c) {
        return function ($request, $response) use($c) {
            $error = ErrorSupportHelper::getJson('4001');
            return $c['response']->withStatus(404)->withHeader('Content-Type', 'application/json')->write($error);
        };
    };
    return $container;
}
<?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;
Example #4
0
 /**
  *  Zend db execute, write
  *
  *  @param zend sql object
  *      Zend\Db\Sql\Insert
  *      Zend\Db\Sql\Update
  *      Zend\Db\Sql\Delete
  *  @return statement result object
  */
 public function execute($write)
 {
     $adapter = $this->getAdapter();
     $sql = $write->getSqlString($adapter->getPlatform());
     if (isTraining()) {
         di('log')->sql($sql);
     }
     $this->error = null;
     try {
         $statement = $adapter->query($sql);
         $result = $statement->execute();
     } catch (Exception $e) {
         // insert/update/delete error
         // 例如: 重覆的鍵值 引發了 衝突
         $this->error = $e;
         return false;
     }
     return $result;
 }
Example #5
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);