Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added supprot for allow_sending in send method #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 65 additions & 37 deletions botogram/objects/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def __(self, *args, **kwargs):
class ChatMixin:
"""Add some methods for chats"""

def _get_call_args(self, reply_to, extra, attach, notify):
def _get_call_args(self, reply_to, extra, attach, notify,
allow_sending):
"""Get default API call arguments"""
# Convert instance of Message to ids in reply_to
if hasattr(reply_to, "id"):
Expand All @@ -62,6 +63,8 @@ def _get_call_args(self, reply_to, extra, attach, notify):
args = {"chat_id": self.id}
if reply_to is not None:
args["reply_to_message_id"] = reply_to
if allow_sending is not None:
args["allow_sending_without_reply"] = allow_sending
if extra is not None:
_deprecated_message(
"The extra parameter", "1.0", "use the attach parameter", -4
Expand Down Expand Up @@ -97,10 +100,12 @@ def _get_file_args(path, file_id, url):
return args, file

@_require_api
def send(self, message, preview=True, reply_to=None, syntax=None,
def send(self, message, preview=True, reply_to=None, allow_sending=None,
syntax=None,
extra=None, attach=None, notify=True):
"""Send a message"""
args = self._get_call_args(reply_to, extra, attach, notify)
args = self._get_call_args(reply_to, extra, attach, notify,
allow_sending)
args["text"] = message
args["disable_web_page_preview"] = not preview

Expand All @@ -112,10 +117,11 @@ def send(self, message, preview=True, reply_to=None, syntax=None,

@_require_api
def send_photo(self, path=None, file_id=None, url=None, caption=None,
syntax=None, reply_to=None, extra=None, attach=None,
notify=True):
syntax=None, reply_to=None, allow_sending=None,
extra=None, attach=None, notify=True):
"""Send a photo"""
args = self._get_call_args(reply_to, extra, attach, notify)
args = self._get_call_args(reply_to, extra, attach, notify,
allow_sending)
if caption is not None:
args["caption"] = caption
if syntax is not None:
Expand All @@ -134,10 +140,11 @@ def send_photo(self, path=None, file_id=None, url=None, caption=None,
@_require_api
def send_audio(self, path=None, file_id=None, url=None, duration=None,
thumb=None, performer=None, title=None, reply_to=None,
extra=None, attach=None, notify=True, caption=None, *,
syntax=None):
allow_sending=None, extra=None, attach=None, notify=True,
caption=None, *, syntax=None):
"""Send an audio track"""
args = self._get_call_args(reply_to, extra, attach, notify)
args = self._get_call_args(reply_to, extra, attach, notify,
allow_sending)
if caption is not None:
args["caption"] = caption
if syntax is not None:
Expand All @@ -164,10 +171,11 @@ def send_audio(self, path=None, file_id=None, url=None, duration=None,

@_require_api
def send_voice(self, path=None, file_id=None, url=None, duration=None,
title=None, reply_to=None, extra=None, attach=None,
notify=True, caption=None, *, syntax=None):
title=None, reply_to=None, allow_sending=None, extra=None,
attach=None, notify=True, caption=None, *, syntax=None):
"""Send a voice message"""
args = self._get_call_args(reply_to, extra, attach, notify)
args = self._get_call_args(reply_to, extra, attach, notify,
allow_sending)
if caption is not None:
args["caption"] = caption
if syntax is not None:
Expand All @@ -194,10 +202,11 @@ def send_voice(self, path=None, file_id=None, url=None, duration=None,
@_require_api
def send_video(self, path=None, file_id=None, url=None,
duration=None, caption=None, streaming=True, thumb=None,
reply_to=None, extra=None, attach=None,
reply_to=None, allow_sending=None, extra=None, attach=None,
notify=True, *, syntax=None):
"""Send a video"""
args = self._get_call_args(reply_to, extra, attach, notify)
args = self._get_call_args(reply_to, extra, attach, notify,
allow_sending)
args["supports_streaming"] = streaming
if duration is not None:
args["duration"] = duration
Expand All @@ -221,10 +230,12 @@ def send_video(self, path=None, file_id=None, url=None,

@_require_api
def send_video_note(self, path=None, file_id=None, duration=None,
diameter=None, thumb=None, reply_to=None, extra=None,
attach=None, notify=True):
diameter=None, thumb=None, reply_to=None,
allow_sending=None, extra=None, attach=None,
notify=True):
"""Send a video note"""
args = self._get_call_args(reply_to, extra, attach, notify)
args = self._get_call_args(reply_to, extra, attach, notify,
allow_sending)
if duration is not None:
args["duration"] = duration
if diameter is not None:
Expand All @@ -245,10 +256,11 @@ def send_video_note(self, path=None, file_id=None, duration=None,
@_require_api
def send_gif(self, path=None, file_id=None, url=None, duration=None,
width=None, height=None, caption=None, thumb=None,
reply_to=None, extra=None, attach=None,
reply_to=None, allow_sending=None, extra=None, attach=None,
notify=True, syntax=None):
"""Send an animation"""
args = self._get_call_args(reply_to, extra, attach, notify)
args = self._get_call_args(reply_to, extra, attach, notify,
allow_sending)
if duration is not None:
args["duration"] = duration
if caption is not None:
Expand All @@ -275,10 +287,11 @@ def send_gif(self, path=None, file_id=None, url=None, duration=None,

@_require_api
def send_file(self, path=None, file_id=None, url=None, thumb=None,
reply_to=None, extra=None, attach=None,
reply_to=None, allow_sending=None, extra=None, attach=None,
notify=True, caption=None, *, syntax=None):
"""Send a generic file"""
args = self._get_call_args(reply_to, extra, attach, notify)
args = self._get_call_args(reply_to, extra, attach, notify,
allow_sending)
if caption is not None:
args["caption"] = caption
if syntax is not None:
Expand All @@ -299,10 +312,12 @@ def send_file(self, path=None, file_id=None, url=None, thumb=None,

@_require_api
def send_location(self, latitude, longitude, live_period=None,
reply_to=None, extra=None, attach=None, notify=True):
reply_to=None, allow_sending=None, extra=None,
attach=None, notify=True):
"""Send a geographic location, set live_period to a number between 60
and 86400 if it's a live location"""
args = self._get_call_args(reply_to, extra, attach, notify)
args = self._get_call_args(reply_to, extra, attach, notify,
allow_sending)
args["latitude"] = latitude
args["longitude"] = longitude

Expand All @@ -317,9 +332,11 @@ def send_location(self, latitude, longitude, live_period=None,

@_require_api
def send_venue(self, latitude, longitude, title, address, foursquare=None,
reply_to=None, extra=None, attach=None, notify=True):
reply_to=None, allow_sending=None, extra=None,
attach=None, notify=True):
"""Send a venue"""
args = self._get_call_args(reply_to, extra, attach, notify)
args = self._get_call_args(reply_to, extra, attach, notify,
allow_sending)
args["latitude"] = latitude
args["longitude"] = longitude
args["title"] = title
Expand All @@ -330,8 +347,8 @@ def send_venue(self, latitude, longitude, title, address, foursquare=None,
self._api.call("sendVenue", args, expect=_objects().Message)

@_require_api
def send_sticker(self, sticker=None, reply_to=None, extra=None,
attach=None, notify=True, *,
def send_sticker(self, sticker=None, reply_to=None, allow_sending=None,
extra=None, attach=None, notify=True, *,
path=None, file_id=None, url=None):
"""Send a sticker"""
if sticker is not None:
Expand All @@ -343,7 +360,8 @@ def send_sticker(self, sticker=None, reply_to=None, extra=None,
"The sticker parameter", "1.0", "use the path parameter", -3
)

args = self._get_call_args(reply_to, extra, attach, notify)
args = self._get_call_args(reply_to, extra, attach, notify,
allow_sending)

files = dict()
args["sticker"], files["sticker"] = self._get_file_args(path,
Expand All @@ -357,10 +375,11 @@ def send_sticker(self, sticker=None, reply_to=None, extra=None,

@_require_api
def send_contact(self, phone, first_name, last_name=None,
vcard=None, *, reply_to=None,
vcard=None, *, reply_to=None, allow_sending=None,
extra=None, attach=None, notify=True):
"""Send a contact"""
args = self._get_call_args(reply_to, extra, attach, notify)
args = self._get_call_args(reply_to, extra, attach, notify,
allow_sending)
args["phone_number"] = phone
args["first_name"] = first_name

Expand All @@ -372,10 +391,12 @@ def send_contact(self, phone, first_name, last_name=None,
return self._api.call("sendContact", args, expect=_objects().Message)

@_require_api
def send_poll(self, question, *kargs, reply_to=None, extra=None,
def send_poll(self, question, *kargs, reply_to=None, allow_sending=None,
extra=None,
attach=None, notify=True):
"""Send a poll"""
args = self._get_call_args(reply_to, extra, attach, notify)
args = self._get_call_args(reply_to, extra, attach, notify,
allow_sending)
args["question"] = question
args["options"] = json.dumps(list(kargs))

Expand Down Expand Up @@ -406,9 +427,10 @@ def remove_photo(self):
self._api.call("deleteChatPhoto", args)

@_require_api
def send_album(self, album=None, reply_to=None, notify=True):
def send_album(self, album=None, reply_to=None, allow_sending=None,
notify=True):
"""Send a Album"""
albums = SendAlbum(self, reply_to, notify)
albums = SendAlbum(self, reply_to, allow_sending, notify)
if album is not None:
albums._content = album._content
albums._file = album._file
Expand Down Expand Up @@ -769,11 +791,13 @@ def add_video(self, path=None, file_id=None, url=None, duration=None,

class SendAlbum(Album):
"""Send the album instance to the chat passed as argument"""
def __init__(self, chat, reply_to=None, notify=True):
def __init__(self, chat, reply_to=None, allow_sending=None,
notify=True):
super(SendAlbum, self).__init__()
self._get_call_args = chat._get_call_args
self.chat = chat
self._api = chat._api
self.reply_to = reply_to
self.allow_sending = allow_sending
self.notify = notify
self._used = False

Expand All @@ -785,9 +809,13 @@ def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type is None:
self.send()

def _get_call_args(self, *args, **kwargs):
return self.chat._get_call_args(*args, **kwargs)

def send(self):
"""Send the Album to telgram"""
args = self._get_call_args(self.reply_to, None, None, self.notify)
args = self._get_call_args(self.reply_to, None, None, self.notify,
self.allow_sending)
args["media"] = json.dumps(self._content)
return self._api.call("sendMediaGroup", args, self._file,
expect=multiple(_objects().Message))
Expand Down
Loading