2017-06-14 17 views
1

我想补充像下面进入我的电报BOT消息自定义按钮,但我不知道如何将它们嵌入到我的信息与PythonPython的按钮选项

我想嵌入它进入此消息

def run(results, index, msgText): 
# The message below this! 
    msg = '<b>Hello there, ' + results[index]['message']['from']['username'] + '!</b> I am <b>Makuna Hattata</b>, a spin-off bot of the original <b>Hakuna Mattata</b> bot \n\nI am a semi-smart bot capable of understanding basic commands, which you can see by pressing the button below!' 
    try: 
     sendMessage(index, results, msg, '&parse_mode=html') 
     logWorked(results[index]['message']['from']['username'], results[index]['message']['from']['id'], "/start message sent") 
    except Exception as err: 
     logError(results[index ]['message']['from']['username'], results[index]['message']['from']['id'], "/start message sent", err) 

回答

0

您可以简单地发送一个自定义键盘。首先你需要telegramapi。您始终可以使用ReplyKeyboardMarkupcommand。它会是这个样子:

keyboard = [["key1", "key2"], ["return"]] 
reply_markup = ReplyKeyboardMarkup.create(keyboard) 
bot.send_message(user_id, 'testing keyboard', reply_markup=reply_markup) 

如果你不想导入任何其他操作,您可以使用下面的函数(它实际上会给你很多的工具,如果你做的工作!):

def send_keyboard(token, chat_id, text, keyboard=[], one_time=True, resize=True): 

    if text is None or len(text) <= 0: 
     return None 

    post_data = { 
     "chat_id": chat_id, 
     "text": text 
    } 

    if len(keyboard) == 0: 
     return None 

    post_data["reply_markup"] = { 
     "keyboard": keyboard, 
     "one_time_keyboard": one_time, 
     "resize_keyboard": resize 
    } 

    headers = { 
     "Content-Type": "application/json", 
     "Content-Length": len(json.dumps(post_data)) 
    } 

    url = TELEGRAM_HOSTNAME + "/bot" + token + "/sendMessage" 
    try: 
     response = requests.post(url, headers=headers, data=json.dumps(post_data)) 
    except: 
     return None 

    if response.status_code >= 400: 
     return None 

    return response 

你只需要给它chat_id,令牌,消息文本和键盘的按键。

+0

任何没有API的方法吗?我试图不使用API​​。 – Splinxyy

+0

我添加了一些代码,看看它是否能解决你的问题。 – cookiedough

+0

因此,缩短了,我把它发送到sendmessage的'reply_markup'参数中? – Splinxyy