Skip to content

Commit 86c1f68

Browse files
committed
Adding sendLocation, its tests and minor fixes
1 parent 6bb8c4e commit 86c1f68

File tree

6 files changed

+78
-8
lines changed

6 files changed

+78
-8
lines changed

telegram/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from sticker import Sticker
1818
from video import Video
1919
# from contact import Contact
20-
# from location import Location
20+
from location import Location
2121
# from inputfile import InputFile
2222
# from userprofilephotos import UserProfilePhotos
2323
# from replykeyboardmarkup import ReplyKeyboardMarkup

telegram/bot.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,47 @@ def sendVideo(self,
313313

314314
return Message.newFromJsonDict(data)
315315

316-
def sendLocation(self):
316+
def sendLocation(self,
317+
chat_id,
318+
latitude,
319+
longitude,
320+
reply_to_message_id=None,
321+
reply_markup=None):
322+
"""Use this method to send point on the map.
323+
324+
Args:
325+
chat_id:
326+
Unique identifier for the message recipient — User or GroupChat id.
327+
latitude:
328+
Latitude of location.
329+
longitude:
330+
Longitude of location.
331+
reply_to_message_id:
332+
If the message is a reply, ID of the original message. [Optional]
333+
reply_markup:
334+
Additional interface options. A JSON-serialized object for a
335+
custom reply keyboard, instructions to hide keyboard or to force a
336+
reply from the user. [Optional]
337+
Returns:
338+
A telegram.Message instance representing the message posted.
339+
"""
340+
317341
url = '%s/sendLocation' % (self.base_url)
318342

343+
data = {'chat_id': chat_id,
344+
'latitude': latitude,
345+
'longitude': longitude}
346+
347+
if reply_to_message_id:
348+
data['reply_to_message_id'] = reply_to_message_id
349+
if reply_markup:
350+
data['reply_markup'] = reply_markup
351+
352+
json_data = self._requestUrl(url, 'POST', data=data)
353+
data = self._parseAndCheckTelegram(json_data.content)
354+
355+
return Message.newFromJsonDict(data)
356+
319357
def sendChatAction(self):
320358
url = '%s/sendChatAction' % (self.base_url)
321359

telegram/location.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python
2+
3+
4+
class Location(object):
5+
def __init__(self, **kwargs):
6+
param_defaults = {
7+
'longitude': None,
8+
'latitude': None,
9+
}
10+
11+
for (param, default) in param_defaults.iteritems():
12+
setattr(self, param, kwargs.get(param, default))
13+
14+
@staticmethod
15+
def newFromJsonDict(data):
16+
return Location(longitude=data.get('longitude', None),
17+
latitude=data.get('latitude', None))

telegram/message.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ def __init__(self, **kwargs):
2929

3030
for (param, default) in param_defaults.iteritems():
3131
setattr(self, param, kwargs.get(param, default))
32+
33+
@property
34+
def chat_id(self):
35+
return self.chat.id
3236

3337
@staticmethod
3438
def newFromJsonDict(data):
@@ -91,6 +95,12 @@ def newFromJsonDict(data):
9195
else:
9296
video = None
9397

98+
if 'location' in data:
99+
from telegram import Location
100+
location = Location.newFromJsonDict(data['location'])
101+
else:
102+
location = None
103+
94104
if 'new_chat_participant' in data:
95105
from telegram import User
96106
new_chat_participant = User.newFromJsonDict(
@@ -121,7 +131,7 @@ def newFromJsonDict(data):
121131
sticker=sticker,
122132
video=video,
123133
contact=data.get('contact', None),
124-
location=data.get('location', None),
134+
location=location,
125135
new_chat_participant=new_chat_participant,
126136
left_chat_participant=left_chat_participant,
127137
new_chat_title=data.get('new_chat_title', None),

telegram/user.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ def __init__(self, **kwargs):
1313
for (param, default) in param_defaults.iteritems():
1414
setattr(self, param, kwargs.get(param, default))
1515

16-
@property
17-
def chat_id(self):
18-
return self.id
19-
2016
@staticmethod
2117
def newFromJsonDict(data):
2218
return User(id=data.get('id', None),

tests/test_bot.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,14 @@ def testResendVideo(self):
9797
'''Test the telegram.Bot sendVideo method'''
9898
print 'Testing sendVideo - Resend'
9999
message = self._bot.sendVideo(video=str('BAADAQADIgEAAvjAuQABOuTB937fPTgC'),
100-
chat_id=12173560)
100+
chat_id=12173560)
101101
self.assertEqual(4, message.video.duration)
102+
103+
def testSendLocation(self):
104+
'''Test the telegram.Bot sendLocation method'''
105+
print 'Testing sendLocation'
106+
message = self._bot.sendLocation(latitude=-23.558873,
107+
longitude=-46.659732,
108+
chat_id=12173560)
109+
self.assertEqual(-23.558873, message.location.latitude)
110+
self.assertEqual(-46.659732, message.location.longitude)

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