Skip to content

Commit cffcfb2

Browse files
Francesco ZimboloMarcoBuster
authored andcommitted
Added support for live locations (#130)
1 parent 0a5ad9c commit cffcfb2

File tree

3 files changed

+115
-7
lines changed

3 files changed

+115
-7
lines changed

botogram/objects/mixins.py

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,20 @@ def send_file(self, path=None, file_id=None, url=None, reply_to=None,
274274
expect=_objects().Message)
275275

276276
@_require_api
277-
def send_location(self, latitude, longitude, reply_to=None, extra=None,
278-
attach=None, notify=True):
279-
"""Send a geographic location"""
277+
def send_location(self, latitude, longitude, live_period=None,
278+
reply_to=None, extra=None, attach=None, notify=True):
279+
"""Send a geographic location, set live_period to a number between 60
280+
and 86400 if it's a live location"""
280281
args = self._get_call_args(reply_to, extra, attach, notify)
281282
args["latitude"] = latitude
282283
args["longitude"] = longitude
283284

285+
if live_period:
286+
if live_period < 60 or live_period > 86400:
287+
raise ValueError(
288+
"live_period must be a number between 60 and 86400")
289+
args["live_period"] = live_period
290+
284291
return self._api.call("sendLocation", args,
285292
expect=_objects().Message)
286293

@@ -445,6 +452,47 @@ def edit_attach(self, attach):
445452

446453
self._api.call("editMessageReplyMarkup", args)
447454

455+
@_require_api
456+
def edit_live_location(self, latitude, longitude, extra=None, attach=None):
457+
"""Edit this message's live location position"""
458+
args = {"message_id": self.id, "chat_id": self.chat.id}
459+
args["latitude"] = latitude
460+
args["longitude"] = longitude
461+
462+
if extra is not None:
463+
_deprecated_message(
464+
"The extra parameter", "1.0", "use the attach parameter", -3
465+
)
466+
args["reply_markup"] = json.dumps(extra.serialize())
467+
468+
if attach is not None:
469+
if not hasattr(attach, "_serialize_attachment"):
470+
raise ValueError("%s is not an attachment" % attach)
471+
args["reply_markup"] = json.dumps(attach._serialize_attachment(
472+
self.chat
473+
))
474+
475+
self._api.call("editMessageLiveLocation", args)
476+
477+
@_require_api
478+
def stop_live_location(self, extra=None, attach=None):
479+
"""Stop this message's live location"""
480+
args = {"message_id": self.id, "chat_id": self.chat.id}
481+
482+
if extra is not None:
483+
_deprecated_message(
484+
"The extra parameter", "1.0", "use the attach parameter", -3
485+
)
486+
args["reply_markup"] = json.dumps(extra.serialize())
487+
488+
if attach is not None:
489+
if not hasattr(attach, "_serialize_attachment"):
490+
raise ValueError("%s is not an attachment" % attach)
491+
args["reply_markup"] = json.dumps(attach._serialize_attachment(
492+
self.chat
493+
))
494+
self._api.call("stopMessageLiveLocation", args)
495+
448496
@_require_api
449497
def reply(self, *args, **kwargs):
450498
"""Reply to the current message"""

docs/api/telegram.rst

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ about its business.
406406

407407
Added support for syntax
408408

409-
.. py:method:: send_location(latitude, longitude, [reply_to=None, attach=None, extra=None, notify=True])
409+
.. py:method:: send_location(latitude, longitude, [live_period=None, reply_to=None, attach=None, extra=None, notify=True])
410410
411411
Send the geographic location to the user. If the location you're sending
412412
is in reply to another message, set *reply_to* to the ID of the other
@@ -418,8 +418,12 @@ about its business.
418418
The *notify* parameter is for defining if your message should trigger
419419
a notification on the client side (yes by default).
420420

421+
The *live_period* parameter is for defining if this location must be a live location and needs to be updated over time.
422+
Leave to `None` if it is not or set it as a number between 60 and 86400 (seconds) if it is.
423+
421424
:param float latitude: The latitude of the location
422425
:param float longitude: The longitude of the location
426+
:param int live_period: The duration of the live location in seconds, None if it is not a live location.
423427
:param int reply_to: The ID of the :py:class:`~botogram.Message` this one is replying to
424428
:param object attach: An extra thing to attach to the message.
425429
:param object extra: An extra reply interface object to attach
@@ -435,6 +439,10 @@ about its business.
435439

436440
Now the method returns the sent message
437441

442+
.. versionchanged:: 0.7
443+
444+
Now the method supports live locations
445+
438446
.. py:method:: send_venue(latitude, longitude, title, address, [foursquare=None, reply_to=None, attach=None, extra=None, notify=True])
439447
440448
Send a venue to the user. A venue is made of its geographic coordinates
@@ -1237,9 +1245,9 @@ about its business.
12371245

12381246
Support text formatting in caption through *syntax*.
12391247

1240-
.. py:method:: send_location(latitude, longitude, [reply_to=None, attach=None, extra=None, notify=True])
1248+
.. py:method:: send_location(latitude, longitude, [live_period=None, reply_to=None, attach=None, extra=None, notify=True])
12411249
1242-
Send the geographic location to the chat. If the location you're sending
1250+
Send the geographic location to the user. If the location you're sending
12431251
is in reply to another message, set *reply_to* to the ID of the other
12441252
:py:class:`~botogram.Message`.
12451253

@@ -1249,8 +1257,12 @@ about its business.
12491257
The *notify* parameter is for defining if your message should trigger
12501258
a notification on the client side (yes by default).
12511259

1260+
The *live_period* parameter is for defining if this location must be a live location and needs to be updated over time.
1261+
Leave to `None` if it is not or set it as a number between 60 and 86400 (seconds) if it is.
1262+
12521263
:param float latitude: The latitude of the location
12531264
:param float longitude: The longitude of the location
1265+
:param int live_period: The duration of the live location in seconds, None if it is not a live location.
12541266
:param int reply_to: The ID of the :py:class:`~botogram.Message` this one is replying to
12551267
:param object attach: An extra thing to attach to the message.
12561268
:param object extra: An extra reply interface object to attach
@@ -1266,6 +1278,10 @@ about its business.
12661278

12671279
Now the method returns the sent message
12681280

1281+
.. versionchanged:: 0.7
1282+
1283+
Now the method supports live locations
1284+
12691285
.. py:method:: send_venue(latitude, longitude, title, address, [foursquare=None, reply_to=None, attach=None, extra=None, notify=True])
12701286
12711287
Send a venue to the chat. A venue is made of its geographic coordinates
@@ -1895,6 +1911,34 @@ about its business.
18951911

18961912
.. versionadded:: 0.4
18971913

1914+
.. py:method:: edit_live_location(latitude, longitude, [extra=None, attach=None])
1915+
1916+
This method allows you to edit the latitude and longitude of a live location you already sent.
1917+
1918+
:param float latitude: The new latitude
1919+
:param float longitude: The new longitude
1920+
:param object attach: An extra thing to attach to the message.
1921+
:param object extra: An extra reply interface object to attach.
1922+
1923+
.. deprecated:: 0.4
1924+
1925+
The *extra* parameter is now deprecated
1926+
1927+
.. versionadded:: 0.7
1928+
1929+
.. py:method:: stop_live_location([extra=None, attach=None])
1930+
1931+
This method allows you to stop a live location and prevent further latitude and longitude edits.
1932+
1933+
:param object attach: An extra thing to attach to the message.
1934+
:param object extra: An extra reply interface object to attach.
1935+
1936+
.. deprecated:: 0.4
1937+
1938+
The *extra* parameter is now deprecated.
1939+
1940+
.. versionadded:: 0.7
1941+
18981942
.. py:method:: forward_to(to[, notify=True])
18991943
19001944
Forward this message *to* another chat or user by specifying their ID. One
@@ -2117,7 +2161,7 @@ about its business.
21172161

21182162
Now the method returns the sent message
21192163

2120-
.. py:method:: reply_with_location(latitude, longitude, [attach=None, extra=None, notify=True])
2164+
.. py:method:: reply_with_location(latitude, longitude, [live_period=None, attach=None, extra=None, notify=True])
21212165
21222166
Send the geographic location to the user.
21232167

@@ -2127,8 +2171,13 @@ about its business.
21272171
The *notify* parameter is for defining if your message should trigger
21282172
a notification on the client side (yes by default).
21292173

2174+
The *live_period* parameter is for defining if this location must be a live location and needs to be updated over time.
2175+
Leave to `None` if it is not or set it as a number between 60 and 86400 (seconds) if it is.
2176+
21302177
:param float latitude: The latitude of the location
21312178
:param float longitude: The longitude of the location
2179+
:param int live_period: The duration of the live location in seconds, None if it is not a live location.
2180+
:param int reply_to: The ID of the :py:class:`~botogram.Message` this one is replying to
21322181
:param object attach: An extra thing to attach to the message.
21332182
:param object extra: An extra reply interface object to attach
21342183
:param bool notify: If you want to trigger the client notification.
@@ -2143,6 +2192,10 @@ about its business.
21432192

21442193
Now the method returns the sent message
21452194

2195+
.. versionchanged:: 0.7
2196+
2197+
Now the method supports live locations
2198+
21462199
.. py:method:: reply_with_venue(latitude, longitude, title, address, [foursquare=None, attach=None, extra=None, notify=True])
21472200
21482201
Reply to this message with a venue. A venue is made of its geographic

docs/changelog/0.7.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ New features
2525
* New attribute :py:attr:`~Message.forward_hidden`
2626
* New attribute :py:attr:`~Message.forward_signature`
2727

28+
* Added support for live locations
29+
30+
* New parameter `live_period` for :py:meth:`~Chat.send_location` and :py:meth:`~User.send_location`
31+
* New parameter `live_period` for :py:meth:`~Message.reply_with_location`
32+
* New method :py:meth:`~Message.edit_live_location`
33+
* New method :py:meth:`~Message.stop_live_location`
34+
2835

2936
Bug fixes
3037
---------

0 commit comments

Comments
 (0)