Skip to content

Commit 9e142ea

Browse files
author
Pietro Albini
committed
Refactor Bot.send* methods and fix #35
Because those methods were identical, this commit generates them dynamically right after the class definition, and appends them to the class. This also fixes an exeception when you send a message to a channel with the Bot.send* methods (issue #35).
1 parent 43cb2b1 commit 9e142ea

File tree

1 file changed

+31
-40
lines changed

1 file changed

+31
-40
lines changed

botogram/frozenbot.py

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -122,46 +122,8 @@ def init_shared_memory(self, func):
122122
"""This decorator is deprecated, and it calls @prepare_memory"""
123123
return self.prepare_memory(func)
124124

125-
# Those are shortcuts to send messages directly to someone
126-
127-
def send(self, chat, message, preview=True, reply_to=None, syntax=None,
128-
extra=None):
129-
"""Send a message in a chat"""
130-
obj = objects.Chat({"id": chat, "type": ""}, self.api)
131-
obj.send(message, preview, reply_to, syntax, extra)
132-
133-
def send_photo(self, chat, path, caption="", reply_to=None, extra=None):
134-
"""Send a photo in a chat"""
135-
obj = objects.Chat({"id": chat, "type": ""}, self.api)
136-
obj.send_photo(path, caption, reply_to, extra)
137-
138-
def send_audio(self, chat, path, duration=None, performer=None, title=None,
139-
reply_to=None, extra=None):
140-
"""Send an audio in a chat"""
141-
obj = objects.Chat({"id": chat, "type": ""}, self.api)
142-
obj.send_audio(path, duration, performer, title, reply_to, extra)
143-
144-
def send_voice(self, chat, path, duration=None, reply_to=None, extra=None):
145-
"""Send a voice message in a chat"""
146-
obj = objects.Chat({"id": chat, "type": ""}, self.api)
147-
obj.send_voice(path, duration, reply_to, extra)
148-
149-
def send_video(self, chat, path, duration=None, caption=None,
150-
reply_to=None, extra=None):
151-
"""Send a video in a chat"""
152-
obj = objects.Chat({"id": chat, "type": ""}, self.api)
153-
obj.send_video(path, duration, caption, reply_to, extra)
154-
155-
def send_file(self, chat, path, reply_to=None, extra=None):
156-
"""Send a generic file in a chat"""
157-
obj = objects.Chat({"id": chat, "type": ""}, self.api)
158-
obj.send_file(path, reply_to, extra)
159-
160-
def send_location(self, chat, latitude, longitude, reply_to=None,
161-
extra=None):
162-
"""Send a generic file in a chat"""
163-
obj = objects.Chat({"id": chat, "type": ""}, self.api)
164-
obj.send_location(latitude, longitude, reply_to, extra)
125+
# This class also contains methods to send messages to users
126+
# They're defined dynamically out of the class body, see below
165127

166128
# Let's process the messages
167129

@@ -241,6 +203,35 @@ def _call(self, func, component=None, **available):
241203
return func(**kwargs)
242204

243205

206+
# Those are shortcuts to send messages directly to someone
207+
# Create dynamic methods for each of the send methods. They're *really*
208+
# repetitive, so generating them with a for loop is not such a bad idea
209+
210+
_proxied_sends = [
211+
objects.Chat.send,
212+
objects.Chat.send_photo,
213+
objects.Chat.send_audio,
214+
objects.Chat.send_voice,
215+
objects.Chat.send_video,
216+
objects.Chat.send_file,
217+
objects.Chat.send_location,
218+
]
219+
220+
for _proxy in _proxied_sends:
221+
@utils.wraps(_proxy)
222+
def _wrapper(self, chat, *args, __proxy=_proxy, **kwargs):
223+
# String chats are channels
224+
if type(chat) == str:
225+
obj = objects.Chat({"id": 0, "type": "channel", "username": chat},
226+
self.api)
227+
else:
228+
obj = objects.Chat({"id": chat, "type": ""}, self.api)
229+
230+
__proxy(obj, *args, **kwargs)
231+
232+
setattr(FrozenBot, _proxy.__name__, _wrapper)
233+
234+
244235
def restore(*args):
245236
"""Restore a FrozenBot instance from pickle"""
246237
return FrozenBot(*args)

0 commit comments

Comments
 (0)