Skip to content

Commit bef03f2

Browse files
author
Pietro Albini
committed
Add support for sending contacts
The Bot API 2.0 update added support for sending contacts, which your bot could receive but not send. This commit implements all the required methods to expose this to the users. Fixes: GH-57
1 parent 0e00253 commit bef03f2

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

botogram/objects/mixins.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@ def send_sticker(self, sticker, reply_to=None, extra=None, notify=True):
164164
return self._api.call("sendSticker", args, files,
165165
expect=_objects().Message)
166166

167+
def send_contact(self, phone, first_name, last_name=None, *, reply_to=None,
168+
extra=None, notify=True):
169+
"""Send a contact"""
170+
args = self._get_call_args(reply_to, extra, notify)
171+
args["phone_number"] = phone
172+
args["first_name"] = first_name
173+
174+
if last_name is not None:
175+
args["last_name"] = last_name
176+
177+
return self._api.call("sendContact", args, expect=_objects().Message)
178+
167179

168180
class MessageMixin:
169181
"""Add some methods for messages"""
@@ -254,6 +266,11 @@ def reply_with_sticker(self, *args, **kwargs):
254266
"""Reply with a sticker to the current message"""
255267
return self.chat.send_sticker(*args, reply_to=self, **kwargs)
256268

269+
@_require_api
270+
def reply_with_contact(self, *args, **kwargs):
271+
"""Reply with a contact to the current message"""
272+
return self.chat.send_contact(*args, reply_to=self, **kwargs)
273+
257274

258275
class FileMixin:
259276
"""Add some methods for files"""

docs/api/telegram.rst

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,30 @@ about its business.
316316

317317
Now the method returns the sent message
318318

319+
.. py:method:: send_contact(phone, first_name, [last_name=None, reply_to=None, extra=None, notify=True])
320+
321+
Send a contact to the user. A Telegram contact is made of its phone
322+
number (with the international prefix), its first name and optionally its
323+
last name. You can use this, for example, to send the user the phone
324+
number of a buisness so he can call them:
325+
326+
.. code-block:: python
327+
328+
@bot.command("support")
329+
def support_command(chat, message, args):
330+
message.sender.send("Hi there, here is our support number:")
331+
message.sender.send_contact("+390124567890", "Support")
332+
333+
:param str phone: The phone number of the contact
334+
:param str first_name: The first name of the contact
335+
:param str last_name: The last name of the contact
336+
:param int reply_to: The ID of the :py:class:`~botogram.Message` this one is replying to
337+
:param object extra: An extra reply interface object to attach
338+
:param bool notify: If you want to trigger a notification on the client
339+
:returns: The message you sent
340+
:rtype: ~botogram.Message
341+
342+
.. versionadded:: 0.3
319343

320344
.. py:class:: botogram.Chat
321345
@@ -732,6 +756,31 @@ about its business.
732756

733757
Now the method returns the sent message
734758

759+
.. py:method:: send_contact(phone, first_name, [last_name=None, reply_to=None, extra=None, notify=True])
760+
761+
Send a contact to the chat. A Telegram contact is made of its phone
762+
number (with the international prefix), its first name and optionally its
763+
last name. You can use this, for example, to send the user the phone
764+
number of a buisness so he can call them:
765+
766+
.. code-block:: python
767+
768+
@bot.command("support")
769+
def support_command(chat, message, args):
770+
chat.send("Hi there, here is our support number:")
771+
chat.send_contact("+390124567890", "Support")
772+
773+
:param str phone: The phone number of the contact
774+
:param str first_name: The first name of the contact
775+
:param str last_name: The last name of the contact
776+
:param int reply_to: The ID of the :py:class:`~botogram.Message` this one is replying to
777+
:param object extra: An extra reply interface object to attach
778+
:param bool notify: If you want to trigger a notification on the client
779+
:returns: The message you sent
780+
:rtype: ~botogram.Message
781+
782+
.. versionadded:: 0.3
783+
735784
.. py:class:: botogram.ParsedText
736785
737786
This class contains the parsed representation of the text of a received
@@ -1327,6 +1376,30 @@ about its business.
13271376

13281377
Now the method returns the sent message
13291378

1379+
.. py:method:: reply_with_contact(phone, first_name, [last_name=None, extra=None, notify=True])
1380+
1381+
Reply to this message with a contact. A Telegram contact is made of its
1382+
phone number (with the international prefix), its first name and
1383+
optionally its last name. You can use this, for example, to send the user
1384+
the phone number of a buisness so he can call them:
1385+
1386+
.. code-block:: python
1387+
1388+
@bot.command("support")
1389+
def support_command(chat, message, args):
1390+
message.reply("Hi there, here is our support number:")
1391+
message.reply_with_contact("+390124567890", "Support")
1392+
1393+
:param str phone: The phone number of the contact
1394+
:param str first_name: The first name of the contact
1395+
:param str last_name: The last name of the contact
1396+
:param object extra: An extra reply interface object to attach
1397+
:param bool notify: If you want to trigger a notification on the client
1398+
:returns: The message you sent
1399+
:rtype: ~botogram.Message
1400+
1401+
.. versionadded:: 0.3
1402+
13301403

13311404
.. py:class:: botogram.Photo
13321405

docs/changelog/0.3.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ New features
4848
* New decorator :py:meth:`botogram.Bot.message_edited`
4949
* New method :py:meth:`botogram.Component.add_message_edited_hook`
5050

51+
* Added support for sending contacts:
52+
53+
* New method :py:meth:`botogram.User.send_contact`
54+
* New method :py:meth:`botogram.Chat.send_contact`
55+
* New method :py:meth:`botogram.Message.reply_with_contact`
56+
5157
* Added new attribute :py:attr:`botogram.Message.pinned_message`
5258
* Added new attribute :py:attr:`botogram.Sticker.emoji`
5359
* Added new attribute :py:attr:`botogram.Chat.admins`

0 commit comments

Comments
 (0)