From 6f67d16452b6f6f762e1d32f6980b41f233d169e Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 19 May 2020 17:59:48 +0200 Subject: [PATCH] Implement reply_image in Telegram serfs --- royalnet/commands/commanddata.py | 9 +++++++++ royalnet/serf/telegram/telegramserf.py | 9 ++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/royalnet/commands/commanddata.py b/royalnet/commands/commanddata.py index f4598f1d..be1e4196 100644 --- a/royalnet/commands/commanddata.py +++ b/royalnet/commands/commanddata.py @@ -3,6 +3,7 @@ import contextlib import logging import asyncio as aio import royalnet.utils as ru +import io from .errors import UnsupportedError from .commandinterface import CommandInterface from royalnet.backpack.tables.aliases import Alias @@ -51,6 +52,14 @@ class CommandData: text: The text to be sent, possibly formatted in the weird undescribed markup that I'm using.""" raise UnsupportedError(f"'{self.reply.__name__}' is not supported") + async def reply_image(self, image: io.IOBase, caption: str) -> None: + """Send an image (with optionally a caption) to the channel where the call was made. + + Parameters: + image: The bytes of the image to send. + caption: The caption to attach to the image.""" + raise UnsupportedError(f"'{self.reply_image.__name__}' is not supported") + async def get_author(self, error_if_none: bool = False): """Try to find the identifier of the user that sent the message. That probably means, the database row identifying the user. diff --git a/royalnet/serf/telegram/telegramserf.py b/royalnet/serf/telegram/telegramserf.py index 38b160d0..1899e1c7 100644 --- a/royalnet/serf/telegram/telegramserf.py +++ b/royalnet/serf/telegram/telegramserf.py @@ -8,7 +8,7 @@ import royalnet.utils as ru import royalnet.backpack.tables as rbt from .escape import escape from ..serf import Serf - +import io import telegram import urllib3 from telegram.utils.request import Request as TRequest @@ -117,6 +117,13 @@ class TelegramSerf(Serf): parse_mode="HTML", disable_web_page_preview=True) + async def reply_image(data, image: io.IOBase, caption: str) -> None: + await self.api_call(data.message.chat.send_photo, + photo=image, + caption=escape(caption), + parse_mode="HTML", + disable_web_page_preview=True) + async def get_author(data, error_if_none=False): user: Optional[telegram.User] = data.message.from_user if user is None: