diff --git a/telegram/__init__.py b/telegram/__init__.py index acf8c1dca..5688c7d2c 100644 --- a/telegram/__init__.py +++ b/telegram/__init__.py @@ -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 diff --git a/telegram/audio.py b/telegram/audio.py index d8d2390a5..fc082d1d0 100644 --- a/telegram/audio.py +++ b/telegram/audio.py @@ -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), diff --git a/telegram/bot.py b/telegram/bot.py index 4d5f6bd50..7f7c7cdc9 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -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) diff --git a/telegram/contact.py b/telegram/contact.py index c0928e2fd..ae10982d3 100644 --- a/telegram/contact.py +++ b/telegram/contact.py @@ -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), diff --git a/telegram/document.py b/telegram/document.py index 1bdba84df..9c99bf051 100644 --- a/telegram/document.py +++ b/telegram/document.py @@ -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 diff --git a/telegram/forcereply.py b/telegram/forcereply.py index e69de29bb..45b4512b5 100644 --- a/telegram/forcereply.py +++ b/telegram/forcereply.py @@ -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) diff --git a/telegram/groupchat.py b/telegram/groupchat.py index 295e68932..c2afee1d1 100644 --- a/telegram/groupchat.py +++ b/telegram/groupchat.py @@ -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)) diff --git a/telegram/location.py b/telegram/location.py index 72e9e2185..cb005994d 100644 --- a/telegram/location.py +++ b/telegram/location.py @@ -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)) diff --git a/telegram/message.py b/telegram/message.py index f5432609a..7686c862f 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -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: diff --git a/telegram/photosize.py b/telegram/photosize.py index d09321db1..f48022b18 100644 --- a/telegram/photosize.py +++ b/telegram/photosize.py @@ -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), diff --git a/telegram/replymarkup.py b/telegram/replymarkup.py new file mode 100644 index 000000000..9d65d80d3 --- /dev/null +++ b/telegram/replymarkup.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python + + +class ReplyMarkup(object): + def to_json(self): + raise NotImplementedError diff --git a/telegram/sticker.py b/telegram/sticker.py index f26ab74f2..fd6f4934b 100644 --- a/telegram/sticker.py +++ b/telegram/sticker.py @@ -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 diff --git a/telegram/update.py b/telegram/update.py index 438a42c07..74950ceff 100644 --- a/telegram/update.py +++ b/telegram/update.py @@ -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 diff --git a/telegram/user.py b/telegram/user.py index e3e18f257..5b82fa13b 100644 --- a/telegram/user.py +++ b/telegram/user.py @@ -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), diff --git a/telegram/userprofilephotos.py b/telegram/userprofilephotos.py index ce111c710..e040ada9c 100644 --- a/telegram/userprofilephotos.py +++ b/telegram/userprofilephotos.py @@ -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 diff --git a/telegram/video.py b/telegram/video.py index 6204cdb49..a34724edd 100644 --- a/telegram/video.py +++ b/telegram/video.py @@ -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 diff --git a/tests/test_bot.py b/tests/test_bot.py index 68d835255..ed865370b 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -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'''