【发布时间】:2019-10-20 22:30:45
【问题描述】:
我需要从内联按钮编辑数据库中的记录。
- Bot 向用户发送带有记录文本的消息并为其添加内联按钮操作(即编辑、删除等)
- 用户单击按钮,从回调启动 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