Adding ReplyMarkup, ForceReply, renaming methods and minor fixes

This commit is contained in:
Leandro Toledo 2015-07-08 21:15:46 -03:00
parent 7e0ce5ffe4
commit 0214910741
17 changed files with 83 additions and 46 deletions

View file

@ -24,6 +24,7 @@ from chataction import ChatAction
from userprofilephotos import UserProfilePhotos
# from replykeyboardmarkup import ReplyKeyboardMarkup
# from replykeyboardhide import ReplyKeyboardHide
# from forcereply import ForceReply
from forcereply import ForceReply
from replymarkup import ReplyMarkup
from error import TelegramError
from bot import Bot

View file

@ -14,7 +14,7 @@ class Audio(object):
setattr(self, param, kwargs.get(param, default))
@staticmethod
def newFromJsonDict(data):
def de_json(data):
return Audio(file_id=data.get('file_id', None),
duration=data.get('duration', None),
mime_type=data.get('mime_type', None),

View file

@ -6,7 +6,7 @@
import json
import requests
from telegram import (User, Message, Update, UserProfilePhotos, TelegramError)
from telegram import (User, Message, Update, UserProfilePhotos, TelegramError, ReplyMarkup)
class Bot(object):
@ -66,7 +66,7 @@ class Bot(object):
json_data = self._requestUrl(url, 'GET')
data = self._parseAndCheckTelegram(json_data.content)
return User.newFromJsonDict(data)
return User.de_json(data)
def sendMessage(self,
chat_id,
@ -107,12 +107,15 @@ class Bot(object):
if reply_to_message_id:
data['reply_to_message_id'] = reply_to_message_id
if reply_markup:
data['reply_markup'] = reply_markup
if isinstance(reply_markup, ReplyMarkup):
data['reply_markup'] = reply_markup.to_json()
else:
data['reply_markup'] = reply_markup
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data.content)
return Message.newFromJsonDict(data)
return Message.de_json(data)
def forwardMessage(self,
chat_id,
@ -149,7 +152,7 @@ class Bot(object):
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data.content)
return Message.newFromJsonDict(data)
return Message.de_json(data)
def sendPhoto(self,
chat_id,
@ -198,7 +201,7 @@ class Bot(object):
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data.content)
return Message.newFromJsonDict(data)
return Message.de_json(data)
def sendAudio(self,
chat_id,
@ -244,7 +247,7 @@ class Bot(object):
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data.content)
return Message.newFromJsonDict(data)
return Message.de_json(data)
def sendDocument(self,
chat_id,
@ -287,7 +290,7 @@ class Bot(object):
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data.content)
return Message.newFromJsonDict(data)
return Message.de_json(data)
def sendSticker(self,
chat_id,
@ -330,7 +333,7 @@ class Bot(object):
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data.content)
return Message.newFromJsonDict(data)
return Message.de_json(data)
def sendVideo(self,
chat_id,
@ -374,7 +377,7 @@ class Bot(object):
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data.content)
return Message.newFromJsonDict(data)
return Message.de_json(data)
def sendLocation(self,
chat_id,
@ -419,7 +422,7 @@ class Bot(object):
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data.content)
return Message.newFromJsonDict(data)
return Message.de_json(data)
def sendChatAction(self,
chat_id,
@ -488,7 +491,7 @@ class Bot(object):
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data.content)
return UserProfilePhotos.newFromJsonDict(data)
return UserProfilePhotos.de_json(data)
def getUpdates(self,
offset=None,
@ -530,7 +533,7 @@ class Bot(object):
json_data = self._requestUrl(url, 'POST', data=data)
data = self._parseAndCheckTelegram(json_data.content)
return [Update.newFromJsonDict(x) for x in data]
return [Update.de_json(x) for x in data]
def setWebhook(self):
url = '%s/setWebhook' % (self.base_url)

View file

@ -14,7 +14,7 @@ class Contact(object):
setattr(self, param, kwargs.get(param, default))
@staticmethod
def newFromJsonDict(data):
def de_json(data):
return Contact(phone_number=data.get('phone_number', None),
first_name=data.get('first_name', None),
last_name=data.get('last_name', None),

View file

@ -15,10 +15,10 @@ class Document(object):
setattr(self, param, kwargs.get(param, default))
@staticmethod
def newFromJsonDict(data):
def de_json(data):
if 'thumb' in data:
from telegram import PhotoSize
thumb = PhotoSize.newFromJsonDict(data['thumb'])
thumb = PhotoSize.de_json(data['thumb'])
else:
thumb = None

View file

@ -0,0 +1,27 @@
#!/usr/bin/env python
import json
from replymarkup import ReplyMarkup
class ForceReply(ReplyMarkup):
def __init__(self, **kwargs):
param_defaults = {
'force_reply': True,
'selective': None
}
for (param, default) in param_defaults.iteritems():
setattr(self, param, kwargs.get(param, default))
@staticmethod
def de_json(data):
return ForceReply(force_reply=data.get('force_reply', None),
selective=data.get('selective', None))
def to_json(self):
json_data = {'force_reply': self.force_reply}
if self.selective:
json_data['selective'] = self.selective
return json.dumps(json_data)

View file

@ -16,6 +16,6 @@ class GroupChat(object):
return self.id
@staticmethod
def newFromJsonDict(data):
def de_json(data):
return GroupChat(id=data.get('id', None),
title=data.get('title', None))

View file

@ -12,6 +12,6 @@ class Location(object):
setattr(self, param, kwargs.get(param, default))
@staticmethod
def newFromJsonDict(data):
def de_json(data):
return Location(longitude=data.get('longitude', None),
latitude=data.get('latitude', None))

View file

@ -35,31 +35,31 @@ class Message(object):
return self.chat.id
@staticmethod
def newFromJsonDict(data):
def de_json(data):
if 'from' in data: # from on api
from telegram import User
user = User.newFromJsonDict(data['from'])
user = User.de_json(data['from'])
else:
user = None
if 'chat' in data:
if 'username' in data['chat']:
from telegram import User
chat = User.newFromJsonDict(data['chat'])
chat = User.de_json(data['chat'])
if 'title' in data['chat']:
from telegram import GroupChat
chat = GroupChat.newFromJsonDict(data['chat'])
chat = GroupChat.de_json(data['chat'])
else:
chat = None
if 'forward_from' in data:
from telegram import User
forward_from = User.newFromJsonDict(data['forward_from'])
forward_from = User.de_json(data['forward_from'])
else:
forward_from = None
if 'reply_to_message' in data:
reply_to_message = Message.newFromJsonDict(
reply_to_message = Message.de_json(
data['reply_to_message']
)
else:
@ -67,49 +67,49 @@ class Message(object):
if 'audio' in data:
from telegram import Audio
audio = Audio.newFromJsonDict(data['audio'])
audio = Audio.de_json(data['audio'])
else:
audio = None
if 'document' in data:
from telegram import Document
document = Document.newFromJsonDict(data['document'])
document = Document.de_json(data['document'])
else:
document = None
if 'photo' in data:
from telegram import PhotoSize
photo = [PhotoSize.newFromJsonDict(x) for x in data['photo']]
photo = [PhotoSize.de_json(x) for x in data['photo']]
else:
photo = None
if 'sticker' in data:
from telegram import Sticker
sticker = Sticker.newFromJsonDict(data['sticker'])
sticker = Sticker.de_json(data['sticker'])
else:
sticker = None
if 'video' in data:
from telegram import Video
video = Video.newFromJsonDict(data['video'])
video = Video.de_json(data['video'])
else:
video = None
if 'contact' in data:
from telegram import Contact
contact = Contact.newFromJsonDict(data['contact'])
contact = Contact.de_json(data['contact'])
else:
contact = None
if 'location' in data:
from telegram import Location
location = Location.newFromJsonDict(data['location'])
location = Location.de_json(data['location'])
else:
location = None
if 'new_chat_participant' in data:
from telegram import User
new_chat_participant = User.newFromJsonDict(
new_chat_participant = User.de_json(
data['new_chat_participant']
)
else:
@ -117,7 +117,7 @@ class Message(object):
if 'left_chat_participant' in data:
from telegram import User
left_chat_participant = User.newFromJsonDict(
left_chat_participant = User.de_json(
data['left_chat_participant']
)
else:

View file

@ -14,7 +14,7 @@ class PhotoSize(object):
setattr(self, param, kwargs.get(param, default))
@staticmethod
def newFromJsonDict(data):
def de_json(data):
return PhotoSize(file_id=data.get('file_id', None),
width=data.get('width', None),
height=data.get('height', None),

6
telegram/replymarkup.py Normal file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env python
class ReplyMarkup(object):
def to_json(self):
raise NotImplementedError

View file

@ -15,10 +15,10 @@ class Sticker(object):
setattr(self, param, kwargs.get(param, default))
@staticmethod
def newFromJsonDict(data):
def de_json(data):
if 'thumb' in data:
from telegram import PhotoSize
thumb = PhotoSize.newFromJsonDict(data['thumb'])
thumb = PhotoSize.de_json(data['thumb'])
else:
thumb = None

View file

@ -12,10 +12,10 @@ class Update(object):
setattr(self, param, kwargs.get(param, default))
@staticmethod
def newFromJsonDict(data):
def de_json(data):
if 'message' in data:
from telegram import Message
message = Message.newFromJsonDict(data['message'])
message = Message.de_json(data['message'])
else:
message = None

View file

@ -14,7 +14,7 @@ class User(object):
setattr(self, param, kwargs.get(param, default))
@staticmethod
def newFromJsonDict(data):
def de_json(data):
return User(id=data.get('id', None),
first_name=data.get('first_name', None),
last_name=data.get('last_name', None),

View file

@ -12,12 +12,12 @@ class UserProfilePhotos(object):
setattr(self, param, kwargs.get(param, default))
@staticmethod
def newFromJsonDict(data):
def de_json(data):
if 'photos' in data:
from telegram import PhotoSize
photos = []
for photo in data['photos']:
photos.append([PhotoSize.newFromJsonDict(x) for x in photo])
photos.append([PhotoSize.de_json(x) for x in photo])
else:
photos = None

View file

@ -18,10 +18,10 @@ class Video(object):
setattr(self, param, kwargs.get(param, default))
@staticmethod
def newFromJsonDict(data):
def de_json(data):
if 'thumb' in data:
from telegram import PhotoSize
thumb = PhotoSize.newFromJsonDict(data['thumb'])
thumb = PhotoSize.de_json(data['thumb'])
else:
thumb = None

View file

@ -33,7 +33,7 @@ class BotTest(unittest.TestCase):
'''Test the telegram.Bot getUpdates method'''
print 'Testing getUpdates'
updates = self._bot.getUpdates()
self.assertEqual(129566520, updates[0].update_id)
self.assertEqual(129566524, updates[0].update_id)
def testForwardMessage(self):
'''Test the telegram.Bot forwardMessage method'''