AttributeError在使用python-telegram-bot时出现的__aexit__错误。

huangapple go评论79阅读模式
英文:

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/

huangapple
  • 本文由 发表于 2023年6月16日 03:17:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定