如何在电报Bot中用键盘回复?

时间:2019-10-22 12:56:40

标签: python python-3.x telegram telegram-bot python-telegram-bot

我有一个电报机器人,该机器人应该在/faq命令和每个/faq键盘选项上显示keyboard1以可视化一些文本(文本尚未添加)。

这是我创建的第一个机器人,并且存在一些问题:

  1. 机器人无法连续监听命令
  2. 它在/faq上没有显示keyboard1
  3. 另一个奇怪的事情,它保留了代码中不再存在的键盘选项,我的意思是我仍然可以看到在以前的代码版本中创建的inlinekeyboard,而且我不知道如何摆脱它。有人可以帮忙吗?这是代码:
import telebot
from tkinter import *
from telegram.ext import Updater
from telegram.ext import CommandHandler, CallbackQueryHandler
from telegram import InlineKeyboardButton, InlineKeyboardMarkup

bot = telebot.TeleBot('TOKEN')
updater = Updater('TOKEN', use_context=True)
updater.dispatcher.add_handler(CommandHandler('start', ...))

@bot.message_handler(commands=['help','start'])
def send_welcome(message):
    if message.text == "/start":
        updater.send_message(message.chat.id,'Some welcome text')
    elif message.text == "/help":
            updater.send_message(message.chat.id,
                             'list of all commands')
  elif message.text == "/lastin":
            updater.send_message(message.chat.id,
                             'some text')
def build_menu(buttons,
               n_cols,
               header_buttons=None,
               footer_buttons=None):
    menu = [buttons[i:i + n_cols] for i in range(0, len(buttons), n_cols)]
    if header_buttons:
        menu.insert(0, [header_buttons])
    if footer_buttons:
        menu.append([footer_buttons])
    return menu

keyboard1 = [[InlineKeyboardButton('Question 1', callback_data='q1')],
            [InlineKeyboardButton('Question 2', callback_data='q2')],
            [InlineKeyboardButton('Question 3', callback_data='q3')],
            [InlineKeyboardButton('Question 4', callback_data='q4')]]
reply_markup = InlineKeyboardMarkup(build_menu(keyboard1, n_cols=2))

@bot.message_handler(content_types=['text'])
def send_text(message):
    if message.text == "/faq":
        bot.send_message(message.chat.id, text = 'FAQ', reply_markup = reply_markup)
    elif message.text == "/lastin":
        bot.send_photo(message.chat.id, photo='https://telegram.org/img/t_logo.png')


updater.start_polling()

1 个答案:

答案 0 :(得分:0)

您似乎同时使用python-telegram-bottelebot,这可能会引起问题。

由于telebot似乎还不支持您正在使用的所有功能(例如send_photo),因此您可能要考虑专门使用python-telegram-bot