Skip to content

Commit 9d02c25

Browse files
authored
Merge pull request #106 from matteb99/release/0.5
add multiple fuction
2 parents 8138079 + 34a1d59 commit 9d02c25

File tree

8 files changed

+296
-15
lines changed

8 files changed

+296
-15
lines changed

botogram/frozenbot.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def _edit_create_fake_message_object(self, chat, message):
161161
"""Helper method for edit_message and edit_caption"""
162162
# Also accept objects
163163
if hasattr(message, "message_id"):
164-
message = message.message_id
164+
message = message.id
165165
if hasattr(chat, "id"):
166166
chat = chat.id
167167

@@ -179,15 +179,15 @@ def _edit_create_fake_message_object(self, chat, message):
179179
}, self.api)
180180

181181
def edit_message(self, chat, message, text, syntax=None, preview=True,
182-
extra=None):
182+
extra=None, attach=None):
183183
"""Edit a message already sent to the user"""
184184
msg = self._edit_create_fake_message_object(chat, message)
185-
msg.edit(text, syntax, preview, extra)
185+
msg.edit(text, syntax, preview, extra, attach)
186186

187-
def edit_caption(self, chat, message, caption, extra=None):
187+
def edit_caption(self, chat, message, caption, extra=None, attach=None):
188188
"""Edit the caption of a media already sent to the user"""
189189
msg = self._edit_create_fake_message_object(chat, message)
190-
msg.edit_caption(caption, extra)
190+
msg.edit_caption(caption, extra, attach)
191191

192192
# Let's process the messages
193193

botogram/objects/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
# flake8: noqa
2222

23-
from .chats import User, Chat, UserProfilePhotos
23+
from .chats import User, Chat, UserProfilePhotos, Permissions
2424
from .media import PhotoSize, Photo, Audio, Voice, Document, Sticker, \
2525
Video, Contact, Location, Venue
2626
from .messages import Message

botogram/objects/chats.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
from .. import api
2222
from .base import BaseObject, multiple
2323
from . import mixins
24+
from datetime import datetime as dt
25+
from time import mktime
26+
2427

2528
from .media import Photo
2629

@@ -38,6 +41,7 @@ class User(BaseObject, mixins.ChatMixin):
3841
optional = {
3942
"last_name": str,
4043
"username": str,
44+
"is_bot": bool
4145
}
4246
_check_equality_ = "id"
4347

@@ -102,6 +106,13 @@ class Chat(BaseObject, mixins.ChatMixin):
102106
"username": str,
103107
"first_name": str,
104108
"last_name": str,
109+
"all_members_are_administrators": bool,
110+
"description": str,
111+
"invite_link": str,
112+
# This is added at the bottom of messages.py due to circular imports
113+
# "pinned_message" = Message
114+
"sticker_set_name": str,
115+
"can_set_sticker_set": bool
105116
}
106117
_check_equality_ = "id"
107118

@@ -260,6 +271,115 @@ def unban(self, user):
260271
"user_id": user,
261272
})
262273

