php - Telegram 不显示通过 Botman 发送的键盘
问题描述
我正在通过 Botman 和 php 为我的电报机器人创建一个进程。
在现在的情况下,我需要在用户消息字段下使用按钮制作一条消息,如下所示:
use BotMan\Drivers\Telegram\Extensions\Keyboard;
use BotMan\Drivers\Telegram\Extensions\KeyboardButton;
...
$this->ask($question, function (Answer $response) use ($action) {
...
}, Keyboard::create(Keyboard::TYPE_KEYBOARD)->addRow(
KeyboardButton::create('test 1')->requestLocation()->callbackData('test1'),
KeyboardButton::create('test 2')->requestLocation()->callbackData('test2')
)->toArray()
);
但我没有看到这些按钮。麻烦在哪里?
解决方案
在阅读了 botman telegram 驱动程序的源代码后,我发现 botman/driver-telegram ^1.6 不支持键盘。(或者我不知道它是如何开箱即用的)
我通过覆盖所有驱动程序代码来解决这个问题。像这样。将所有原始代码复制到我自己的 CustomTelegramDriver.php 并加载它
DriverManager::loadDriver(CustomTelegramDriver::class);
然后在 buildServicePayload 方法reply_markup
中检查 $additionalParameters 中的键
像这样:
if ($message instanceof Question) {
self::getLogger()->info("message instanceoff Question", ["custom_telegram_driver"]);
$parameters['text'] = $message->getText();
// Where reply_markup passed from additionalParameters!
// this line of code is my fix and it get to work keyboard
if(isset($additionalParameters['reply_markup'])) {
$parameters['reply_markup'] = $additionalParameters['reply_markup'];
} else {
$parameters['reply_markup'] = json_encode([
'inline_keyboard' => $this->convertQuestion($message)
], true);
}
}
然后在我的机器人代码中问我通过了这个
$keyboard = Keyboard::create(Keyboard::TYPE_KEYBOARD)
->oneTimeKeyboard()
->addRow(KeyboardButton::create(OnboardingConversation::translate("btn_lang_en", "en"))->callbackData('en'))
->addRow(KeyboardButton::create(OnboardingConversation::translate("btn_lang_ru", "en"))->callbackData('ru'))
->toArray();
在我的询问代码中
$question = Question::create("test");
$this->ask($question, function (Answer $answer) {
// some stuff
}, $keyboard);
推荐阅读
- java - 使用 Gradle 5.0 进行集成测试
- typescript - 如何在 TypeScript 中正确进行递归类型定义?
- javascript - 在整页脚本中使导航可点击
- cassandra - janusgraph 加载记录期间的提交性能低
- python - 在线更新 Word2Vec
- c++ - 在 C++ 中使用 std::allocator::allocate(0) 分配零个对象
- c - 如何打印我在二维数组中找到最大数字的行?
- python-3.x - 使用另一个名称为每个函数调用保存 CSV
- apache-flink - flink中job、task和subtask的区别
- java - 如何从连接三个表的 Derby 数据库中获取聚合和列数据