mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-03-13 11:18:20 +01:00
Adding sendLocation, its tests and minor fixes
This commit is contained in:
parent
6bb8c4e4a0
commit
86c1f68419
6 changed files with 78 additions and 8 deletions
|
@ -17,7 +17,7 @@ from document import Document
|
||||||
from sticker import Sticker
|
from sticker import Sticker
|
||||||
from video import Video
|
from video import Video
|
||||||
# from contact import Contact
|
# from contact import Contact
|
||||||
# from location import Location
|
from location import Location
|
||||||
# from inputfile import InputFile
|
# from inputfile import InputFile
|
||||||
# from userprofilephotos import UserProfilePhotos
|
# from userprofilephotos import UserProfilePhotos
|
||||||
# from replykeyboardmarkup import ReplyKeyboardMarkup
|
# from replykeyboardmarkup import ReplyKeyboardMarkup
|
||||||
|
|
|
@ -313,9 +313,47 @@ class Bot(object):
|
||||||
|
|
||||||
return Message.newFromJsonDict(data)
|
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)
|
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):
|
def sendChatAction(self):
|
||||||
url = '%s/sendChatAction' % (self.base_url)
|
url = '%s/sendChatAction' % (self.base_url)
|
||||||
|
|
||||||
|
|
|
@ -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))
|
|
@ -29,6 +29,10 @@ class Message(object):
|
||||||
|
|
||||||
for (param, default) in param_defaults.iteritems():
|
for (param, default) in param_defaults.iteritems():
|
||||||
setattr(self, param, kwargs.get(param, default))
|
setattr(self, param, kwargs.get(param, default))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def chat_id(self):
|
||||||
|
return self.chat.id
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def newFromJsonDict(data):
|
def newFromJsonDict(data):
|
||||||
|
@ -91,6 +95,12 @@ class Message(object):
|
||||||
else:
|
else:
|
||||||
video = None
|
video = None
|
||||||
|
|
||||||
|
if 'location' in data:
|
||||||
|
from telegram import Location
|
||||||
|
location = Location.newFromJsonDict(data['location'])
|
||||||
|
else:
|
||||||
|
location = None
|
||||||
|
|
||||||
if 'new_chat_participant' in data:
|
if 'new_chat_participant' in data:
|
||||||
from telegram import User
|
from telegram import User
|
||||||
new_chat_participant = User.newFromJsonDict(
|
new_chat_participant = User.newFromJsonDict(
|
||||||
|
@ -121,7 +131,7 @@ class Message(object):
|
||||||
sticker=sticker,
|
sticker=sticker,
|
||||||
video=video,
|
video=video,
|
||||||
contact=data.get('contact', None),
|
contact=data.get('contact', None),
|
||||||
location=data.get('location', None),
|
location=location,
|
||||||
new_chat_participant=new_chat_participant,
|
new_chat_participant=new_chat_participant,
|
||||||
left_chat_participant=left_chat_participant,
|
left_chat_participant=left_chat_participant,
|
||||||
new_chat_title=data.get('new_chat_title', None),
|
new_chat_title=data.get('new_chat_title', None),
|
||||||
|
|
|
@ -13,10 +13,6 @@ class User(object):
|
||||||
for (param, default) in param_defaults.iteritems():
|
for (param, default) in param_defaults.iteritems():
|
||||||
setattr(self, param, kwargs.get(param, default))
|
setattr(self, param, kwargs.get(param, default))
|
||||||
|
|
||||||
@property
|
|
||||||
def chat_id(self):
|
|
||||||
return self.id
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def newFromJsonDict(data):
|
def newFromJsonDict(data):
|
||||||
return User(id=data.get('id', None),
|
return User(id=data.get('id', None),
|
||||||
|
|
|
@ -97,5 +97,14 @@ class BotTest(unittest.TestCase):
|
||||||
'''Test the telegram.Bot sendVideo method'''
|
'''Test the telegram.Bot sendVideo method'''
|
||||||
print 'Testing sendVideo - Resend'
|
print 'Testing sendVideo - Resend'
|
||||||
message = self._bot.sendVideo(video=str('BAADAQADIgEAAvjAuQABOuTB937fPTgC'),
|
message = self._bot.sendVideo(video=str('BAADAQADIgEAAvjAuQABOuTB937fPTgC'),
|
||||||
chat_id=12173560)
|
chat_id=12173560)
|
||||||
self.assertEqual(4, message.video.duration)
|
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)
|
||||||
|
|
Loading…
Add table
Reference in a new issue