[英]Telegram does not show keyboard sent via 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 電報驅動程序的源代碼后,我發現 botman/driver-telegram ^1.6 不支持鍵盤。 (或者我不知道它是如何開箱即用的)
我通過覆蓋所有驅動程序代碼來解決這個問題。 像這樣。 將所有原始代碼復制到我自己的 CustomTelegramDriver.php 並加載它
DriverManager::loadDriver(CustomTelegramDriver::class);
然后在 buildServicePayload 方法中,我檢查 $additionalParameters 中的reply_markup
鍵
像這樣:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.