This conversation works good but waits working time end with one user then awnser other users, I tried to find a way for this conversation to interact with multiple users at the same time but I couldn't find, How can I fix this solve this issue?
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()
as my code i check all of examples in python-telegram-bot github but i didnt understand how fix this issue
To allow your bot to answer multiple users at same time you have to pass run_async=True
in your conversation handler like:
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
)
This will make your conversation handler run asynchronously allowing multiple users to interact at same time.
If run_async
is set to True, you should not pass a handler instance, that needs to be run synchronously in another context.