1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Implemented #17 (with some small adjustments) and ForceReply

Changed apihelper#convert_markup
Constructed the Jsonable abstract class. All subclasses must override Jsonable#to_json.
Made ReplyKeyboardHide, ReplyKeyboardMarkup and ForceReply a subclass of Jsonable to make things less complicated in convert_markup.
This commit is contained in:
pieter 2015-07-01 18:17:33 +02:00
parent 8812765652
commit b444565b7b
2 changed files with 38 additions and 4 deletions

View File

@ -146,10 +146,9 @@ def check_result(func_name, result):
return result_json return result_json
def convert_markup(markup): def convert_markup(markup):
if isinstance(markup, types.ReplyKeyboardMarkup): if not isinstance(markup, types.Jsonable):
return markup.to_json() return markup.to_json()
else: return markup
return markup
class ApiError(Exception): class ApiError(Exception):
def __init__(self, message, result): def __init__(self, message, result):

View File

@ -22,6 +22,19 @@ ForceReply
import json import json
class Jsonable:
"""
Subclasses of this class are guaranteed to be able to be converted to JSON format.
All subclasses of this class must override to_json.
"""
def to_json(self):
"""
Returns a JSON string representation of this class.
This function must be overridden by subclasses.
:return: a JSON formatted string.
"""
raise NotImplementedError
class User: class User:
@classmethod @classmethod
@ -268,7 +281,29 @@ class UserProfilePhotos:
self.photos = photos self.photos = photos
class ReplyKeyboardMarkup: class ForceReply(Jsonable):
def __init__(self, selective=None):
self.selective = selective
def to_json(self):
json_dict = {'force_reply': True}
if self.selective:
json_dict['selective'] = True
return json.dumps(json_dict)
class ReplyKeyboardHide(Jsonable):
def __init__(self, selective=None):
self.selective = selective
def to_json(self):
json_dict = {'hide_keyboard': True}
if self.selective:
json_dict['selective'] = True
return json.dumps(json_dict)
class ReplyKeyboardMarkup(Jsonable):
def __init__(self, resize_keyboard=None, one_time_keyboard=None, selective=None, row_width=3): def __init__(self, resize_keyboard=None, one_time_keyboard=None, selective=None, row_width=3):
self.resize_keyboard = resize_keyboard self.resize_keyboard = resize_keyboard
self.one_time_keyboard = one_time_keyboard self.one_time_keyboard = one_time_keyboard