log telegram error but don't fail

This commit is contained in:
j 2023-08-21 23:06:01 +02:00
parent 6018def64d
commit ae238c90f8

View file

@ -1,25 +1,33 @@
import asyncio import asyncio
import logging
import telegram import telegram
from django.conf import settings from django.conf import settings
logger = logging.getLogger(__name__)
def send(msg, to=None, channel=None, preview_url=None, preview_title=None, preview_description=None, preview_image=None): def send(msg, to=None, channel=None, preview_url=None, preview_title=None, preview_description=None, preview_image=None):
bot = telegram.Bot(token=settings.TELEGRAM_TOKEN) bot = telegram.Bot(token=settings.TELEGRAM_TOKEN)
if preview_image: try:
asyncio.run(bot.send_photo( if preview_image:
chat_id=channel, asyncio.run(bot.send_photo(
caption=msg, chat_id=channel,
photo=preview_image, caption=msg,
parse_mode='html' photo=preview_image,
)) parse_mode='html'
else: ))
asyncio.run(bot.send_message( else:
chat_id=channel, asyncio.run(bot.send_message(
text=msg, chat_id=channel,
parse_mode='html' text=msg,
)) parse_mode='html'
))
except:
logger.error("failed to send message to telegram: %s", msg, exc_info=True)
return False
return True
def send_reaction(target_address, target_ts, emoji, to=None, group=None, remove=False): def send_reaction(target_address, target_ts, emoji, to=None, group=None, remove=False):