X Tutup
Skip to content

Commit b90b608

Browse files
committed
Add to_json method to classes
1 parent 73cbd11 commit b90b608

File tree

16 files changed

+240
-8
lines changed

16 files changed

+240
-8
lines changed

telegram/audio.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env python
22

33

4+
import json
5+
6+
47
class Audio(object):
58
def __init__(self,
69
file_id,
@@ -18,3 +21,15 @@ def de_json(data):
1821
duration=data.get('duration', None),
1922
mime_type=data.get('mime_type', None),
2023
file_size=data.get('file_size', None))
24+
25+
def to_json(self):
26+
json_data = {'file_id': self.file_id,
27+
'duration': self.duration}
28+
if self.mime_type:
29+
json_data['mime_type'] = self.mime_type
30+
if self.file_size:
31+
json_data['file_size'] = self.file_size
32+
return json.dumps(json_data)
33+
34+
def __str__(self):
35+
return self.to_json()

telegram/bot.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ def sendPhoto(self,
197197
if reply_to_message_id:
198198
data['reply_to_message_id'] = reply_to_message_id
199199
if reply_markup:
200-
data['reply_markup'] = reply_markup
200+
if isinstance(reply_markup, ReplyMarkup):
201+
data['reply_markup'] = reply_markup.to_json()
202+
else:
203+
data['reply_markup'] = reply_markup
201204

202205
json_data = self._requestUrl(url, 'POST', data=data)
203206
data = self._parseAndCheckTelegram(json_data.content)
@@ -243,7 +246,10 @@ def sendAudio(self,
243246
if reply_to_message_id:
244247
data['reply_to_message_id'] = reply_to_message_id
245248
if reply_markup:
246-
data['reply_markup'] = reply_markup
249+
if isinstance(reply_markup, ReplyMarkup):
250+
data['reply_markup'] = reply_markup.to_json()
251+
else:
252+
data['reply_markup'] = reply_markup
247253

248254
json_data = self._requestUrl(url, 'POST', data=data)
249255
data = self._parseAndCheckTelegram(json_data.content)
@@ -286,7 +292,10 @@ def sendDocument(self,
286292
if reply_to_message_id:
287293
data['reply_to_message_id'] = reply_to_message_id
288294
if reply_markup:
289-
data['reply_markup'] = reply_markup
295+
if isinstance(reply_markup, ReplyMarkup):
296+
data['reply_markup'] = reply_markup.to_json()
297+
else:
298+
data['reply_markup'] = reply_markup
290299

291300
json_data = self._requestUrl(url, 'POST', data=data)
292301
data = self._parseAndCheckTelegram(json_data.content)
@@ -329,7 +338,10 @@ def sendSticker(self,
329338
if reply_to_message_id:
330339
data['reply_to_message_id'] = reply_to_message_id
331340
if reply_markup:
332-
data['reply_markup'] = reply_markup
341+
if isinstance(reply_markup, ReplyMarkup):
342+
data['reply_markup'] = reply_markup.to_json()
343+
else:
344+
data['reply_markup'] = reply_markup
333345

334346
json_data = self._requestUrl(url, 'POST', data=data)
335347
data = self._parseAndCheckTelegram(json_data.content)
@@ -373,7 +385,10 @@ def sendVideo(self,
373385
if reply_to_message_id:
374386
data['reply_to_message_id'] = reply_to_message_id
375387
if reply_markup:
376-
data['reply_markup'] = reply_markup
388+
if isinstance(reply_markup, ReplyMarkup):
389+
data['reply_markup'] = reply_markup.to_json()
390+
else:
391+
data['reply_markup'] = reply_markup
377392

378393
json_data = self._requestUrl(url, 'POST', data=data)
379394
data = self._parseAndCheckTelegram(json_data.content)
@@ -418,7 +433,10 @@ def sendLocation(self,
418433
if reply_to_message_id:
419434
data['reply_to_message_id'] = reply_to_message_id
420435
if reply_markup:
421-
data['reply_markup'] = reply_markup
436+
if isinstance(reply_markup, ReplyMarkup):
437+
data['reply_markup'] = reply_markup.to_json()
438+
else:
439+
data['reply_markup'] = reply_markup
422440

423441
json_data = self._requestUrl(url, 'POST', data=data)
424442
data = self._parseAndCheckTelegram(json_data.content)

telegram/contact.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env python
22

33

4+
import json
5+
6+
47
class Contact(object):
58
def __init__(self,
69
phone_number,
@@ -18,3 +21,15 @@ def de_json(data):
1821
first_name=data.get('first_name', None),
1922
last_name=data.get('last_name', None),
2023
user_id=data.get('user_id', None))
24+
25+
def to_json(self):
26+
json_data = {'phone_number': self.phone_number,
27+
'first_name': self.first_name}
28+
if self.last_name:
29+
json_data['last_name'] = self.last_name
30+
if self.user_id:
31+
json_data['user_id'] = self.user_id
32+
return json.dumps(json_data)
33+
34+
def __str__(self):
35+
return self.to_json()

telegram/document.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env python
22

33

4+
import json
5+
6+
47
class Document(object):
58
def __init__(self,
69
file_id,
@@ -27,3 +30,17 @@ def de_json(data):
2730
file_name=data.get('file_name', None),
2831
mime_type=data.get('mime_type', None),
2932
file_size=data.get('file_size', None))
33+
34+
def to_json(self):
35+
json_data = {'file_id': self.file_id,
36+
'thumb': self.thumb.to_json()}
37+
if self.file_name:
38+
json_data['file_name'] = self.file_name
39+
if self.mime_type:
40+
json_data['mime_type'] = self.mime_type
41+
if self.file_size:
42+
json_data['file_size'] = self.file_size
43+
return json.dumps(json_data)
44+
45+
def __str__(self):
46+
return self.to_json()

telegram/forcereply.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ def to_json(self):
2222
if self.selective:
2323
json_data['selective'] = self.selective
2424
return json.dumps(json_data)
25+
26+
def __str__(self):
27+
return self.to_json()

telegram/groupchat.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env python
22

33

4+
import json
5+
6+
47
class GroupChat(object):
58
def __init__(self,
69
id,
@@ -12,3 +15,11 @@ def __init__(self,
1215
def de_json(data):
1316
return GroupChat(id=data.get('id', None),
1417
title=data.get('title', None))
18+
19+
def to_json(self):
20+
json_data = {'id': self.id,
21+
'title': self.title}
22+
return json.dumps(json_data)
23+
24+
def __str__(self):
25+
return self.to_json()

telegram/location.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env python
22

33

4+
import json
5+
6+
47
class Location(object):
58
def __init__(self,
69
longitude,
@@ -12,3 +15,11 @@ def __init__(self,
1215
def de_json(data):
1316
return Location(longitude=data.get('longitude', None),
1417
latitude=data.get('latitude', None))
18+
19+
def to_json(self):
20+
json_data = {'longitude': self.longitude,
21+
'latitude': self.latitude}
22+
return json.dumps(json_data)
23+
24+
def __str__(self):
25+
return self.to_json()

telegram/message.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env python
22

33

4+
import json
5+
6+
47
class Message(object):
58
def __init__(self,
69
message_id,
@@ -52,7 +55,7 @@ def chat_id(self):
5255

5356
@staticmethod
5457
def de_json(data):
55-
if 'from' in data: # from is a reserved word, use user_from instead.
58+
if 'from' in data: # from is a reserved word, use from_user instead.
5659
from telegram import User
5760
from_user = User.de_json(data['from'])
5861
else:
@@ -154,3 +157,47 @@ def de_json(data):
154157
new_chat_photo=data.get('new_chat_photo', None),
155158
delete_chat_photo=data.get('delete_chat_photo', None),
156159
group_chat_created=data.get('group_chat_created', None))
160+
161+
def to_json(self):
162+
json_data = {'message_id': self.message_id,
163+
'from': self.from_user.to_json(),
164+
'date': self.date,
165+
'chat': self.chat.to_json()}
166+
if self.forward_from:
167+
json_data['forward_from'] = self.forward_from
168+
if self.forward_date:
169+
json_data['forward_date'] = self.forward_date
170+
if self.reply_to_message:
171+
json_data['reply_to_message'] = self.reply_to_message
172+
if self.text:
173+
json_data['text'] = self.text
174+
if self.audio:
175+
json_data['audio'] = self.audio.to_json()
176+
if self.document:
177+
json_data['document'] = self.document.to_json()
178+
if self.photo:
179+
json_data['photo'] = self.photo.to_json()
180+
if self.sticker:
181+
json_data['sticker'] = self.sticker.to_json()
182+
if self.video:
183+
json_data['video'] = self.video.to_json()
184+
if self.contact:
185+
json_data['contact'] = self.contact.to_json()
186+
if self.location:
187+
json_data['location'] = self.location.to_json()
188+
if self.new_chat_participant:
189+
json_data['new_chat_participant'] = self.new_chat_participant
190+
if self.left_chat_participant:
191+
json_data['left_chat_participant'] = self.left_chat_participant
192+
if self.new_chat_title:
193+
json_data['new_chat_title'] = self.new_chat_title
194+
if self.new_chat_photo:
195+
json_data['new_chat_photo'] = self.new_chat_photo
196+
if self.delete_chat_photo:
197+
json_data['delete_chat_photo'] = self.delete_chat_photo
198+
if self.group_chat_created:
199+
json_data['group_chat_created'] = self.group_chat_created
200+
return json.dumps(json_data)
201+
202+
def __str__(self):
203+
return self.to_json()

telegram/photosize.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env python
22

33

4+
import json
5+
6+
47
class PhotoSize(object):
58
def __init__(self,
69
file_id,
@@ -18,3 +21,14 @@ def de_json(data):
1821
width=data.get('width', None),
1922
height=data.get('height', None),
2023
file_size=data.get('file_size', None))
24+
25+
def to_json(self):
26+
json_data = {'file_id': self.file_id,
27+
'width': self.width,
28+
'height': self.height}
29+
if self.file_size:
30+
json_data['file_size'] = self.file_size
31+
return json.dumps(json_data)
32+
33+
def __str__(self):
34+
return self.to_json()

telegram/replymarkup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
class ReplyMarkup(object):
55
def to_json(self):
66
raise NotImplementedError
7+
8+
def __str__(self):
9+
return self.to_json()

0 commit comments

Comments
 (0)
X Tutup