Skip to content

Commit 5a5cf5b

Browse files
matteob99renyhp
authored andcommitted
Add ability to send files by ID or URL
Co-authored-by: renyhp <[email protected]>
1 parent f0ce5ad commit 5a5cf5b

File tree

2 files changed

+176
-54
lines changed

2 files changed

+176
-54
lines changed

botogram/objects/mixins.py

Lines changed: 78 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,34 @@ def send(self, message, preview=True, reply_to=None, syntax=None,
9090
return self._api.call("sendMessage", args, expect=_objects().Message)
9191

9292
@_require_api
93-
def send_photo(self, path, caption=None, reply_to=None, extra=None,
94-
attach=None, notify=True):
93+
def send_photo(self, path=None, file_id=None, url=None, caption=None,
94+
reply_to=None, extra=None, attach=None, notify=True):
9595
"""Send a photo"""
9696
args = self._get_call_args(reply_to, extra, attach, notify)
9797
if caption is not None:
9898
args["caption"] = caption
9999

100-
files = {"photo": open(path, "rb")}
100+
if path is not None and file_id is None and url is None:
101+
files = {"photo": open(path, "rb")}
102+
elif file_id is not None and path is None and url is None:
103+
args["photo"] = file_id
104+
files = None
105+
elif url is not None and file_id is None and path is None:
106+
args["photo"] = url
107+
files = None
108+
elif path is None and file_id is None and url is None:
109+
raise TypeError("path or file_id or URL is missing")
110+
else:
111+
raise TypeError("Only one among path, file_id and URL must be" +
112+
"passed")
101113

102114
return self._api.call("sendPhoto", args, files,
103115
expect=_objects().Message)
104116

105117
@_require_api
106-
def send_audio(self, path, duration=None, performer=None,
107-
title=None, reply_to=None, extra=None, attach=None,
108-
notify=True, caption=None):
118+
def send_audio(self, path=None, file_id=None, url=None, duration=None,
119+
performer=None, title=None, reply_to=None,
120+
extra=None, attach=None, notify=True, caption=None):
109121
"""Send an audio track"""
110122
args = self._get_call_args(reply_to, extra, attach, notify)
111123
if caption is not None:
@@ -117,50 +129,100 @@ def send_audio(self, path, duration=None, performer=None,
117129
if title is not None:
118130
args["title"] = title
119131

120-
files = {"audio": open(path, "rb")}
132+
if path is not None and file_id is None and url is None:
133+
files = {"audio": open(path, "rb")}
134+
elif file_id is not None and path is None and url is None:
135+
files = None
136+
args["audio"] = file_id
137+
elif url is not None and file_id is None and path is None:
138+
args["audio"] = url
139+
files = None
140+
elif path is None and file_id is None and url is None:
141+
raise TypeError("path or file_id or URL is missing")
142+
else:
143+
raise TypeError("Only one among path, file_id and URL must be" +
144+
"passed")
121145

122146
return self._api.call("sendAudio", args, files,
123147
expect=_objects().Message)
124148

125149
@_require_api
126-
def send_voice(self, path, duration=None, title=None, reply_to=None,
127-
extra=None, attach=None, notify=True, caption=None):
150+
def send_voice(self, path=None, file_id=None, url=None, duration=None,
151+
title=None, reply_to=None, extra=None, attach=None,
152+
notify=True, caption=None):
128153
"""Send a voice message"""
129154
args = self._get_call_args(reply_to, extra, attach, notify)
130155
if caption is not None:
131156
args["caption"] = caption
132157
if duration is not None:
133158
args["duration"] = duration
134159

135-
files = {"voice": open(path, "rb")}
160+
if path is not None and file_id is None and url is None:
161+
files = {"voice": open(path, "rb")}
162+
elif file_id is not None and path is None and url is None:
163+
files = None
164+
args["voice"] = file_id
165+
elif url is not None and file_id is None and path is None:
166+
args["voice"] = url
167+
files = None
168+
elif path is None and file_id is None and url is None:
169+
raise TypeError("path or file_id or URL is missing")
170+
else:
171+
raise TypeError("Only one among path, file_id and URL must be" +
172+
"passed")
136173

137174
return self._api.call("sendVoice", args, files,
138175
expect=_objects().Message)
139176

140177
@_require_api
141-
def send_video(self, path, duration=None, caption=None, reply_to=None,
142-
extra=None, attach=None, notify=True):
178+
def send_video(self, path=None, file_id=None, url=None,
179+
duration=None, caption=None, reply_to=None, extra=None,
180+
attach=None, notify=True):
143181
"""Send a video"""
144182
args = self._get_call_args(reply_to, extra, attach, notify)
145183
if duration is not None:
146184
args["duration"] = duration
147185
if caption is not None:
148186
args["caption"] = caption
149187

150-
files = {"video": open(path, "rb")}
188+
if path is not None and file_id is None and url is None:
189+
files = {"video": open(path, "rb")}
190+
elif file_id is not None and path is None and url is None:
191+
files = None
192+
args["video"] = file_id
193+
elif url is not None and file_id is None and path is None:
194+
args["video"] = url
195+
files = None
196+
elif path is None and file_id is None and url is None:
197+
raise TypeError("path or file_id or URL is missing")
198+
else:
199+
raise TypeError("Only one among path, file_id and URL must be" +
200+
"passed")
151201

152202
return self._api.call("sendVideo", args, files,
153203
expect=_objects().Message)
154204

155205
@_require_api
156-
def send_file(self, path, reply_to=None, extra=None,
157-
attach=None, notify=True, caption=None):
206+
def send_file(self, path=None, file_id=None, url=None, reply_to=None,
207+
extra=None, attach=None, notify=True, caption=None):
158208
"""Send a generic file"""
159209
args = self._get_call_args(reply_to, extra, attach, notify)
160210
if caption is not None:
161211
args["caption"] = caption
162212

163-
files = {"document": open(path, "rb")}
213+
if path is not None and file_id is None and url is None:
214+
files = {"document": open(path, "rb")}
215+
elif file_id is not None and path is None and url is None:
216+
files = None
217+
args["document"] = file_id
218+
elif url is not None and file_id is None and path is None:
219+
args["document"] = url
220+
files = None
221+
elif path is None and file_id is None and url is None:
222+
raise TypeError("path or file_id or URL is missing")
223+
else:
224+
raise TypeError("Only one among path, file_id and URL must be" +
225+
"passed")
164226

165227
return self._api.call("sendDocument", args, files,
166228
expect=_objects().Message)

0 commit comments

Comments
 (0)