英文:
AttributeError __aexit__ when using python-telegram-bot
问题
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 a script without asyncio and it ran without any problem. Unfortunately, other users could not use this script because it said that this was concurrent code and it had to 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)
Please replace "TOKEN" and "CHANNEL" with your actual API token and channel ID.
英文:
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)
答案1
得分: 4
你正在使用 13.x 版本的库,但是只有自 20.x 版本以后才支持在 asyncio
中使用 telegram.Bot
(例如,作为异步上下文管理器,async with bot:
),请参阅:https://github.com/python-telegram-bot/python-telegram-bot/wiki/Transition-guide-to-Version-20.0#asyncio
你需要升级你正在使用的库,或者参考旧版本的文档,旧版本有不同的接口:https://docs.python-telegram-bot.org/en/v13.11/
英文:
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/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论