首页 > 解决方案 > 如何在 python Telegram bot 对话处理程序中循环

问题描述

bot 运行一次后不会返回到开始消息。

import logging
from config import TOKEN, hello_msg, key1_msg
import mysql.connector
from telegram.ext import Filters, ConversationHandler, CommandHandler, MessageHandler, 
Filters, Updater, InlineQueryHandler, CallbackQueryHandler, CallbackContext
from telegram import InlineQueryResultArticle, InputTextMessageContent, 
InlineKeyboardButton, InlineKeyboardMarkup, Update

CHOOSING, CHOOSING2 = range(2)


keyboard1 = [
[InlineKeyboardButton("Welcome Message", callback_data = 'welcome_Message')],
[InlineKeyboardButton("Commands", callback_data = 'Commands')],
[InlineKeyboardButton("Links to our website", callback_data = 'links_to_our_websites')],
[InlineKeyboardButton("Demo Try", callback_data = 'Demo_try')],
[InlineKeyboardButton("register", callback_data = 'register')],
[InlineKeyboardButton("Contact us", callback_data = 'Contact_us')]
]

def start(update: Update, context: CallbackContext):
    global up, cal
    up = update
    cal = CallbackContext
    context.bot.send_message(chat_id = update.effective_chat.id, text = hello_msg)
    reply_markup = InlineKeyboardMarkup(keyboard1)
    update.message.reply_text("please Choose: ", reply_markup = reply_markup)
    return CHOOSING

def button1(update: Update, context: CallbackContext):
    query = update.callback_query
    query.answer()
    answer = query.data
    context.bot.send_message(chat_id = update.effective_chat.id, text = key1_msg[answer])
    return CHOOSING


def main():
    updater = Updater(TOKEN, use_context=True)
    dp = updater.dispatcher

    new = ConversationHandler(entry_points = [CommandHandler("start", start)], states = {CHOOSING : [MessageHandler(Filters.regex('^(Welcome Message|Commands|Links to our website|Demo Try|register|Contact us)$'), start)]},fallbacks=[MessageHandler(Filters.regex('^start$'), sart)])

    dp.add_handler(new)
    dp.add_handler(CallbackQueryHandler(button1))
    updater.start_polling()
    updater.idle()

if __name__ == "__main__":
    main()

我想在用户从按钮中选择一个选项后立即再次运行启动命令......我想学习如何在对话处理程序中提供处理程序时传递参数。谢谢你。

标签: pythontelegramtelegram-botpython-telegram-botpy-telegram-bot-api

解决方案


推荐阅读