Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在电报机器人中用键盘回复?_Python_Python 3.x_Telegram_Telegram Bot_Python Telegram Bot - Fatal编程技术网

Python 如何在电报机器人中用键盘回复?

Python 如何在电报机器人中用键盘回复?,python,python-3.x,telegram,telegram-bot,python-telegram-bot,Python,Python 3.x,Telegram,Telegram Bot,Python Telegram Bot,我有一个电报机器人,它应该在/faq命令和每个/faq键盘选项上显示键盘1,以可视化一些文本(文本尚未添加) 这是我创建的第一个机器人,存在一些问题: 机器人不连续地监听命令 在/faq 另一件奇怪的事情是,它保留了代码中不再存在的键盘选项,我的意思是我仍然可以看到以前代码版本中创建的inlinekeyboard,我不知道如何摆脱它。有人能帮忙吗?代码如下: 您似乎同时使用了这两个选项,这可能会导致您的问题 AS代码> TeleBoot似乎还不支持您使用的所有特征(例如),您可能只考虑使用 P

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

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

  • 机器人不连续地监听命令
  • /faq
  • 另一件奇怪的事情是,它保留了代码中不再存在的键盘选项,我的意思是我仍然可以看到以前代码版本中创建的inlinekeyboard,我不知道如何摆脱它。有人能帮忙吗?代码如下:
  • 您似乎同时使用了这两个选项,这可能会导致您的问题

    AS代码> TeleBoot似乎还不支持您使用的所有特征(例如),您可能只考虑使用<代码> Python电报BOT。

    您似乎同时使用这两种方法,这可能会引起您的问题。


    AS代码> TeleBoot似乎还不支持您使用的所有特征(例如),您可能只考虑使用<代码> Python电报BOT。那么我应该导入什么以及如何定义bot=telebot.telebot('TOKEN')?非常感谢您可以删除该行以及telebot导入。您已经使用的

    更新程序
    命令处理程序
    足以满足基本要求。您还可以删除
    消息处理程序
    带注释的函数,并注册命令,如
    启动
    命令。请参阅和。为一个愚蠢的问题道歉,但如果删除telebot导入,@bot.message_handler(commands=['start'])有一个问题,因为未定义bot,应该使用什么来代替它?也请删除这些,因为它们用于将命令映射到函数,
    python电报bot
    所做的,例如使用
    add\u handler
    方法,您可以在示例bot中看到,如所述的
    echobot2
    。谢谢!那么我应该导入什么以及如何定义bot=telebot.telebot('TOKEN')?非常感谢您可以删除该行以及telebot导入。您已经使用的
    更新程序
    命令处理程序
    足以满足基本要求。您还可以删除
    消息处理程序
    带注释的函数,并注册命令,如
    启动
    命令。请参阅和。为一个愚蠢的问题道歉,但如果删除telebot导入,@bot.message_handler(commands=['start'])有一个问题,因为未定义bot,应该使用什么来代替它?也请删除这些,因为它们用于将命令映射到函数,
    python电报bot
    所做的,例如使用
    add\u handler
    方法,您可以在示例bot中看到,如所述的
    echobot2
    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()