Repository navigation
Expand file tree
/
Copy pathtype_helpers.py
More file actions
151 lines (117 loc) · 4.59 KB
/
Copy pathtype_helpers.py
File metadata and controls
151 lines (117 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import six
import json
import telegram_types as types
class KeyboardHelper:
"""
Helper class to construct a ReplyKeyboardMarkup object.
Basic usage:
>>> keyboard = KeyboardHelper().add("A").add("B").add("C").new_row().add("D").add("E").build()
>>> another = KeyboardHelper().add("Send location", request_location=True).build(selective=True)
"""
def __init__(self):
self.keyboard = [[]]
def __add(self, item):
self.keyboard[-1].append(item)
def add(self, text, **kwargs):
"""
Add a new button to the current row.
:param text: Text of the button. If none of the optional fields are used,
it will be sent to the bot as a message when the button is pressed
:param kwargs: Optional values to be passed to the KeyboardButton constructor
:return: KeyboardHelper self for method chaining
"""
self.keyboard[-1].append(types.KeyboardButton(text, **kwargs))
return self
def new_row(self):
"""
Switch to a new row.
:return: KeyboardHelper self for method chaining
"""
self.keyboard.append([])
return self
def build(self, **kwargs):
"""
Build the ReplyKeyboardMarkup object.
:param kwargs: Any optional arguments to be passed to the ReplyKeyboardMarkup constructor
:return: types.ReplyKeyboardMarkup
"""
return types.ReplyKeyboardMarkup(self.keyboard, **kwargs)
class InlineKeyboardHelper:
"""
Helper class to construct an InlineKeyboardMarkup object.
Basic usage:
>>> keyboard = InlineKeyboardHelper().add("A").add("B").add("C").new_row().add("D").add("E").build()
>>> another = InlineKeyboardHelper().add("Visit site", url="http://telegram.org").build(selective=True)
"""
def __init__(self):
self.keyboard = [[]]
def add(self, text, **kwargs):
"""
Add a new button to the current row.
:param text: Label text on the button
:param kwargs: Optional values to be passed to the InlineKeyboardButton constructor
:return: KeyboardHelper self for method chaining
"""
self.keyboard[-1].append(types.InlineKeyboardButton(text, **kwargs))
return self
def new_row(self):
"""
Switch to a new row.
:return: KeyboardHelper self for method chaining
"""
self.keyboard.append([])
return self
def build(self):
"""
Build the InlineReplyKeyboardMarkup object.
:return: types.ReplyKeyboardMarkup
"""
return types.InlineKeyboardMarkup(self.keyboard)
class InlineQueryResultHelper:
TYPES = [
'article', 'audio', 'contact', 'document', 'gif', 'location',
'mpeg4_gif', 'photo', 'venue', 'video', 'voice'
]
CACHED_TYPES = ['photo', 'gif', 'mpeg4_gif', 'sticker', 'document', 'video', 'voice', 'audio']
__CACHED_FILE_ID_KEYS = {
'photo': 'photo_file_id',
'gif': 'gif_file_id',
'mpeg4_gif': 'mpeg4_file_id',
'sticker': 'sticker_file_id',
'document': 'document_file_id',
'video': 'video_file_id',
'voice': 'voice_file_id',
'audio': 'audio_file_id'
}
def __init__(self):
raise NotImplementedError('Instantiation not allowed')
@classmethod
def create_result(cls, type, id, **kwargs):
if type not in cls.TYPES:
raise ValueError('Unknown type: {0}. Known types: {1}'.format(type, cls.TYPES))
json_dict = kwargs
json_dict['type'] = type
json_dict['id'] = id
for k, v in six.iteritems(json_dict):
json_dict[k] = types.to_dict(v)
return json_dict
@classmethod
def create_cached_result(cls, type, id, file_id, **kwargs):
if type not in cls.CACHED_TYPES:
raise ValueError('Unknown cached type: {0}. Known types: {1}'.format(type, cls.CACHED_TYPES))
json_dict = kwargs
json_dict['type'] = type
json_dict['id'] = id
json_dict[cls.__CACHED_FILE_ID_KEYS[type]] = file_id
for k, v in six.iteritems(json_dict):
json_dict[k] = types.to_dict(v)
return json_dict
class InlineQueryResultsBuilder:
def __init__(self):
self.results = []
def add(self, type, id, **kwargs):
self.results.append(InlineQueryResultHelper.create_result(type, id, **kwargs))
def add_cached(self, type, id, file_id, **kwargs):
self.results.append(InlineQueryResultHelper.create_cached_result(type, id, file_id, **kwargs))
def build(self):
return json.dumps(self.results)