274+
def kick(self, user, time=None):
275+
if self.type == "private":
276+
raise RuntimeError("This chat is a private!")
277+
if isinstance(user, User):
278+
user = user.id
279+
280+
if time is None:
281+
self._api.call("unbanChatMember", {
282+
"chat_id": self.id,
283+
"user_id": user
284+
})
285+
else:
286+
if isinstance(time, dt):
287+
time = mktime(time.timetuple())
288+
self._api.call("kickChatMember", {
289+
"chat_id": self.id,
290+
"user_id": user,
291+
"until_date": time
292+
})
293+
294+
def permissions(self, user):
295+
return Permissions(user, self)
296+
297+
298+
class Permissions:
299+
def __init__(self, user, chat):
300+
if chat.type not in ("group", "supergroup"):
301+
raise RuntimeError("This chat is not a group or a supergroup!")
302+
# Accept also an instance of `User`
303+
self._chatid = chat.id
304+
self._api = chat._api
305+
if isinstance(user, User):
306+
self._user = user.id
307+
else:
308+
self._user = user
309+
infouser = self._api.call("getChatMember", {
310+
"chat_id": self._chatid,
311+
"user_id": self._user},
312+
expect=ChatMember)
313+
314+
try:
315+
self._until_date = infouser.until_date
316+
except AttributeError:
317+
self._until_date = 0
318+
self.until_date = self._until_date
319+
320+
try:
321+
self._send_messages = infouser.can_send_messages
322+
except AttributeError:
323+
self._send_messages = True
324+
self.send_messages = self._send_messages
325+
326+
try:
327+
self._send_media_messages = infouser.can_media_messages
328+
except AttributeError:
329+
self._send_media_messages = True
330+
self.send_media_messages = self._send_media_messages
331+
332+
try:
333+
self._send_other_messages = infouser.can_send_other_messages
334+
except AttributeError:
335+
self._send_other_messages = True
336+
self.send_other_messages = self._send_other_messages
337+
338+
try:
339+
self._add_web_page_previews = infouser.can_add_web_page_previews
340+
except AttributeError:
341+
self._add_web_page_previews = True
342+
self.add_web_page_previews = self._add_web_page_previews
343+
344+
def __enter__(self):
345+
return self
346+
347+
def __exit__(self, exc_type, exc_val, exc_tb):
348+
if exc_type is None:
349+
self.save()
350+
351+
def save(self):
352+
arguments = {
353+
"chat_id": self.chatid,
354+
"user_id": self.user
355+
}
356+
modify = False
357+
358+
if isinstance(self.until_date, dt):
359+
self.until_date = mktime(self.until_date.timetuple())
360+
modify = True
361+
if self._until_date != self.until_date:
362+
arguments.update({"until_date": self.until_date})
363+
modify = True
364+
if self._send_messages != self.send_messages:
365+
arguments.update({"can_send_messages": self.send_messages})
366+
modify = True
367+
if self._send_media_messages != self.send_media_messages:
368+
arguments.update({"can_send_media_messages":
369+
self.send_media_messages})
370+
modify = True
371+
if self._send_other_messages != self.send_other_messages:
372+
arguments.update({"can_send_other_messages":
373+
self.send_other_messages})
374+
modify = True
375+
if self._add_web_page_previews != self.add_web_page_previews:
376+
arguments.update({"can_add_web_page_previews":
377+
self.add_web_page_previews})
378+
modify = True
379+
380+
if modify:
381+
self._api.call("restrictChatMember", arguments)
382+
263383

264384
class ChatMember(BaseObject):
265385
"""Telegram API representation of a chat member
@@ -271,6 +391,22 @@ class ChatMember(BaseObject):
271391
"user": User,
272392
"status": str,
273393
}
394+
optional = {
395+
"until_date": int,
396+
"can_be_edited": bool,
397+
"can_change_info": bool,
398+
"can_post_messages": bool,
399+
"can_edit_messages": bool,
400+
"can_delete_messages": bool,
401+
"can_invite_users": bool,
402+
"can_restrict_members": bool,
403+
"can_pin_messages": bool,
404+
"can_promote_members": bool,
405+
"can_send_messages": bool,
406+
"can_send_media_messages": bool,
407+
"can_send_other_messages": bool,
408+
"can_add_web_page_previews": bool
409+
}
274410
_check_equality_ = "user"
275411

276412

@@ -284,3 +420,9 @@ class UserProfilePhotos(BaseObject):
284420
"total_count": int,
285421
"photos": multiple(Photo),
286422
}
423+
424+
425+
# add this code on the button to avoid import loop
426+
# flake8: noqa
427+
from .messages import Message
428+
Chat.optional["pinned_message"] = Message

botogram/objects/messages.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from .base import BaseObject, _itself
2424
from . import mixins
2525
from .. import utils
26-
2726
from .chats import User, Chat
2827
from .media import Audio, Voice, Document, Photo, Sticker, Video, Contact, \
2928
Location, Venue
@@ -93,7 +92,7 @@ def __eq__(self, other):
9392
if self._message is None or other._message is None:
9493
return id(self) == id(other)
9594

96-
return self._message.message_id == other._message.message_id and \
95+
return self._message.id == other._message.id and \
9796
self._offset == other._offset and self._length == other._length
9897

9998
def __str__(self):
@@ -364,6 +363,7 @@ def from_(self):
364363
replace_keys = {
365364
"from": "sender",
366365
"entities": "parsed_text",
366+
"message_id": "id",
367367

368368
# Those are provided dynamically by self.forward_from
369369
"forward_from": "_forward_from",
@@ -413,6 +413,12 @@ def new_chat_participant(self):
413413

414414
@property
415415
@utils.deprecated("Message.left_chat_participant", "1.0",
416-
"Rename property ot Message.left_chat_member")
416+
"Rename property to Message.left_chat_member")
417417
def left_chat_participant(self):
418418
return self.left_chat_member
419+
420+
@property
421+
@utils.deprecated("Message.message_id", "0.5",
422+
"Rename property to Message.id")
423+
def message_id(self):
424+
return self.id

botogram/objects/mixins.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def _get_call_args(self, reply_to, extra, attach, notify):
5656
"""Get default API call arguments"""
5757
# Convert instance of Message to ids in reply_to
5858
if hasattr(reply_to, "message_id"):
59-
reply_to = reply_to.message_id
59+
reply_to = reply_to.id
6060

6161
args = {"chat_id": self.id}
6262
if reply_to is not None:
@@ -351,7 +351,7 @@ def forward_to(self, to, notify=True):
351351
@_require_api
352352
def edit(self, text, syntax=None, preview=True, extra=None, attach=None):
353353
"""Edit this message"""
354-
args = {"message_id": self.message_id, "chat_id": self.chat.id}
354+
args = {"message_id": self.id, "chat_id": self.chat.id}
355355
args["text"] = text
356356

357357
syntax = syntaxes.guess_syntax(text, syntax)
@@ -463,7 +463,7 @@ def delete(self):
463463
"""Delete the message"""
464464
return self._api.call("deleteMessage", {
465465
"chat_id": self.chat.id,
466-
"message_id": self.message_id,
466+
"message_id": self.id,
467467
})
468468

469469

-84 Bytes
Binary file not shown.

docs/api/bot.rst

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ components.
438438

439439
:return: A frozen instance of the current bot.
440440

441-
.. py:method:: edit_message(chat, message, text, [syntax=None, preview=True, extra=None])
441+
.. py:method:: edit_message(chat, message, text, [syntax=None, preview=True, attach=None, extra=None])
442442
443443
With this method you can edit the text of a message the user already
444444
received. This allows you to do a lot of interesting things, like
@@ -456,11 +456,15 @@ components.
456456
:param str text: The new text of the message
457457
:param bool preview: Whether to show link previews.
458458
:param str syntax: The name of the syntax used for the message.
459+
:param object attach: An extra thing to attach to the message.
459460
:param object extra: An extra reply interface object to attach.
460461

461462
.. versionadded:: 0.3
463+
.. versionchanged:: 0.6
462464

463-
.. py:method:: edit_caption(caption, [extra=None])
465+
Added support for attach
466+
467+
.. py:method:: edit_caption(caption, [attach=None, extra=None])
464468
465469
With this method you can edit the caption of the media attached to a
466470
message the user already received. This allows you to do a lot of
@@ -470,10 +474,15 @@ components.
470474
Please remember you can only edit messages your bot sent to the user.
471475

472476
:param str caption: The new caption of the media file.
477+
:param object attach: An extra thing to attach to the message.
473478
:param object extra: An extra reply interface object to attach.
474479

475480
.. versionadded:: 0.3
476481

482+
.. versionchanged:: 0.6
483+
484+
Added support for attach
485+
477486
.. py:method:: chat(id)
478487
479488
Get the :py:class:`~botogram.Chat` object of the chat with the ID you

0 commit comments

Comments
 (0)