Skip to content

Commit d7a46d2

Browse files
author
Pietro Albini
committed
Add support for sending generic files
Added and documented the following methods: * botogram.Bot.send_file * botogram.User.send_file * botogram.Chat.send_file * botogram.Message.reply_with_file
1 parent 8959a96 commit d7a46d2

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

botogram/frozenbot.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ def send_voice(self, chat, path, duration=None, reply_to=None, extra=None):
154154
obj = objects.Chat({"id": chat, "type": ""}, self.api)
155155
obj.send_voice(path, duration, reply_to, extra)
156156

157+
def send_file(self, chat, path, reply_to=None, extra=None):
158+
"""Send a generic file in a chat"""
159+
obj = objects.Chat({"id": chat, "type": ""}, self.api)
160+
obj.send_file(path, reply_to, extra)
161+
157162
# Let's process the messages
158163

159164
def process(self, update):

botogram/objects/mixins.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,26 @@ def send_voice(self, path, duration=None, title=None, reply_to=None,
124124

125125
self._api.call("sendVoice", args, files)
126126

127+
@_require_api
128+
def send_file(self, path, reply_to=None, extra=None):
129+
"""Send a generic file"""
130+
# Convert instances of Message to ids in reply_to
131+
if hasattr(reply_to, "message_id"):
132+
reply_to = reply_to.message_id
133+
134+
# Get the correct chat_id
135+
chat_id = self.username if self.type == "channel" else self.id
136+
137+
args = {"chat_id": chat_id}
138+
if reply_to is not None:
139+
args["reply_to_message_id"] = reply_to
140+
if extra is not None:
141+
args["reply_markup"] = extra.serialize()
142+
143+
files = {"document": open(path, "rb")}
144+
145+
self._api.call("sendDocument", args, files)
146+
127147

128148
class MessageMixin:
129149
"""Add some methods for messages"""
@@ -162,6 +182,11 @@ def reply_with_voice(self, path, duration=None, extra=None):
162182
"""Reply with a voice message to the current message"""
163183
self.chat.send_voice(path, duration, self.message_id, extra)
164184

185+
@_require_api
186+
def reply_with_file(self, path, extra=None):
187+
"""Reply with a generic file to the current chat"""
188+
self.chat.send_file(path, self.message_id, extra)
189+
165190

166191
class FileMixin:
167192
"""Add some methods for files"""

docs/api/bot.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,25 @@ components.
379379
:param int duration: The message duration, in seconds
380380
:param int reply_to: The ID of the :py:class:`~botogram.Message` this one is replying to
381381
:param object extra: An extra reply interface object to attach
382+
383+
.. py:method:: send_file(chat, path, [reply_to=None, extra=None])
384+
385+
This method sends a generic file to a specific chat. The chat must be
386+
identified by its ID, and Telegram applies some restrictions on the chats
387+
allowed to receive your photo: only users who sent you a message in the
388+
past are allowed, and also the group chats your bot is currently in.
389+
390+
You must provide the *path* to the file. If the file you're sending is in
391+
reply to another message, set *reply_to* to the ID of the other
392+
:py:class:`~botogram.Message`. *extra* is an optional object which
393+
specifies additional reply interface options on the recipient's end, and
394+
can be one of the following types:
395+
396+
* :py:class:`botogram.ReplyKeyboardMarkup`
397+
* :py:class:`botogram.ReplyKeyboardHide`
398+
* :py:class:`botogram.ForceReply`
399+
400+
:param int chat: The ID of the chat which should receive the file
401+
:param str path: The path to the file
402+
:param int reply_to: The ID of the :py:class:`~botogram.Message` this one is replying to
403+
:param object extra: An extra reply interface object to attach

docs/api/telegram.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,22 @@ Available Classes
142142
:param int reply_to: The ID of the :py:class:`~botogram.Message` this one is replying to
143143
:param object extra: An extra reply interface object to attach
144144

145+
.. py:method:: send_file(path, [reply_to=None, extra=None])
146+
147+
Send the generic file found in the *path* to the user. If the file you're
148+
sending is in reply to another message, set *reply_to* to the ID of the
149+
other :py:class:`~botogram.Message`. *extra* is an optional object which
150+
specifies additional reply interface options on the recipient's end, and
151+
can be one of the following types:
152+
153+
* :py:class:`botogram.ReplyKeyboardMarkup`
154+
* :py:class:`botogram.ReplyKeyboardHide`
155+
* :py:class:`botogram.ForceReply`
156+
157+
:param str path: The path to the file
158+
:param int reply_to: The ID of the :py:class:`~botogram.Message` this one is replying to
159+
:param object extra: An extra reply interface object to attach
160+
145161

146162
.. py:class:: botogram.Chat
147163
@@ -262,6 +278,22 @@ Available Classes
262278
:param int reply_to: The ID of the :py:class:`~botogram.Message` this one is replying to
263279
:param object extra: An extra reply interface object to attach
264280

281+
.. py:method:: send_file(path, [reply_to=None, extra=None])
282+
283+
Send the generic file found in the *path* to the chat. If the file you're
284+
sending is in reply to another message, set *reply_to* to the ID of the
285+
other :py:class:`~botogram.Message`. *extra* is an optional object which
286+
specifies additional reply interface options on the recipient's end, and
287+
can be one of the following types:
288+
289+
* :py:class:`botogram.ReplyKeyboardMarkup`
290+
* :py:class:`botogram.ReplyKeyboardHide`
291+
* :py:class:`botogram.ForceReply`
292+
293+
:param str path: The path to the file
294+
:param int reply_to: The ID of the :py:class:`~botogram.Message` this one is replying to
295+
:param object extra: An extra reply interface object to attach
296+
265297

266298
.. py:class:: botogram.Message
267299
@@ -524,6 +556,21 @@ Available Classes
524556
:param int duration: The message duration, in seconds
525557
:param object extra: An extra reply interface object to attach
526558

559+
.. py:method:: send_file(path, [extra=None])
560+
561+
Reply with the generic file found in the *path* to the chat. If the file
562+
you're sending is in reply to another message, set *reply_to* to the ID
563+
of the other :py:class:`~botogram.Message`. *extra* is an optional
564+
object which specifies additional reply interface options on the
565+
recipient's end, and can be one of the following types:
566+
567+
* :py:class:`botogram.ReplyKeyboardMarkup`
568+
* :py:class:`botogram.ReplyKeyboardHide`
569+
* :py:class:`botogram.ForceReply`
570+
571+
:param str path: The path to the file
572+
:param object extra: An extra reply interface object to attach
573+
527574

528575
.. py:class:: botogram.Photo
529576

0 commit comments

Comments
 (0)