Why does bot reply to messages, that where sent while he was inactive #3806
-
this is snippet from my project def start_bot():
global loop, chat_queue, app
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
print('starting bot')
chat_queue= []
defaults = Defaults(parse_mode=ParseMode.HTML, tzinfo=pytz.timezone(COUNTRY))
app = (
Application.builder()
.token(TOKEN)
.defaults(defaults)
.build()
)
filter_users = TypeHandler(Update, whitelist_user)
app.add_handler(filter_users, -1)
# Commands
app.add_handler(CommandHandler('start', start_command))
app.add_handler(CommandHandler('help', help_command))
app.add_handler(CommandHandler('custom', custom_command))
app.add_handler(CommandHandler('sticker', send_sticker))
# Messages
app.add_handler(MessageHandler(filters.TEXT, handle_message))
app.add_handler(MessageHandler(filters.VOICE, handle_voice))
# Error
app.add_error_handler(error)
user_states = {}
app.context_types.context.user_states = user_states
# chat_queue_thread = threading.Thread(target=process_chat_queue)
# chat_queue_thread.start()
print('pooling')
try:
loop.run_until_complete(app.run_polling())
except RuntimeError as e:
if str(e) == 'Event loop is closed':
print('Bot stopped gracefully')
else:
raise
if loop is not None:
loop.close()
# def start_bot_in_thread():
# Flag to indicate whether the bot should stop
# Stop the bot gracefully
def stop_bot():
global bot_thread, app
global loop
if loop is not None:
loop.stop()
loop = None
def gui():
def start_bot_with_delay():
global chat_queue
chat_queue = [] # Clear the chat queue
bot_thread = threading.Thread(target=start_bot)
bot_thread.start()
time.sleep(1)
print('\n\nafter')
customtkinter.set_appearance_mode("System") # Modes: system (default), light, dark
customtkinter.set_default_color_theme("blue") # Themes: blue (default), dark-blue, green
app = customtkinter.CTk()
app.geometry("400x240")
stop_button = customtkinter.CTkButton(master=app, text="Stop", command=stop_bot)
stop_button.place(relx=0.7, rely=0.5, anchor=customtkinter.CENTER)
button = customtkinter.CTkButton(master=app, text="CTkButton", command=start_bot_with_delay)
button.place(relx=0.5, rely=0.5, anchor=customtkinter.CENTER)
app.mainloop()
if __name__ == '__main__':
gui() Overall i have simple gui to stop and activate the bot, when i stopped him and message something via telegram and ONLY AFTER that i activate him it will start replying to messages that were sent while he was inactive. (when user messages something to bot it adds to a queue but when startnig the bot, thread or even in the function start_bot() i make sure that this queue will be empty) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
also sorry for bad formatting i used this emoji for code, but it sill kinda broke |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
run_polling
/run_webhook
/get_updates
all have a parameter calleddrop_pendinge_updates
Application.run_polling
is not a coroutine function, passing it torun_until_complete
makes no sense.