Skip to content

Commit 733be21

Browse files
author
Pietro Albini
committed
Simplify the code of the send_* methods
1 parent 404ede2 commit 733be21

File tree

1 file changed

+17
-60
lines changed

1 file changed

+17
-60
lines changed

botogram/objects/mixins.py

Lines changed: 17 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,31 @@ def __(self, *args, **kwargs):
2323
class ChatMixin:
2424
"""Add some methods for chats"""
2525

26-
@_require_api
27-
def send(self, message, preview=True, reply_to=None, syntax=None,
28-
extra=None):
29-
"""Send a message"""
26+
def _get_call_args(self, reply_to, extra):
27+
"""Get default API call arguments"""
3028
# Convert instance of Message to ids in reply_to
3129
if hasattr(reply_to, "message_id"):
3230
reply_to = reply_to.message_id
3331

3432
# Get the correct chat_id
3533
chat_id = self.username if self.type == "channel" else self.id
3634

37-
# Build API call arguments
38-
args = {"chat_id": chat_id, "text": message,
39-
"disable_web_page_preview": not preview}
35+
args = {"chat_id": chat_id}
4036
if reply_to is not None:
4137
args["reply_to_message_id"] = reply_to
4238
if extra is not None:
4339
args["reply_markup"] = extra.serialize()
4440

41+
return args
42+
43+
@_require_api
44+
def send(self, message, preview=True, reply_to=None, syntax=None,
45+
extra=None):
46+
"""Send a message"""
47+
args = self._get_call_args(reply_to, extra)
48+
args["text"] = message
49+
args["disable_web_page_preview"] = not preview
50+
4551
syntax = syntaxes.guess_syntax(message, syntax)
4652
if syntax is not None:
4753
args["parse_mode"] = syntax
@@ -51,94 +57,45 @@ def send(self, message, preview=True, reply_to=None, syntax=None,
5157
@_require_api
5258
def send_photo(self, path, caption=None, reply_to=None, extra=None):
5359
"""Send a photo"""
54-
# Convert instance of Message to ids in reply_to
55-
if hasattr(reply_to, "message_id"):
56-
reply_to = reply_to.message_id
57-
58-
# Get the correct chat_id
59-
chat_id = self.username if self.type == "channel" else self.id
60-
61-
# Build API call arguments
62-
args = {"chat_id": chat_id}
60+
args = self._get_call_args(reply_to, extra)
6361
if caption is not None:
6462
args["caption"] = caption
65-
if reply_to is not None:
66-
args["reply_to_message_id"] = reply_to
67-
if extra is not None:
68-
args["reply_markup"] = extra.serialize()
6963

7064
files = {"photo": open(path, "rb")}
71-
7265
self._api.call("sendPhoto", args, files)
7366

7467
@_require_api
7568
def send_audio(self, path, duration=None, performer=None, title=None,
7669
reply_to=None, extra=None):
7770
"""Send an audio track"""
78-
# Convert instance of Message to ids in reply_to
79-
if hasattr(reply_to, "message_id"):
80-
reply_to = reply_to.message_id
81-
82-
# Get the correct chat_id
83-
chat_id = self.username if self.type == "channel" else self.id
84-
85-
args = {"chat_id": chat_id}
71+
args = self._get_call_args(reply_to, extra)
8672
if duration is not None:
8773
args["duration"] = duration
8874
if performer is not None:
8975
args["performer"] = performer
9076
if title is not None:
9177
args["title"] = title
92-
if reply_to is not None:
93-
args["reply_to_message_id"] = reply_to
94-
if extra is not None:
95-
args["reply_markup"] = extra.serialize()
9678

9779
files = {"audio": open(path, "rb")}
98-
9980
self._api.call("sendAudio", args, files)
10081

10182
@_require_api
10283
def send_voice(self, path, duration=None, title=None, reply_to=None,
10384
extra=None):
10485
"""Send a voice message"""
105-
# Convert instance of Message to ids in reply_to
106-
if hasattr(reply_to, "message_id"):
107-
reply_to = reply_to.message_id
108-
109-
# Get the corret chat_id
110-
chat_id = self.username if self.type == "channel" else self.id
111-
112-
args = {"chat_id": chat_id}
86+
args = self._get_call_args(reply_to, extra)
11387
if duration is not None:
11488
args["duration"] = duration
115-
if reply_to is not None:
116-
args["reply_to_message_id"] = reply_to
117-
if extra is not None:
118-
args["reply_markup"] = extra.serialize()
11989

12090
files = {"voice": open(path, "rb")}
121-
12291
self._api.call("sendVoice", args, files)
12392

12493
@_require_api
12594
def send_file(self, path, reply_to=None, extra=None):
12695
"""Send a generic file"""
127-
# Convert instances of Message to ids in reply_to
128-
if hasattr(reply_to, "message_id"):
129-
reply_to = reply_to.message_id
130-
131-
# Get the correct chat_id
132-
chat_id = self.username if self.type == "channel" else self.id
133-
134-
args = {"chat_id": chat_id}
135-
if reply_to is not None:
136-
args["reply_to_message_id"] = reply_to
137-
if extra is not None:
138-
args["reply_markup"] = extra.serialize()
96+
args = self._get_call_args(reply_to, extra)
13997

14098
files = {"document": open(path, "rb")}
141-
14299
self._api.call("sendDocument", args, files)
143100

144101

0 commit comments

Comments
 (0)