Skip to content

Commit bfad2fa

Browse files
Eldinnietsnoam
authored andcommitted
support 3.4 API (python-telegram-bot#865)
1 parent 8a8b121 commit bfad2fa

13 files changed

+403
-35
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ make the development of bots easy and straightforward. These classes are contain
9696
Telegram API support
9797
====================
9898

99-
As of **23. July 2017**, all types and methods of the Telegram Bot API 3.2 are supported.
99+
All types and methods of the Telegram Bot API 3.4 are supported.
100100

101101
==========
102102
Installing

telegram/bot.py

Lines changed: 177 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ def send_location(self,
768768
reply_markup=None,
769769
timeout=None,
770770
location=None,
771+
live_period=None,
771772
**kwargs):
772773
"""Use this method to send point on the map.
773774
@@ -780,6 +781,8 @@ def send_location(self,
780781
latitude (:obj:`float`, optional): Latitude of location.
781782
longitude (:obj:`float`, optional): Longitude of location.
782783
location (:class:`telegram.Location`, optional): The location to send.
784+
live_period (:obj:`int`, optional): Period in seconds for which the location will be
785+
updated, should be between 60 and 86400.
783786
disable_notification (:obj:`bool`, optional): Sends the message silently. Users will
784787
receive a notification with no sound.
785788
reply_to_message_id (:obj:`int`, optional): If the message is a reply, ID of the
@@ -803,14 +806,126 @@ def send_location(self,
803806

804807
if not (all([latitude, longitude]) or location):
805808
raise ValueError("Either location or latitude and longitude must be passed as"
806-
"argument")
809+
"argument.")
810+
811+
if not ((latitude is not None or longitude is not None) ^ bool(location)):
812+
raise ValueError("Either location or latitude and longitude must be passed as"
813+
"argument. Not both.")
807814

808815
if isinstance(location, Location):
809816
latitude = location.latitude
810817
longitude = location.longitude
811818

812819
data = {'chat_id': chat_id, 'latitude': latitude, 'longitude': longitude}
813820

821+
if live_period:
822+
data['live_period'] = live_period
823+
824+
return url, data
825+
826+
@log
827+
@message
828+
def edit_message_live_location(self,
829+
chat_id=None,
830+
message_id=None,
831+
inline_message_id=None,
832+
latitude=None,
833+
longitude=None,
834+
location=None,
835+
reply_markup=None,
836+
**kwargs):
837+
"""Use this method to edit live location messages sent by the bot or via the bot
838+
(for inline bots). A location can be edited until its :attr:`live_period` expires or
839+
editing is explicitly disabled by a call to :attr:`stop_message_live_location`.
840+
841+
Note:
842+
You can either supply a :obj:`latitude` and :obj:`longitude` or a :obj:`location`.
843+
844+
Args:
845+
chat_id (:obj:`int` | :obj:`str`): Unique identifier for the target chat or username
846+
of the target channel (in the format @channelusername).
847+
message_id (:obj:`int`, optional): Required if inline_message_id is not specified.
848+
Identifier of the sent message.
849+
inline_message_id (:obj:`str`, optional): Required if chat_id and message_id are not
850+
specified. Identifier of the inline message.
851+
latitude (:obj:`float`, optional): Latitude of location.
852+
longitude (:obj:`float`, optional): Longitude of location.
853+
location (:class:`telegram.Location`, optional): The location to send.
854+
reply_markup (:class:`telegram.ReplyMarkup`, optional): Additional interface options. A
855+
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
856+
to remove reply keyboard or to force a reply from the user.
857+
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
858+
the read timeout from the server (instead of the one specified during creation of
859+
the connection pool).
860+
861+
Returns:
862+
:class:`telegram.Message`: On success the edited message.
863+
"""
864+
865+
url = '{0}/editMessageLiveLocation'.format(self.base_url)
866+
867+
if not (all([latitude, longitude]) or location):
868+
raise ValueError("Either location or latitude and longitude must be passed as"
869+
"argument.")
870+
if not ((latitude is not None or longitude is not None) ^ bool(location)):
871+
raise ValueError("Either location or latitude and longitude must be passed as"
872+
"argument. Not both.")
873+
874+
if isinstance(location, Location):
875+
latitude = location.latitude
876+
longitude = location.longitude
877+
878+
data = {'latitude': latitude, 'longitude': longitude}
879+
880+
if chat_id:
881+
data['chat_id'] = chat_id
882+
if message_id:
883+
data['message_id'] = message_id
884+
if inline_message_id:
885+
data['inline_message_id'] = inline_message_id
886+
887+
return url, data
888+
889+
@log
890+
@message
891+
def stop_message_live_location(self,
892+
chat_id=None,
893+
message_id=None,
894+
inline_message_id=None,
895+
reply_markup=None,
896+
**kwargs):
897+
"""Use this method to stop updating a live location message sent by the bot or via the bot
898+
(for inline bots) before live_period expires.
899+
900+
Args:
901+
chat_id (:obj:`int` | :obj:`str`): Unique identifier for the target chat or username
902+
of the target channel (in the format @channelusername).
903+
message_id (:obj:`int`, optional): Required if inline_message_id is not specified.
904+
Identifier of the sent message.
905+
inline_message_id (:obj:`str`, optional): Required if chat_id and message_id are not
906+
specified. Identifier of the inline message.
907+
reply_markup (:class:`telegram.ReplyMarkup`, optional): Additional interface options. A
908+
JSON-serialized object for an inline keyboard, custom reply keyboard, instructions
909+
to remove reply keyboard or to force a reply from the user.
910+
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
911+
the read timeout from the server (instead of the one specified during creation of
912+
the connection pool).
913+
914+
Returns:
915+
:class:`telegram.Message`: On success the edited message.
916+
"""
917+
918+
url = '{0}/stopMessageLiveLocation'.format(self.base_url)
919+
920+
data = {}
921+
922+
if chat_id:
923+
data['chat_id'] = chat_id
924+
if message_id:
925+
data['message_id'] = message_id
926+
if inline_message_id:
927+
data['inline_message_id'] = inline_message_id
928+
814929
return url, data
815930

816931
@log
@@ -1825,6 +1940,63 @@ def get_chat_member(self, chat_id, user_id, timeout=None, **kwargs):
18251940

18261941
return ChatMember.de_json(result, self)
18271942

1943+
@log
1944+
def set_chat_sticker_set(self, chat_id, sticker_set_name, timeout=None, **kwargs):
1945+
"""Use this method to set a new group sticker set for a supergroup.
1946+
The bot must be an administrator in the chat for this to work and must have the appropriate
1947+
admin rights. Use the field :attr:`telegram.Chat.can_set_sticker_set` optionally returned
1948+
in :attr:`get_chat` requests to check if the bot can use this method.
1949+
1950+
Args:
1951+
chat_id (:obj:`int` | :obj:`str`): Unique identifier for the target chat or username
1952+
of the target supergroup (in the format @supergroupusername).
1953+
sticker_set_name (:obj:`str`): Name of the sticker set to be set as the group
1954+
sticker set.
1955+
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
1956+
the read timeout from the server (instead of the one specified during creation of
1957+
the connection pool).
1958+
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
1959+
1960+
1961+
Returns:
1962+
:obj:`bool`: True on success.
1963+
"""
1964+
1965+
url = '{0}/setChatStickerSet'.format(self.base_url)
1966+
1967+
data = {'chat_id': chat_id, 'sticker_set_name': sticker_set_name}
1968+
1969+
result = self._request.post(url, data, timeout=timeout)
1970+
1971+
return result
1972+
1973+
@log
1974+
def delete_chat_sticker_set(self, chat_id, timeout=None, **kwargs):
1975+
"""Use this method to delete a group sticker set from a supergroup. The bot must be an
1976+
administrator in the chat for this to work and must have the appropriate admin rights.
1977+
Use the field :attr:`telegram.Chat.can_set_sticker_set` optionally returned in
1978+
:attr:`get_chat` requests to check if the bot can use this method.
1979+
1980+
Args:
1981+
chat_id (:obj:`int` | :obj:`str`): Unique identifier for the target chat or username
1982+
of the target supergroup (in the format @supergroupusername).
1983+
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
1984+
the read timeout from the server (instead of the one specified during creation of
1985+
the connection pool).
1986+
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
1987+
1988+
Returns:
1989+
:obj:`bool`: True on success.
1990+
"""
1991+
1992+
url = '{0}/deleteChatStickerSet'.format(self.base_url)
1993+
1994+
data = {'chat_id': chat_id}
1995+
1996+
result = self._request.post(url, data, timeout=timeout)
1997+
1998+
return result
1999+
18282000
def get_webhook_info(self, timeout=None, **kwargs):
18292001
"""Use this method to get current webhook status. Requires no parameters.
18302002
@@ -2794,6 +2966,8 @@ def __reduce__(self):
27942966
sendVoice = send_voice
27952967
sendVideoNote = send_video_note
27962968
sendLocation = send_location
2969+
editMessageLiveLocation = edit_message_live_location
2970+
stopMessageLiveLocation = stop_message_live_location
27972971
sendVenue = send_venue
27982972
sendContact = send_contact
27992973
sendGame = send_game
@@ -2814,6 +2988,8 @@ def __reduce__(self):
28142988
getChat = get_chat
28152989
getChatAdministrators = get_chat_administrators
28162990
getChatMember = get_chat_member
2991+
setChatStickerSet = set_chat_sticker_set
2992+
deleteChatStickerSet = delete_chat_sticker_set
28172993
getChatMembersCount = get_chat_members_count
28182994
getWebhookInfo = get_webhook_info
28192995
setGameScore = set_game_score

telegram/chat.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class Chat(TelegramObject):
3838
invite_link (:obj:`str`): Optional. Chat invite link, for supergroups and channel chats.
3939
pinned_message (:class:`telegram.Message`): Optional. Pinned message, for supergroups.
4040
Returned only in get_chat.
41+
sticker_set_name (:obj:`str`): Optional. For supergroups, name of Group sticker set.
42+
can_set_sticker_set (:obj:`bool`): Optional. ``True``, if the bot can change group the
43+
sticker set.
4144
4245
Args:
4346
id (:obj:`int`): Unique identifier for this chat. This number may be greater than 32 bits
@@ -61,6 +64,10 @@ class Chat(TelegramObject):
6164
pinned_message (:class:`telegram.Message`, optional): Pinned message, for supergroups.
6265
Returned only in get_chat.
6366
bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods.
67+
sticker_set_name (:obj:`str`, optional): For supergroups, name of Group sticker set.
68+
Returned only in get_chat.
69+
can_set_sticker_set (:obj:`bool`, optional): ``True``, if the bot can change group the
70+
sticker set. Returned only in get_chat.
6471
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
6572
6673
"""
@@ -87,6 +94,8 @@ def __init__(self,
8794
description=None,
8895
invite_link=None,
8996
pinned_message=None,
97+
sticker_set_name=None,
98+
can_set_sticker_set=None,
9099
**kwargs):
91100
# Required
92101
self.id = int(id)
@@ -101,6 +110,8 @@ def __init__(self,
101110
self.description = description
102111
self.invite_link = invite_link
103112
self.pinned_message = pinned_message
113+
self.sticker_set_name = sticker_set_name
114+
self.can_set_sticker_set = can_set_sticker_set
104115

105116
self.bot = bot
106117
self._id_attrs = (self.id,)

telegram/inline/inlinequeryresultlocation.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class InlineQueryResultLocation(InlineQueryResult):
3333
latitude (:obj:`float`): Location latitude in degrees.
3434
longitude (:obj:`float`): Location longitude in degrees.
3535
title (:obj:`str`): Location title.
36+
live_period (:obj:`int`): Optional. Period in seconds for which the location can be
37+
updated, should be between 60 and 86400.
3638
reply_markup (:class:`telegram.InlineKeyboardMarkup`): Optional. Inline keyboard attached
3739
to the message.
3840
input_message_content (:class:`telegram.InputMessageContent`): Optional. Content of the
@@ -46,6 +48,8 @@ class InlineQueryResultLocation(InlineQueryResult):
4648
latitude (:obj:`float`): Location latitude in degrees.
4749
longitude (:obj:`float`): Location longitude in degrees.
4850
title (:obj:`str`): Location title.
51+
live_period (:obj:`int`, optional): Period in seconds for which the location can be
52+
updated, should be between 60 and 86400.
4953
reply_markup (:class:`telegram.InlineKeyboardMarkup`, optional): Inline keyboard attached
5054
to the message.
5155
input_message_content (:class:`telegram.InputMessageContent`, optional): Content of the
@@ -62,6 +66,7 @@ def __init__(self,
6266
latitude,
6367
longitude,
6468
title,
69+
live_period=None,
6570
reply_markup=None,
6671
input_message_content=None,
6772
thumb_url=None,
@@ -75,6 +80,8 @@ def __init__(self,
7580
self.title = title
7681

7782
# Optionals
83+
if live_period:
84+
self.live_period = live_period
7885
if reply_markup:
7986
self.reply_markup = reply_markup
8087
if input_message_content:

telegram/inline/inputlocationmessagecontent.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ class InputLocationMessageContent(InputMessageContent):
3232
Args:
3333
latitude (:obj:`float`): Latitude of the location in degrees.
3434
longitude (:obj:`float`): Longitude of the location in degrees.
35+
live_period (:obj:`int`, optional): Period in seconds for which the location can be
36+
updated, should be between 60 and 86400.
3537
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
3638
3739
"""
3840

39-
def __init__(self, latitude, longitude, **kwargs):
41+
def __init__(self, latitude, longitude, live_period=None, **kwargs):
4042
# Required
4143
self.latitude = latitude
4244
self.longitude = longitude
45+
self.live_period = live_period

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy