【问题标题】:longman telegram bot handle user inputlongman 电报机器人处理用户输入
【发布时间】:2019-10-20 22:30:45
【问题描述】:

我需要从内联按钮编辑数据库中的记录。

  1. Bot 向用户发送带有记录文本的消息并为其添加内联按钮操作(即编辑、删除等)
  2. 用户单击按钮,从回调启动 EditCommand 与新对话。但在第一种情况下,下一条消息不要调用 execute() EditCommand - 对话消失了。

$query->getData() 包含操作和记录 ID,例如 action=edit&id=3

点击内联按钮后如何获取用户输入?

 //hook.php
        CallbackqueryCommand::addCallbackHandler(function($query) use ($telegram) {  
        ...
        case 'myaction':
           return $telegram->executeCommand('edit');
        }
// EditCommand.php
public function execute()
    {    
        if ($this->getCallbackQuery() !== null) {
            $message = $this->getCallbackQuery()->getMessage();
        }
        else {
            $message = $this->getMessage();
        }        
        $this->conversation = new Conversation($user_id, $chat_id, $this->getName());
        $notes = &$this->conversation->notes;
        !is_array($notes) && $notes = [];
        $state = 0;
        if (isset($notes['state'])) {
            $state = $notes['state'];
        }
        $result = Request::emptyResponse();        
        switch ($state) {
            case 0:                
                if ($text === '') {
                    $notes['state'] = 0;
                    $this->conversation->update();
                    $data['text'] = 'Choose button';
                    $data['reply_markup'] = $keyboard;
                    $result = Request::sendMessage($data);
                    break;
                }
                $notes['laundry'] = $text;
                $text             = '';
            case 1:  
            ...

然后在 EditCommand 中 execute() 只触发一次。我认为是因为来自用户的第二条消息不是回调。

【问题讨论】:

    标签: php telegram-bot


    【解决方案1】:

    解决了。对话必须像

    $this->conversation = new Conversation(null !== $this->getCallbackQuery() ? $chat_id : $user_id, $chat_id, $this->getName());
    

    【讨论】:

      猜你喜欢
      • 2020-09-24
      • 2019-09-12
      • 2017-07-10
      • 1970-01-01
      • 2021-09-09
      • 1970-01-01
      • 2017-08-18
      • 2018-03-31
      • 2022-08-08
      相关资源
      最近更新 更多