bot will not call back the command processing function when receiving the command #3974
-
I tried to write a simple bot program. When I sent a command to the bot, I could see on the console that the bot received the command but did not call the callback function. import asyncio
import logging
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import Update, InputMediaPhoto, BotCommand
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters
logger = logging.getLogger("bot")
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
do_stop_bot = False
async def stop_bot(update: Update, context: ContextTypes.DEFAULT_TYPE):
logger.info("stop attempt")
logger.info("stop")
await context.bot.sendMessage(chat_id=update.effective_chat.id, text="completed")
global do_stop_bot
do_stop_bot = True
async def block():
while not do_stop_bot:
await asyncio.sleep(1)
async def run_bot(application: Application):
application.add_handler(CommandHandler("stop_bot", stop_bot))
# run bot
await application.initialize()
await application.start()
await application.updater.start_polling(allowed_updates=Update.ALL_TYPES, read_timeout=600, write_timeout=600
, pool_timeout=600, connect_timeout=600, timeout=600)
logger.info("bot start")
await block()
# stop bot
await application.updater.stop()
await application.stop()
await application.shutdown()
logger.info("bot stop")
def run():
application = Application.builder().token("api key").build()
asyncio.run(run_bot(application))
run() In addition, this code runs normally under python3.11 on the Windows 11, but cannot run normally on python3.9 and 3.8 on the Debian 11. |
Beta Was this translation helpful? Give feedback.
Answered by
Bibo-Joshi
Nov 13, 2023
Replies: 1 comment
-
Looks like the same issue as in #3669 :) tl;dr: move |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
stay-miku
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks like the same issue as in #3669 :) tl;dr: move
application = Application.builder()…
to somewhere withinasyncio.run