irazasyed / telegram-bot-sdk触发器命令php

时间:2018-01-18 06:03:37

标签: php telegram telegram-bot

我正在使用irazasyed / telegram-bot-sdk,我需要按文本触发命令。

以下是代码示例:

    $telegram = new Api($key);
    $commands = [
        \Startdev::class,
        \Help::class,
        \Cinema::class,
        \Theater::class,
        \Exhibition::class,
        \Holidays::class,
        \Sport::class
    ];

    $telegram->addCommands($commands);
    $commandsHandler= $telegram->commandsHandler(true);

    $updates = $telegram->getWebhookUpdates();

    if (!empty($updates)){

        if ($updates->isType('callback_query')) {

            $query = $updates->getCallbackQuery();
            $data  = $query->getData();

           //$data - here is my command (for example - "cinema")

            $telegram->answerCallbackQuery([
                'callback_query_id' => $query->getId()
            ]);

            try{
                $telegram->triggerCommand($data,$commandsHandler);
            }  catch (Exception $e){
                $this->log($e->getMessage());
            } 

        }
    }

什么都没发生。但是命令/cinema的处理程序完全有效。

你能帮助我或提出一些可能有问题的建议吗?

更多信息:
irazasyed/telegram-bot-sdk: master-dev版本。

1 个答案:

答案 0 :(得分:0)

我无法回答为什么triggerCommand无效,但这段代码完美无缺。

$commands = [...];
$telegram->addCommands($commands);
$commandsHandler = $telegram->commandsHandler(true);
//$command = "yourCommand" for example, $arguments = array of something
$res = $telegram->getCommandBus()->execute($command, $arguments, $commandsHandler);

希望它对像我这样的人有用=)