我有这段代码,应该在电报中查找 /help 命令。因此,一旦您在电报频道中输入 /help,它就会为您提供选项。代码如下。
from telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, CallbackContext
from telegram.ext import filters
# Define your bot token here
TOKEN = "YOUR_BOT_TOKEN"
def start(update, context):
update.message.reply_text("Welcome to your Telegram bot!")
def help_command(update, context):
update.message.reply_text("You requested help. Here are some available commands:\n"
"/help - Show this help message\n"
"/start - Start the bot")
def handle_message(update, context):
text = update.message.text
if text == '/start':
start(update, context)
elif text == '/help':
help_command(update, context)
def main():
# Initialize the Updater with your bot token
updater = Updater(token=TOKEN, use_context=True)
dispatcher = updater.dispatcher
# Define the command handlers
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("help", help_command))
# Handle non-command messages using a filter
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_message))
# Start the bot
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
但是我收到此错误
TypeError: Updater.__init__() got an unexpected keyword argument 'token'
您能否告诉我如何解决此错误。
Updater类有两个参数,
bot
和update_queue
。您需要首先创建一个 telegram.Bot 实例和 asyncio.Queue 实例:
from asyncio import Queue
from telegram import Bot
from telegram.ext import Updater
bot = Bot(TOKEN, ...)
update_queue = Queue()
updater = Updater(bot, update_queue)
我对
Telegram
一点也不熟悉,对asyncio
的了解也有限,所以这是我能提供的最大帮助。