본문 바로가기
컴퓨터

python-telegram-bot v20.0 에러 날 때 해결방법(RuntimeWarning: coroutine 'Bot.send_message' was never awaited)

by 사막장 2023. 1. 12.

기존 python-telegram-bot 에선 다음과 같이 심플하게 메세지를 보낼 수 있었다.

import telegram

bot = telegram.Bot(token='<본인의 token>')
chat_id = <본인의 chat_id>

bot.send_message(chat_id=chat_id, text="보낼내용")

그런데 v20 으로 업데이트 되며 이 방법으로 메세지를 보내려고 하면

RuntimeWarning: coroutine 'Bot.send_message' was never awaited

라는 에러메세지를 뿜으며 작동되지않는다.

파이썬 asyncio 를 이용해서 간단히 해결할 수 있다.

import telegram
import asyncio

bot = telegram.Bot(token='<본인의 token>')
chat_id = <본인의 chat_id>

asyncio.run(bot.send_message(chat_id=chat_id, text="MESSAGE"))

굳이 텔레그램 봇으로 실시간 대화를 주고받아야 하는게 아니고,

나처럼 알람봇으로만 사용한다면 요정도가 가장 간단한 해결방법이 아닐까 싶다.

댓글