Adding sendLocation, its tests and minor fixes

This commit is contained in:
Leandro Toledo 2015-07-08 09:37:25 -03:00
parent 6bb8c4e4a0
commit 86c1f68419
6 changed files with 78 additions and 8 deletions

View file

@ -17,7 +17,7 @@ from document import Document
from sticker import Sticker
from video import Video
# from contact import Contact
# from location import Location
from location import Location
# from inputfile import InputFile
# from userprofilephotos import UserProfilePhotos
# from replykeyboardmarkup import ReplyKeyboardMarkup

View file

@ -313,9 +313,47 @@ class Bot(object):
return Message.newFromJsonDict(data)
def sendLocation(self):
def sendLocation(self,
chat_id,
latitude,
longitude,
reply_to_message_id=None,
reply_markup=None):
"""Use this method to send point on the map.
Args:
chat_id:
Unique identifier for the message recipient User or GroupChat id.
latitude:
Latitude of location.
longitude:
Longitude of location.
reply_to_message_id:
If the message is a reply, ID of the original message. [Optional]
reply_markup:
Additional interface options. A JSON-serialized object for a
custom reply keyboard, instructions to hide keyboard or to force a
reply from the user. [Optional]
Returns:
A telegram.Message instance representing the message posted.
"""
url = '%s/sendLocation' % (self.base_url)
data = {'chat_id': chat_id,
'latitude': latitude,
'longitude': longitude}
if reply_to_message_id:
data['reply_to_message_id'] = reply_to_message_id
if reply_markup:
data['reply_markup'] = reply_markup
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data.content)
return Message.newFromJsonDict(data)
def sendChatAction(self):
url = '%s/sendChatAction' % (self.base_url)

View file

@ -0,0 +1,17 @@
#!/usr/bin/env python
class Location(object):
def __init__(self, **kwargs):
param_defaults = {
'longitude': None,
'latitude': None,
}
for (param, default) in param_defaults.iteritems():
setattr(self, param, kwargs.get(param, default))
@staticmethod
def newFromJsonDict(data):
return Location(longitude=data.get('longitude', None),
latitude=data.get('latitude', None))

View file

@ -29,6 +29,10 @@ class Message(object):
for (param, default) in param_defaults.iteritems():
setattr(self, param, kwargs.get(param, default))
@property
def chat_id(self):
return self.chat.id
@staticmethod
def newFromJsonDict(data):
@ -91,6 +95,12 @@ class Message(object):
else:
video = None
if 'location' in data:
from telegram import Location
location = Location.newFromJsonDict(data['location'])
else:
location = None
if 'new_chat_participant' in data:
from telegram import User
new_chat_participant = User.newFromJsonDict(
@ -121,7 +131,7 @@ class Message(object):
sticker=sticker,
video=video,
contact=data.get('contact', None),
location=data.get('location', None),
location=location,
new_chat_participant=new_chat_participant,
left_chat_participant=left_chat_participant,
new_chat_title=data.get('new_chat_title', None),

View file

@ -13,10 +13,6 @@ class User(object):
for (param, default) in param_defaults.iteritems():
setattr(self, param, kwargs.get(param, default))
@property
def chat_id(self):
return self.id
@staticmethod
def newFromJsonDict(data):
return User(id=data.get('id', None),

View file

@ -97,5 +97,14 @@ class BotTest(unittest.TestCase):
'''Test the telegram.Bot sendVideo method'''
print 'Testing sendVideo - Resend'
message = self._bot.sendVideo(video=str('BAADAQADIgEAAvjAuQABOuTB937fPTgC'),
chat_id=12173560)
chat_id=12173560)
self.assertEqual(4, message.video.duration)
def testSendLocation(self):
'''Test the telegram.Bot sendLocation method'''
print 'Testing sendLocation'
message = self._bot.sendLocation(latitude=-23.558873,
longitude=-46.659732,
chat_id=12173560)
self.assertEqual(-23.558873, message.location.latitude)
self.assertEqual(-46.659732, message.location.longitude)