Skip to content

Commit c20d646

Browse files
committed
Adding sendChatAction, its tests and minor fixes
1 parent 86c1f68 commit c20d646

6 files changed

Lines changed: 49 additions & 3 deletions

File tree

telegram/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from video import Video
1919
# from contact import Contact
2020
from location import Location
21+
from chataction import ChatAction
2122
# from inputfile import InputFile
2223
# from userprofilephotos import UserProfilePhotos
2324
# from replykeyboardmarkup import ReplyKeyboardMarkup

telegram/bot.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,37 @@ def sendLocation(self,
354354

355355
return Message.newFromJsonDict(data)
356356

357-
def sendChatAction(self):
357+
def sendChatAction(self,
358+
chat_id,
359+
action):
360+
"""Use this method when you need to tell the user that something is
361+
happening on the bot's side. The status is set for 5 seconds or less
362+
(when a message arrives from your bot, Telegram clients clear its
363+
typing status).
364+
365+
Args:
366+
chat_id:
367+
Unique identifier for the message recipient — User or GroupChat id.
368+
action:
369+
Type of action to broadcast. Choose one, depending on what the user
370+
is about to receive:
371+
- ChatAction.TYPING for text messages,
372+
- ChatAction.UPLOAD_PHOTO for photos,
373+
- ChatAction.UPLOAD_VIDEO or upload_video for videos,
374+
- ChatAction.UPLOAD_AUDIO or upload_audio for audio files,
375+
- ChatAction.UPLOAD_DOCUMENT for general files,
376+
- ChatAction.FIND_LOCATION for location data.
377+
Returns:
378+
?
379+
"""
380+
358381
url = '%s/sendChatAction' % (self.base_url)
359382

383+
data = {'chat_id': chat_id,
384+
'action': action}
385+
386+
self._requestUrl(url, 'POST', data=data)
387+
360388
def getUserProfilePhotos(self):
361389
url = '%s/getUserProfilePhotos' % (self.base_url)
362390

telegram/chataction.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env python
2+
3+
4+
class ChatAction(object):
5+
TYPING = 'typing'
6+
UPLOAD_PHOTO = 'upload_photo'
7+
RECORD_VIDEO = 'upload_video'
8+
RECORD_AUDIO = 'upload_audio'
9+
UPLOAD_DOCUMENT = 'upload_document'
10+
FIND_LOCATION = 'find_location'

telegram/location.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ def __init__(self, **kwargs):
1414
@staticmethod
1515
def newFromJsonDict(data):
1616
return Location(longitude=data.get('longitude', None),
17-
latitude=data.get('latitude', None))
17+
latitude=data.get('latitude', None))

telegram/message.py

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

3030
for (param, default) in param_defaults.iteritems():
3131
setattr(self, param, kwargs.get(param, default))
32-
32+
3333
@property
3434
def chat_id(self):
3535
return self.chat.id

tests/test_bot.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,10 @@ def testSendLocation(self):
108108
chat_id=12173560)
109109
self.assertEqual(-23.558873, message.location.latitude)
110110
self.assertEqual(-46.659732, message.location.longitude)
111+
112+
def testSendChatAction(self):
113+
'''Test the telegram.Bot sendChatAction method'''
114+
print 'Testing sendChatAction - ChatAction.TYPING'
115+
message = self._bot.sendChatAction(action=telegram.ChatAction.TYPING,
116+
chat_id=12173560)
117+
# TODO: return json

0 commit comments

Comments
 (0)