朗文电报机器人处理用户输入

ke1evra

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

  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() 中只触发一次。我认为因为来自用户的第二条消息不是回调。

ke1evra

解决了。对话必须像

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章