Skip to content

[FEATURE] Send message in shutdown handler #3454

@djmattyg007

Description

@djmattyg007

What kind of feature are you missing? Where do you notice a shortcoming of PTB?

I tried using the post_shutdown hook in the Application class to run some code like this:

async def notify_shutdown(app: Application):
    await app.bot.send_message(chat_id=MY_USER_ID, text="<b>NOTE!</b> Bot shutting down", parse_mode=ParseMode.HTML)

Unfortunately an exception was thrown:

telegram.error.NetworkError: Unknown error in HTTP implementation: RuntimeError('This HTTPXRequest is not initialized!')

Describe the solution you'd like

I'd like a pre-shutdown hook that runs before Application.stop() has completed. This would presumably allow me to send messages as part of the shutdown process.

Describe alternatives you've considered

I haven't considered any alternatives, as this isn't critical functionality I need.

Additional context

I'm running version 20.0a6.

Here's the full stack trace. It's taken from journalctl output, so it also has timestamps and other context.

Jan 01 11:42:45 shacklebolt python[1042542]: 2023-01-01 11:42:45,423 - telegram.ext._application - INFO - Application is stopping. This might take a moment.
Jan 01 11:42:45 shacklebolt python[1042542]: 2023-01-01 11:42:45,423 - apscheduler.scheduler - INFO - Scheduler has been shut down
Jan 01 11:42:45 shacklebolt python[1042542]: 2023-01-01 11:42:45,434 - telegram.ext._application - INFO - Application.stop() complete
Jan 01 11:42:45 shacklebolt python[1042542]: Traceback (most recent call last):
Jan 01 11:42:45 shacklebolt python[1042542]:   File "/usr/lib/python3.10/site-packages/telegram/request/_baserequest.py", line 276, in _request_wrapper
Jan 01 11:42:45 shacklebolt python[1042542]:     code, payload = await self.do_request(
Jan 01 11:42:45 shacklebolt python[1042542]:   File "/usr/lib/python3.10/site-packages/telegram/request/_httpxrequest.py", line 153, in do_request
Jan 01 11:42:45 shacklebolt python[1042542]:     raise RuntimeError("This HTTPXRequest is not initialized!")
Jan 01 11:42:45 shacklebolt python[1042542]: RuntimeError: This HTTPXRequest is not initialized!
Jan 01 11:42:45 shacklebolt python[1042542]: The above exception was the direct cause of the following exception:
Jan 01 11:42:45 shacklebolt python[1042542]: Traceback (most recent call last):
Jan 01 11:42:45 shacklebolt python[1042542]:   File "/usr/local/lib/telegram-bots/shacklebot-admin/app.py", line 183, in <module>
Jan 01 11:42:45 shacklebolt python[1042542]:     tg_app.run_polling()
Jan 01 11:42:45 shacklebolt python[1042542]:   File "/usr/lib/python3.10/site-packages/telegram/ext/_application.py", line 670, in run_polling
Jan 01 11:42:45 shacklebolt python[1042542]:     return self.__run(
Jan 01 11:42:45 shacklebolt python[1042542]:   File "/usr/lib/python3.10/site-packages/telegram/ext/_application.py", line 869, in __run
Jan 01 11:42:45 shacklebolt python[1042542]:     loop.run_until_complete(self.post_shutdown(self))
Jan 01 11:42:45 shacklebolt python[1042542]:   File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
Jan 01 11:42:45 shacklebolt python[1042542]:     return future.result()
Jan 01 11:42:45 shacklebolt python[1042542]:   File "/usr/local/lib/telegram-bots/shacklebot-admin/app.py", line 53, in notify_shutdown
Jan 01 11:42:45 shacklebolt python[1042542]:     await notify_known_users(app.bot, "Shacklebot Admin shutdown")
Jan 01 11:42:45 shacklebolt python[1042542]:   File "/usr/local/lib/telegram-bots/shacklebot-admin/app.py", line 45, in notify_known_users
Jan 01 11:42:45 shacklebolt python[1042542]:     await bot.send_message(chat_id=user_id, text=msg.strip(), parse_mode=ParseMode.HTML)
Jan 01 11:42:45 shacklebolt python[1042542]:   File "/usr/lib/python3.10/site-packages/telegram/ext/_extbot.py", line 2474, in send_message
Jan 01 11:42:45 shacklebolt python[1042542]:     return await super().send_message(
Jan 01 11:42:45 shacklebolt python[1042542]:   File "/usr/lib/python3.10/site-packages/telegram/_bot.py", line 313, in decorator
Jan 01 11:42:45 shacklebolt python[1042542]:     result = await func(*args, **kwargs)  # skipcq: PYL-E1102
Jan 01 11:42:45 shacklebolt python[1042542]:   File "/usr/lib/python3.10/site-packages/telegram/_bot.py", line 723, in send_message
Jan 01 11:42:45 shacklebolt python[1042542]:     return await self._send_message(  # type: ignore[return-value]
Jan 01 11:42:45 shacklebolt python[1042542]:   File "/usr/lib/python3.10/site-packages/telegram/ext/_extbot.py", line 481, in _send_message
Jan 01 11:42:45 shacklebolt python[1042542]:     result = await super()._send_message(
Jan 01 11:42:45 shacklebolt python[1042542]:   File "/usr/lib/python3.10/site-packages/telegram/_bot.py", line 467, in _send_message
Jan 01 11:42:45 shacklebolt python[1042542]:     result = await self._post(
Jan 01 11:42:45 shacklebolt python[1042542]:   File "/usr/lib/python3.10/site-packages/telegram/_bot.py", line 395, in _post
Jan 01 11:42:45 shacklebolt python[1042542]:     return await self._do_post(
Jan 01 11:42:45 shacklebolt python[1042542]:   File "/usr/lib/python3.10/site-packages/telegram/ext/_extbot.py", line 306, in _do_post
Jan 01 11:42:45 shacklebolt python[1042542]:     return await super()._do_post(
Jan 01 11:42:45 shacklebolt python[1042542]:   File "/usr/lib/python3.10/site-packages/telegram/_bot.py", line 426, in _do_post
Jan 01 11:42:45 shacklebolt python[1042542]:     return await request.post(
Jan 01 11:42:45 shacklebolt python[1042542]:   File "/usr/lib/python3.10/site-packages/telegram/request/_baserequest.py", line 167, in post
Jan 01 11:42:45 shacklebolt python[1042542]:     result = await self._request_wrapper(
Jan 01 11:42:45 shacklebolt python[1042542]:   File "/usr/lib/python3.10/site-packages/telegram/request/_baserequest.py", line 292, in _request_wrapper
Jan 01 11:42:45 shacklebolt python[1042542]:     raise NetworkError(f"Unknown error in HTTP implementation: {repr(exc)}") from exc
Jan 01 11:42:45 shacklebolt python[1042542]: telegram.error.NetworkError: Unknown error in HTTP implementation: RuntimeError('This HTTPXRequest is not initialized!')

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions