Skip to content
Advertisement

longman telegram bot handle user input

I need to edit record in database from inline button.

  1. Bot send message to user with record text and add inline button actions for it (i.e. Edit, Delete etc.)
  2. User click button, from callback starts EditCommand with new conversation. but after first case next message dont call execute() EditCommand – conversation disappeared.

$query->getData() contains action and record ID like action=edit&id=3

how can I get user input after inline button click?

 //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:  
            ...

and then in EditCommand execute() triggered only once. I think because second message from user is not callback.

Advertisement

Answer

solved. conversation must created like

$this->conversation = new Conversation(null !== $this->getCallbackQuery() ? $chat_id : $user_id, $chat_id, $this->getName());
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement