Skip to content

Commit 50639fb

Browse files
author
Pietro Albini
committed
Add Bot.chat and deprecate Bot.send*
The Bot API 2.1 update introduced a new API call to get all information about a chat from its ID, which can be used to recreate a Chat object. Because of that, all the send* methods on the Bot class were retundant, since you can do `bot.chat(123).send("Message")`. Because of that, all those methods are now deprecated, and they will be removed in botogram 1.0. Fixes: GH-65
1 parent 5be5d2e commit 50639fb

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

botogram/frozenbot.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ def init_shared_memory(self, func):
128128
# This class also contains methods to send messages to users
129129
# They're defined dynamically out of the class body, see below
130130

131+
# Get a chat from its ID
132+
133+
def chat(self, id):
134+
"""Get an instance of botogram.Chat based on its ID"""
135+
return self.api.call("getChat", {"chat_id": id}, expect=objects.Chat)
136+
131137
# Edit messages already sent
132138

133139
def _edit_create_fake_message_object(self, chat, message):
@@ -267,6 +273,8 @@ def hide_commands(self):
267273

268274
for _proxy in _proxied_sends:
269275
@utils.wraps(_proxy)
276+
@utils.deprecated("0.1", "1.0", "Use Bot.chat(id).%s() instead."
277+
% _proxy.__name__)
270278
def _wrapper(self, chat, *args, __proxy=_proxy, **kwargs):
271279
# String chats are channels
272280
if type(chat) == str:

docs/api/bot.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,24 @@ components.
359359

360360
.. versionadded:: 0.3
361361

362+
.. py:method:: chat(id)
363+
364+
Get the :py:class:`~botogram.Chat` object of the chat with the ID you
365+
provided to this method. You can use this to get information about a chat
366+
you know about, or to send messages to other chats:
367+
368+
.. code-block:: python
369+
370+
BROADCAST_TO = [123, 321, 132] # List of chat IDs
371+
372+
@bot.command("broadcast")
373+
def broadcast_command(bot, chat, message, args):
374+
"""Broadcast a message to multiple chats"""
375+
to_send = " ".join(args)
376+
377+
for chat_id in BROADCAST_TO:
378+
bot.chat(chat_id).send(to_send)
379+
362380
.. py:method:: send(chat, message[, preview=True, reply_to=None, syntax=None, extra=None, notify=True])
363381
364382
This method sends a message to a specific chat. The chat must be
@@ -396,6 +414,8 @@ components.
396414

397415
Now the method returns the sent message
398416

417+
.. deprecated:: 0.3 it will be removed in botogram 1.0
418+
399419
.. py:method:: send_photo(chat, path[, caption="", reply_to=None, extra=None, notify=True])
400420
401421
This method sends a photo to a specific chat. The chat must be identified
@@ -427,6 +447,8 @@ components.
427447

428448
Now the method returns the sent message
429449

450+
.. deprecated:: 0.3 it will be removed in botogram 1.0
451+
430452
.. py:method:: send_audio(chat, path, [duration=None, performer=None, title=None, reply_to=None, extra=None, notify=True])
431453
432454
This method sends an audio track to a specific chat. The chat must be
@@ -463,6 +485,8 @@ components.
463485

464486
Now the method returns the sent message
465487

488+
.. deprecated:: 0.3 it will be removed in botogram 1.0
489+
466490
.. py:method:: send_voice(chat, path, [duration=None, reply_to=None, extra=None, notify=True])
467491
468492
This method sends a voice message to a specific chat. The chat must be
@@ -497,6 +521,8 @@ components.
497521

498522
Now the method returns the sent message
499523

524+
.. deprecated:: 0.3 it will be removed in botogram 1.0
525+
500526
.. py:method:: send_video(chat, path, [duration=None, caption=None, reply_to=None, extra=None, notify=True])
501527
502528
This method sends a video to a specific chat. The chat must be identified
@@ -532,6 +558,8 @@ components.
532558

533559
Now the method returns the sent message
534560

561+
.. deprecated:: 0.3 it will be removed in botogram 1.0
562+
535563
.. py:method:: send_file(chat, path, [reply_to=None, extra=None, notify=True])
536564
537565
This method sends a generic file to a specific chat. The chat must be
@@ -564,6 +592,8 @@ components.
564592

565593
Now the method returns the sent message
566594

595+
.. deprecated:: 0.3 it will be removed in botogram 1.0
596+
567597
.. py:method:: send_location(chat, latitude, longitude, [reply_to=None, extra=None, notify=True])
568598
569599
This method sends a geographic location to a specific chat. The chat must
@@ -597,6 +627,8 @@ components.
597627

598628
Now the method returns the sent message
599629

630+
.. deprecated:: 0.3 it will be removed in botogram 1.0
631+
600632
.. py:method:: send_sticker(sticker, [reply_to=None, extra=None, notify=True])
601633
602634
This method sends a sticker to a specific chat chat (in webp format). The
@@ -629,6 +661,8 @@ components.
629661

630662
Now the method returns the sent message
631663

664+
.. deprecated:: 0.3 it will be removed in botogram 1.0
665+
632666
.. py:attribute:: hide_commands
633667
634668
This attribute is now deprecated, use the ``hidden`` argument of the

docs/changelog.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ New features
5757
* Added new attribute :py:attr:`botogram.Chat.members_count`
5858
* Added new method :py:meth:`botogram.Chat.status_of`
5959
* Added new method :py:meth:`botogram.Chat.leave`
60+
* Added new method :py:meth:`botogram.Bot.chat`
6061
* Every method which sends something to a chat now returns the sent
6162
:py:class:`~botogram.Message`
6263
* Multiple instances of the same bot are now properly handled (as errors)
@@ -95,6 +96,14 @@ Deprecated features will be removed in botogram 1.0!
9596
* ``Message.new_chat_participant`` is now deprecated
9697
* ``Message.left_chat_participant`` is now deprecated
9798
* ``Bot.hide_commands`` is now deprecated
99+
* ``Bot.send`` is now deprecated
100+
* ``Bot.send_photo`` is now deprecated
101+
* ``Bot.send_audio`` is now deprecated
102+
* ``Bot.send_voice`` is now deprecated
103+
* ``Bot.send_video`` is now deprecated
104+
* ``Bot.send_file`` is now deprecated
105+
* ``Bot.send_location`` is now deprecated
106+
* ``Bot.send_sticker`` is now deprecated
98107

99108
.. _issue 67: https://github.com/pietroalbini/botogram/issues/67
100109

0 commit comments

Comments
 (0)