From c20d6465fcb201c5136b26e2afd4d6d7dc2e54dd Mon Sep 17 00:00:00 2001 From: Leandro Toledo Date: Wed, 8 Jul 2015 09:55:06 -0300 Subject: [PATCH] Adding sendChatAction, its tests and minor fixes --- telegram/__init__.py | 1 + telegram/bot.py | 30 +++++++++++++++++++++++++++++- telegram/chataction.py | 10 ++++++++++ telegram/location.py | 2 +- telegram/message.py | 2 +- tests/test_bot.py | 7 +++++++ 6 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 telegram/chataction.py diff --git a/telegram/__init__.py b/telegram/__init__.py index b2b76dcad..e810021b1 100644 --- a/telegram/__init__.py +++ b/telegram/__init__.py @@ -18,6 +18,7 @@ from sticker import Sticker from video import Video # from contact import Contact from location import Location +from chataction import ChatAction # from inputfile import InputFile # from userprofilephotos import UserProfilePhotos # from replykeyboardmarkup import ReplyKeyboardMarkup diff --git a/telegram/bot.py b/telegram/bot.py index f6b43a913..7d4efc354 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -354,9 +354,37 @@ class Bot(object): return Message.newFromJsonDict(data) - def sendChatAction(self): + def sendChatAction(self, + chat_id, + action): + """Use this method when you need to tell the user that something is + happening on the bot's side. The status is set for 5 seconds or less + (when a message arrives from your bot, Telegram clients clear its + typing status). + + Args: + chat_id: + Unique identifier for the message recipient — User or GroupChat id. + action: + Type of action to broadcast. Choose one, depending on what the user + is about to receive: + - ChatAction.TYPING for text messages, + - ChatAction.UPLOAD_PHOTO for photos, + - ChatAction.UPLOAD_VIDEO or upload_video for videos, + - ChatAction.UPLOAD_AUDIO or upload_audio for audio files, + - ChatAction.UPLOAD_DOCUMENT for general files, + - ChatAction.FIND_LOCATION for location data. + Returns: + ? + """ + url = '%s/sendChatAction' % (self.base_url) + data = {'chat_id': chat_id, + 'action': action} + + self._requestUrl(url, 'POST', data=data) + def getUserProfilePhotos(self): url = '%s/getUserProfilePhotos' % (self.base_url) diff --git a/telegram/chataction.py b/telegram/chataction.py new file mode 100644 index 000000000..f301318a4 --- /dev/null +++ b/telegram/chataction.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python + + +class ChatAction(object): + TYPING = 'typing' + UPLOAD_PHOTO = 'upload_photo' + RECORD_VIDEO = 'upload_video' + RECORD_AUDIO = 'upload_audio' + UPLOAD_DOCUMENT = 'upload_document' + FIND_LOCATION = 'find_location' diff --git a/telegram/location.py b/telegram/location.py index b51d3426b..72e9e2185 100644 --- a/telegram/location.py +++ b/telegram/location.py @@ -14,4 +14,4 @@ class Location(object): @staticmethod def newFromJsonDict(data): return Location(longitude=data.get('longitude', None), - latitude=data.get('latitude', None)) + latitude=data.get('latitude', None)) diff --git a/telegram/message.py b/telegram/message.py index 2006d711c..fca6d8e86 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -29,7 +29,7 @@ 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 diff --git a/tests/test_bot.py b/tests/test_bot.py index 5fcf78979..c84392402 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -108,3 +108,10 @@ class BotTest(unittest.TestCase): chat_id=12173560) self.assertEqual(-23.558873, message.location.latitude) self.assertEqual(-46.659732, message.location.longitude) + + def testSendChatAction(self): + '''Test the telegram.Bot sendChatAction method''' + print 'Testing sendChatAction - ChatAction.TYPING' + message = self._bot.sendChatAction(action=telegram.ChatAction.TYPING, + chat_id=12173560) + # TODO: return json