我用 python-telegram-bot 写了一个对话,但多个用户不能同时与 bot 交互
I wrote a Conversation with python-telegram-bot but multiple user cant interact with bot at the same time
这个对话很好,但是等待一个用户的工作时间结束,然后 awnser 其他用户,我试图找到一种方法让这个对话同时与多个用户交互,但我找不到,我怎么能解决这个问题?
from telegram import Update, ReplyKeyboardRemove
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext, ConversationHandler
from datetime import datetime
GET_START_DATE, GET_END_DATE, CALCULATE = map(chr, range(3))
END = ConversationHandler.END
def get_start_date(update: Update, context: CallbackContext) -> int:
update.message.reply_text(
'\U0001F4C5 start date')
update.message.reply_text(
'\U00002705 Example :10-04-2022')
return GET_START_DATE
def get_end_date(update: Update, context: CallbackContext) -> int:
context.user_data['start_date'] = update.message.text
update.message.reply_text(
'\U0001F4C5 end date :')
update.message.reply_text(
'\U00002705 Example :20-04-2022')
return GET_END_DATE
def get_file(update: Update, context: CallbackContext) -> int:
context.user_data['end_date'] = update.message.text
days = (datetime.strptime(context.user_data['end_date'], "%Y-%m-%d") - datetime.strptime(
context.user_data['start_date'], "%Y-%m-%d")).days
if days <= 125:
update.message.reply_text(
'\U0001F4C1 upload file plz')
return CALCULATE
else:
update.message.reply_text(
'\U0000274C too long days')
update.message.reply_text(
' \U000021A9 again test /command')
return ConversationHandler.END
def calculate(update: Update, context: CallbackContext) -> int:
try:
# calculate something here
return ConversationHandler.END
except:
update.message.reply_text(
'\U0000274C something wrong try again /command ')
return ConversationHandler.END
def cancel(update: Update, context: CallbackContext) -> int:
update.message.reply_text(
'Bye', reply_markup=ReplyKeyboardRemove()
)
return ConversationHandler.END
def main() -> None:
updater = Updater("TOKEN_BOT_TEST")
dispatcher = updater.dispatcher
conv_handler_test = ConversationHandler(
entry_points=[CommandHandler('test', get_start_date)],
states={
GET_START_DATE: [MessageHandler(Filters.text, get_end_date)],
GET_END_DATE: [MessageHandler(Filters.text, get_file)],
CALCULATE: [MessageHandler(Filters.document, calculate)]
},
fallbacks=[CommandHandler('cancel', cancel)],
map_to_parent={
END: END,
},
)
dispatcher.add_handler(conv_handler_test)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
作为我的代码,我检查了 python-telegram-bot github 中的所有示例,但我不明白如何解决这个问题
要让您的机器人同时回答多个用户,您必须在对话处理程序中传递 run_async=True
,例如:
conv_handler_test = ConversationHandler(
entry_points=[CommandHandler('test', get_start_date)],
states={
GET_START_DATE: [MessageHandler(Filters.text, get_end_date)],
GET_END_DATE: [MessageHandler(Filters.text, get_file)],
CALCULATE: [MessageHandler(Filters.document, calculate)]
},
fallbacks=[CommandHandler('cancel', cancel)],
map_to_parent={
END: END,
},
run_async=True
)
这将使您的对话处理程序运行异步允许多个用户同时进行交互。
如果 run_async
设置为 True,则不应传递处理程序实例,它需要在另一个上下文中同步 运行。
这个对话很好,但是等待一个用户的工作时间结束,然后 awnser 其他用户,我试图找到一种方法让这个对话同时与多个用户交互,但我找不到,我怎么能解决这个问题?
from telegram import Update, ReplyKeyboardRemove
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext, ConversationHandler
from datetime import datetime
GET_START_DATE, GET_END_DATE, CALCULATE = map(chr, range(3))
END = ConversationHandler.END
def get_start_date(update: Update, context: CallbackContext) -> int:
update.message.reply_text(
'\U0001F4C5 start date')
update.message.reply_text(
'\U00002705 Example :10-04-2022')
return GET_START_DATE
def get_end_date(update: Update, context: CallbackContext) -> int:
context.user_data['start_date'] = update.message.text
update.message.reply_text(
'\U0001F4C5 end date :')
update.message.reply_text(
'\U00002705 Example :20-04-2022')
return GET_END_DATE
def get_file(update: Update, context: CallbackContext) -> int:
context.user_data['end_date'] = update.message.text
days = (datetime.strptime(context.user_data['end_date'], "%Y-%m-%d") - datetime.strptime(
context.user_data['start_date'], "%Y-%m-%d")).days
if days <= 125:
update.message.reply_text(
'\U0001F4C1 upload file plz')
return CALCULATE
else:
update.message.reply_text(
'\U0000274C too long days')
update.message.reply_text(
' \U000021A9 again test /command')
return ConversationHandler.END
def calculate(update: Update, context: CallbackContext) -> int:
try:
# calculate something here
return ConversationHandler.END
except:
update.message.reply_text(
'\U0000274C something wrong try again /command ')
return ConversationHandler.END
def cancel(update: Update, context: CallbackContext) -> int:
update.message.reply_text(
'Bye', reply_markup=ReplyKeyboardRemove()
)
return ConversationHandler.END
def main() -> None:
updater = Updater("TOKEN_BOT_TEST")
dispatcher = updater.dispatcher
conv_handler_test = ConversationHandler(
entry_points=[CommandHandler('test', get_start_date)],
states={
GET_START_DATE: [MessageHandler(Filters.text, get_end_date)],
GET_END_DATE: [MessageHandler(Filters.text, get_file)],
CALCULATE: [MessageHandler(Filters.document, calculate)]
},
fallbacks=[CommandHandler('cancel', cancel)],
map_to_parent={
END: END,
},
)
dispatcher.add_handler(conv_handler_test)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
作为我的代码,我检查了 python-telegram-bot github 中的所有示例,但我不明白如何解决这个问题
要让您的机器人同时回答多个用户,您必须在对话处理程序中传递 run_async=True
,例如:
conv_handler_test = ConversationHandler(
entry_points=[CommandHandler('test', get_start_date)],
states={
GET_START_DATE: [MessageHandler(Filters.text, get_end_date)],
GET_END_DATE: [MessageHandler(Filters.text, get_file)],
CALCULATE: [MessageHandler(Filters.document, calculate)]
},
fallbacks=[CommandHandler('cancel', cancel)],
map_to_parent={
END: END,
},
run_async=True
)
这将使您的对话处理程序运行异步允许多个用户同时进行交互。
如果 run_async
设置为 True,则不应传递处理程序实例,它需要在另一个上下文中同步 运行。