fix telegram send photo

This commit is contained in:
jxxghp
2023-06-10 17:00:13 +08:00
parent 6fe8d1f04c
commit 183f9afff2

View File

@ -1,8 +1,10 @@
import threading import threading
from pathlib import Path
from threading import Event from threading import Event
from typing import Optional, List from typing import Optional, List
import telebot import telebot
from telebot.types import InputFile
from app.core.config import settings from app.core.config import settings
from app.core.context import MediaInfo, Context from app.core.context import MediaInfo, Context
@ -152,14 +154,20 @@ class Telegram(metaclass=Singleton):
""" """
if image: if image:
ret = self._bot.send_photo(chat_id=userid or self._telegram_chat_id, req = RequestUtils().get_res(image)
photo=image, if req and req.content:
caption=caption, image_file = Path(settings.TEMP_PATH) / Path(image).name
parse_mode="Markdown") image_file.write_bytes(req.content)
else: photo = InputFile(image_file)
ret = self._bot.send_message(chat_id=userid or self._telegram_chat_id, ret = self._bot.send_photo(chat_id=userid or self._telegram_chat_id,
text=caption, photo=photo,
parse_mode="Markdown") caption=caption,
parse_mode="Markdown")
if ret:
return True
ret = self._bot.send_message(chat_id=userid or self._telegram_chat_id,
text=caption,
parse_mode="Markdown")
return True if ret else False return True if ret else False