I am trying to send two messages, spaced a few seconds apart using python-telegram-bot. The first message is sent successfully however I get a "RuntimeError: Event loop is closed When Resending Message" error at the second attempt to send the message. The full traceback is below.
I am using python-telegram-bot v21.0.1 and python v3.10.
How do I fix this?
asyncio.run(bot.send_message('-0000', text=alert))
sleep(5)
asyncio.run(bot.send_message('-0000', text=alert))
Traceback:
Traceback (most recent call last):
File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\telegram\request\_baserequest.py", line 330, in _request_wrapper
code, payload = await self.do_request(
File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\telegram\request\_httpxrequest.py", line 276, in do_request
res = await self._client.request(
File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\httpx\_client.py", line 1574, in request
return await self.send(request, auth=auth, follow_redirects=follow_redirects)
File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\httpx\_client.py", line 1661, in send
response = await self._send_handling_auth(
File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\httpx\_client.py", line 1689, in _send_handling_auth
response = await self._send_handling_redirects(
File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\httpx\_client.py", line 1726, in _send_handling_redirects
response = await self._send_single_request(request)
File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\httpx\_client.py", line 1763, in _send_single_request
response = await transport.handle_async_request(request)
File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\httpx\_transports\default.py", line 373, in handle_async_request
resp = await self._pool.handle_async_request(req)
File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\httpcore\_async\connection_pool.py", line 216, in handle_async_request
raise exc from None
File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\httpcore\_async\connection_pool.py", line 196, in handle_async_request
response = await connection.handle_async_request(
File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\httpcore\_async\connection.py", line 101, in handle_async_request
return await self._connection.handle_async_request(request)
File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\httpcore\_async\http11.py", line 142, in handle_async_request
await self._response_closed()
File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\httpcore\_async\http11.py", line 257, in _response_closed
await self.aclose()
File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\httpcore\_async\http11.py", line 265, in aclose
await self._network_stream.aclose()
File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\httpcore\_backends\anyio.py", line 54, in aclose
await self._stream.aclose()
File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\anyio\streams\tls.py", line 202, in aclose
await self.transport_stream.aclose()
File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\site-packages\anyio\_backends\_asyncio.py", line 1191, in aclose
self._transport.close()
File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 109, in close
self._loop.call_soon(self._call_connection_lost, None)
File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 753, in call_soon
self._check_closed()
File "C:\Users\A\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 515, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed