Issue
I have a script that should send an image to a telegram channel but I am getting the error message AttributeError __aexit__
. I am new to asyncio library and I am trying to understand what is going on but I have no idea what the problem could be.
This is the current script I am trying to run:
import sys
import asyncio
import telegram
async def main():
bot = telegram.Bot("TOKEN")
chat_id = "CHANNEL"
filename = sys.argv[1]
caption = sys.argv[2]
async with bot:
await bot.send_photo(chat_id=chat_id, photo=open(filename, 'rb'), caption=caption)
if __name__ == '__main__':
asyncio.run(main())
I was using this documentation: https://github.com/python-telegram-bot/python-telegram-bot/wiki/Introduction-to-the-API
I am using python-telegram-bot version 13.11.
Note: TOKEN and CHANNEL should be replaced with the API token and channel respectively.
I was previously using the a script without asyncio and it ran without any problem. Unfortunately other user could not use this script because it said that this was a concurrent code and it had be awaited. This is the old script (working for only some users):
import sys
import telegram
bot = telegram.Bot("TOKEN")
chat_id = "CHANNEL"
filename = sys.argv[1]
caption = sys.argv[2]
bot.send_photo(chat_id=chat_id, photo=open(filename, 'rb'), caption=caption)
Solution
You are using version 13.x of the library, but using telegram.Bot
with asyncio
(e.g. as an async context manager, async with bot:
) is only supported since version 20.x, see: https://github.com/python-telegram-bot/python-telegram-bot/wiki/Transition-guide-to-Version-20.0#asyncio
You need to upgrade the library you are using or refer to the documentation of the older version, which has a different interface: https://docs.python-telegram-bot.org/en/v13.11/
Answered By - mkrieger1
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.