如何在对接Telegram的Rasa聊天机器人中设置独立行按钮
Rasa对接Telegram按钮布局优化及utter_message传参方案
一、实现按钮每行单独显示+宽度适配标题
Telegram的键盘按钮布局由Rasa传递的二维数组控制,同时需开启自适应键盘参数:
- 调整按钮为二维数组结构:把每个按钮单独放在一个子列表中,确保每行仅显示一个按钮
# domain.yml中的示例配置 utter_sample_buttons: - text: "" buttons: - [{"title": "完整显示的长标题按钮1", "payload": "/intent1"}] - [{"title": "完整显示的长标题按钮2", "payload": "/intent2"}] custom: resize_keyboard: true # 开启后按钮宽度自动适配标题长度 - 核心逻辑说明:
- 一维数组的按钮会被Telegram横向排列在同一行
- 二维数组的每个子列表对应一行按钮
resize_keyboard: true让Telegram根据按钮标题长度自动调整宽度,避免标题截断
二、在utter_message中仅传递按钮
如果不需要发送文本内容,只需传递按钮,直接将text设为空字符串即可:
方式1:在domain.yml中配置utterance
utter_only_buttons: - text: "" buttons: - [{"title": "按钮A", "payload": "/intent_a"}] - [{"title": "按钮B", "payload": "/intent_b"}] custom: resize_keyboard: true
方式2:在自定义Python动作中调用
from rasa_sdk import Action, Tracker from rasa_sdk.executor import CollectingDispatcher class ActionOnlyButtons(Action): def name(self) -> str: return "action_only_buttons" def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: dict) -> list: dispatcher.utter_message( text="", buttons=[ [{"title": "按钮A", "payload": "/intent_a"}], [{"title": "按钮B", "payload": "/intent_b"}] ], custom={"resize_keyboard": True} ) return []
内容的提问来源于stack exchange,提问作者gopal shendge
相关产品推荐
相关产品推荐

