mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-01-01 08:56:27 +01:00
Adding ReplyMarkup, ForceReply, renaming methods and minor fixes
This commit is contained in:
parent
7e0ce5ffe4
commit
0214910741
17 changed files with 83 additions and 46 deletions
|
@ -24,6 +24,7 @@ from chataction import ChatAction
|
||||||
from userprofilephotos import UserProfilePhotos
|
from userprofilephotos import UserProfilePhotos
|
||||||
# from replykeyboardmarkup import ReplyKeyboardMarkup
|
# from replykeyboardmarkup import ReplyKeyboardMarkup
|
||||||
# from replykeyboardhide import ReplyKeyboardHide
|
# from replykeyboardhide import ReplyKeyboardHide
|
||||||
# from forcereply import ForceReply
|
from forcereply import ForceReply
|
||||||
|
from replymarkup import ReplyMarkup
|
||||||
from error import TelegramError
|
from error import TelegramError
|
||||||
from bot import Bot
|
from bot import Bot
|
||||||
|
|
|
@ -14,7 +14,7 @@ class Audio(object):
|
||||||
setattr(self, param, kwargs.get(param, default))
|
setattr(self, param, kwargs.get(param, default))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def newFromJsonDict(data):
|
def de_json(data):
|
||||||
return Audio(file_id=data.get('file_id', None),
|
return Audio(file_id=data.get('file_id', None),
|
||||||
duration=data.get('duration', None),
|
duration=data.get('duration', None),
|
||||||
mime_type=data.get('mime_type', None),
|
mime_type=data.get('mime_type', None),
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from telegram import (User, Message, Update, UserProfilePhotos, TelegramError)
|
from telegram import (User, Message, Update, UserProfilePhotos, TelegramError, ReplyMarkup)
|
||||||
|
|
||||||
|
|
||||||
class Bot(object):
|
class Bot(object):
|
||||||
|
@ -66,7 +66,7 @@ class Bot(object):
|
||||||
json_data = self._requestUrl(url, 'GET')
|
json_data = self._requestUrl(url, 'GET')
|
||||||
data = self._parseAndCheckTelegram(json_data.content)
|
data = self._parseAndCheckTelegram(json_data.content)
|
||||||
|
|
||||||
return User.newFromJsonDict(data)
|
return User.de_json(data)
|
||||||
|
|
||||||
def sendMessage(self,
|
def sendMessage(self,
|
||||||
chat_id,
|
chat_id,
|
||||||
|
@ -107,12 +107,15 @@ class Bot(object):
|
||||||
if reply_to_message_id:
|
if reply_to_message_id:
|
||||||
data['reply_to_message_id'] = reply_to_message_id
|
data['reply_to_message_id'] = reply_to_message_id
|
||||||
if reply_markup:
|
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)
|
json_data = self._requestUrl(url, 'POST', data=data)
|
||||||
data = self._parseAndCheckTelegram(json_data.content)
|
data = self._parseAndCheckTelegram(json_data.content)
|
||||||
|
|
||||||
return Message.newFromJsonDict(data)
|
return Message.de_json(data)
|
||||||
|
|
||||||
def forwardMessage(self,
|
def forwardMessage(self,
|
||||||
chat_id,
|
chat_id,
|
||||||
|
@ -149,7 +152,7 @@ class Bot(object):
|
||||||
json_data = self._requestUrl(url, 'POST', data=data)
|
json_data = self._requestUrl(url, 'POST', data=data)
|
||||||
data = self._parseAndCheckTelegram(json_data.content)
|
data = self._parseAndCheckTelegram(json_data.content)
|
||||||
|
|
||||||
return Message.newFromJsonDict(data)
|
return Message.de_json(data)
|
||||||
|
|
||||||
def sendPhoto(self,
|
def sendPhoto(self,
|
||||||
chat_id,
|
chat_id,
|
||||||
|
@ -198,7 +201,7 @@ class Bot(object):
|
||||||
json_data = self._requestUrl(url, 'POST', data=data)
|
json_data = self._requestUrl(url, 'POST', data=data)
|
||||||
data = self._parseAndCheckTelegram(json_data.content)
|
data = self._parseAndCheckTelegram(json_data.content)
|
||||||
|
|
||||||
return Message.newFromJsonDict(data)
|
return Message.de_json(data)
|
||||||
|
|
||||||
def sendAudio(self,
|
def sendAudio(self,
|
||||||
chat_id,
|
chat_id,
|
||||||
|
@ -244,7 +247,7 @@ class Bot(object):
|
||||||
json_data = self._requestUrl(url, 'POST', data=data)
|
json_data = self._requestUrl(url, 'POST', data=data)
|
||||||
data = self._parseAndCheckTelegram(json_data.content)
|
data = self._parseAndCheckTelegram(json_data.content)
|
||||||
|
|
||||||
return Message.newFromJsonDict(data)
|
return Message.de_json(data)
|
||||||
|
|
||||||
def sendDocument(self,
|
def sendDocument(self,
|
||||||
chat_id,
|
chat_id,
|
||||||
|
@ -287,7 +290,7 @@ class Bot(object):
|
||||||
json_data = self._requestUrl(url, 'POST', data=data)
|
json_data = self._requestUrl(url, 'POST', data=data)
|
||||||
data = self._parseAndCheckTelegram(json_data.content)
|
data = self._parseAndCheckTelegram(json_data.content)
|
||||||
|
|
||||||
return Message.newFromJsonDict(data)
|
return Message.de_json(data)
|
||||||
|
|
||||||
def sendSticker(self,
|
def sendSticker(self,
|
||||||
chat_id,
|
chat_id,
|
||||||
|
@ -330,7 +333,7 @@ class Bot(object):
|
||||||
json_data = self._requestUrl(url, 'POST', data=data)
|
json_data = self._requestUrl(url, 'POST', data=data)
|
||||||
data = self._parseAndCheckTelegram(json_data.content)
|
data = self._parseAndCheckTelegram(json_data.content)
|
||||||
|
|
||||||
return Message.newFromJsonDict(data)
|
return Message.de_json(data)
|
||||||
|
|
||||||
def sendVideo(self,
|
def sendVideo(self,
|
||||||
chat_id,
|
chat_id,
|
||||||
|
@ -374,7 +377,7 @@ class Bot(object):
|
||||||
json_data = self._requestUrl(url, 'POST', data=data)
|
json_data = self._requestUrl(url, 'POST', data=data)
|
||||||
data = self._parseAndCheckTelegram(json_data.content)
|
data = self._parseAndCheckTelegram(json_data.content)
|
||||||
|
|
||||||
return Message.newFromJsonDict(data)
|
return Message.de_json(data)
|
||||||
|
|
||||||
def sendLocation(self,
|
def sendLocation(self,
|
||||||
chat_id,
|
chat_id,
|
||||||
|
@ -419,7 +422,7 @@ class Bot(object):
|
||||||
json_data = self._requestUrl(url, 'POST', data=data)
|
json_data = self._requestUrl(url, 'POST', data=data)
|
||||||
data = self._parseAndCheckTelegram(json_data.content)
|
data = self._parseAndCheckTelegram(json_data.content)
|
||||||
|
|
||||||
return Message.newFromJsonDict(data)
|
return Message.de_json(data)
|
||||||
|
|
||||||
def sendChatAction(self,
|
def sendChatAction(self,
|
||||||
chat_id,
|
chat_id,
|
||||||
|
@ -488,7 +491,7 @@ class Bot(object):
|
||||||
json_data = self._requestUrl(url, 'POST', data=data)
|
json_data = self._requestUrl(url, 'POST', data=data)
|
||||||
data = self._parseAndCheckTelegram(json_data.content)
|
data = self._parseAndCheckTelegram(json_data.content)
|
||||||
|
|
||||||
return UserProfilePhotos.newFromJsonDict(data)
|
return UserProfilePhotos.de_json(data)
|
||||||
|
|
||||||
def getUpdates(self,
|
def getUpdates(self,
|
||||||
offset=None,
|
offset=None,
|
||||||
|
@ -530,7 +533,7 @@ class Bot(object):
|
||||||
json_data = self._requestUrl(url, 'POST', data=data)
|
json_data = self._requestUrl(url, 'POST', data=data)
|
||||||
data = self._parseAndCheckTelegram(json_data.content)
|
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):
|
def setWebhook(self):
|
||||||
url = '%s/setWebhook' % (self.base_url)
|
url = '%s/setWebhook' % (self.base_url)
|
||||||
|
|
|
@ -14,7 +14,7 @@ class Contact(object):
|
||||||
setattr(self, param, kwargs.get(param, default))
|
setattr(self, param, kwargs.get(param, default))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def newFromJsonDict(data):
|
def de_json(data):
|
||||||
return Contact(phone_number=data.get('phone_number', None),
|
return Contact(phone_number=data.get('phone_number', None),
|
||||||
first_name=data.get('first_name', None),
|
first_name=data.get('first_name', None),
|
||||||
last_name=data.get('last_name', None),
|
last_name=data.get('last_name', None),
|
||||||
|
|
|
@ -15,10 +15,10 @@ class Document(object):
|
||||||
setattr(self, param, kwargs.get(param, default))
|
setattr(self, param, kwargs.get(param, default))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def newFromJsonDict(data):
|
def de_json(data):
|
||||||
if 'thumb' in data:
|
if 'thumb' in data:
|
||||||
from telegram import PhotoSize
|
from telegram import PhotoSize
|
||||||
thumb = PhotoSize.newFromJsonDict(data['thumb'])
|
thumb = PhotoSize.de_json(data['thumb'])
|
||||||
else:
|
else:
|
||||||
thumb = None
|
thumb = None
|
||||||
|
|
||||||
|
|
|
@ -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)
|
|
@ -16,6 +16,6 @@ class GroupChat(object):
|
||||||
return self.id
|
return self.id
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def newFromJsonDict(data):
|
def de_json(data):
|
||||||
return GroupChat(id=data.get('id', None),
|
return GroupChat(id=data.get('id', None),
|
||||||
title=data.get('title', None))
|
title=data.get('title', None))
|
||||||
|
|
|
@ -12,6 +12,6 @@ class Location(object):
|
||||||
setattr(self, param, kwargs.get(param, default))
|
setattr(self, param, kwargs.get(param, default))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def newFromJsonDict(data):
|
def de_json(data):
|
||||||
return Location(longitude=data.get('longitude', None),
|
return Location(longitude=data.get('longitude', None),
|
||||||
latitude=data.get('latitude', None))
|
latitude=data.get('latitude', None))
|
||||||
|
|
|
@ -35,31 +35,31 @@ class Message(object):
|
||||||
return self.chat.id
|
return self.chat.id
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def newFromJsonDict(data):
|
def de_json(data):
|
||||||
if 'from' in data: # from on api
|
if 'from' in data: # from on api
|
||||||
from telegram import User
|
from telegram import User
|
||||||
user = User.newFromJsonDict(data['from'])
|
user = User.de_json(data['from'])
|
||||||
else:
|
else:
|
||||||
user = None
|
user = None
|
||||||
|
|
||||||
if 'chat' in data:
|
if 'chat' in data:
|
||||||
if 'username' in data['chat']:
|
if 'username' in data['chat']:
|
||||||
from telegram import User
|
from telegram import User
|
||||||
chat = User.newFromJsonDict(data['chat'])
|
chat = User.de_json(data['chat'])
|
||||||
if 'title' in data['chat']:
|
if 'title' in data['chat']:
|
||||||
from telegram import GroupChat
|
from telegram import GroupChat
|
||||||
chat = GroupChat.newFromJsonDict(data['chat'])
|
chat = GroupChat.de_json(data['chat'])
|
||||||
else:
|
else:
|
||||||
chat = None
|
chat = None
|
||||||
|
|
||||||
if 'forward_from' in data:
|
if 'forward_from' in data:
|
||||||
from telegram import User
|
from telegram import User
|
||||||
forward_from = User.newFromJsonDict(data['forward_from'])
|
forward_from = User.de_json(data['forward_from'])
|
||||||
else:
|
else:
|
||||||
forward_from = None
|
forward_from = None
|
||||||
|
|
||||||
if 'reply_to_message' in data:
|
if 'reply_to_message' in data:
|
||||||
reply_to_message = Message.newFromJsonDict(
|
reply_to_message = Message.de_json(
|
||||||
data['reply_to_message']
|
data['reply_to_message']
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
@ -67,49 +67,49 @@ class Message(object):
|
||||||
|
|
||||||
if 'audio' in data:
|
if 'audio' in data:
|
||||||
from telegram import Audio
|
from telegram import Audio
|
||||||
audio = Audio.newFromJsonDict(data['audio'])
|
audio = Audio.de_json(data['audio'])
|
||||||
else:
|
else:
|
||||||
audio = None
|
audio = None
|
||||||
|
|
||||||
if 'document' in data:
|
if 'document' in data:
|
||||||
from telegram import Document
|
from telegram import Document
|
||||||
document = Document.newFromJsonDict(data['document'])
|
document = Document.de_json(data['document'])
|
||||||
else:
|
else:
|
||||||
document = None
|
document = None
|
||||||
|
|
||||||
if 'photo' in data:
|
if 'photo' in data:
|
||||||
from telegram import PhotoSize
|
from telegram import PhotoSize
|
||||||
photo = [PhotoSize.newFromJsonDict(x) for x in data['photo']]
|
photo = [PhotoSize.de_json(x) for x in data['photo']]
|
||||||
else:
|
else:
|
||||||
photo = None
|
photo = None
|
||||||
|
|
||||||
if 'sticker' in data:
|
if 'sticker' in data:
|
||||||
from telegram import Sticker
|
from telegram import Sticker
|
||||||
sticker = Sticker.newFromJsonDict(data['sticker'])
|
sticker = Sticker.de_json(data['sticker'])
|
||||||
else:
|
else:
|
||||||
sticker = None
|
sticker = None
|
||||||
|
|
||||||
if 'video' in data:
|
if 'video' in data:
|
||||||
from telegram import Video
|
from telegram import Video
|
||||||
video = Video.newFromJsonDict(data['video'])
|
video = Video.de_json(data['video'])
|
||||||
else:
|
else:
|
||||||
video = None
|
video = None
|
||||||
|
|
||||||
if 'contact' in data:
|
if 'contact' in data:
|
||||||
from telegram import Contact
|
from telegram import Contact
|
||||||
contact = Contact.newFromJsonDict(data['contact'])
|
contact = Contact.de_json(data['contact'])
|
||||||
else:
|
else:
|
||||||
contact = None
|
contact = None
|
||||||
|
|
||||||
if 'location' in data:
|
if 'location' in data:
|
||||||
from telegram import Location
|
from telegram import Location
|
||||||
location = Location.newFromJsonDict(data['location'])
|
location = Location.de_json(data['location'])
|
||||||
else:
|
else:
|
||||||
location = None
|
location = None
|
||||||
|
|
||||||
if 'new_chat_participant' in data:
|
if 'new_chat_participant' in data:
|
||||||
from telegram import User
|
from telegram import User
|
||||||
new_chat_participant = User.newFromJsonDict(
|
new_chat_participant = User.de_json(
|
||||||
data['new_chat_participant']
|
data['new_chat_participant']
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
@ -117,7 +117,7 @@ class Message(object):
|
||||||
|
|
||||||
if 'left_chat_participant' in data:
|
if 'left_chat_participant' in data:
|
||||||
from telegram import User
|
from telegram import User
|
||||||
left_chat_participant = User.newFromJsonDict(
|
left_chat_participant = User.de_json(
|
||||||
data['left_chat_participant']
|
data['left_chat_participant']
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -14,7 +14,7 @@ class PhotoSize(object):
|
||||||
setattr(self, param, kwargs.get(param, default))
|
setattr(self, param, kwargs.get(param, default))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def newFromJsonDict(data):
|
def de_json(data):
|
||||||
return PhotoSize(file_id=data.get('file_id', None),
|
return PhotoSize(file_id=data.get('file_id', None),
|
||||||
width=data.get('width', None),
|
width=data.get('width', None),
|
||||||
height=data.get('height', None),
|
height=data.get('height', None),
|
||||||
|
|
6
telegram/replymarkup.py
Normal file
6
telegram/replymarkup.py
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
|
||||||
|
class ReplyMarkup(object):
|
||||||
|
def to_json(self):
|
||||||
|
raise NotImplementedError
|
|
@ -15,10 +15,10 @@ class Sticker(object):
|
||||||
setattr(self, param, kwargs.get(param, default))
|
setattr(self, param, kwargs.get(param, default))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def newFromJsonDict(data):
|
def de_json(data):
|
||||||
if 'thumb' in data:
|
if 'thumb' in data:
|
||||||
from telegram import PhotoSize
|
from telegram import PhotoSize
|
||||||
thumb = PhotoSize.newFromJsonDict(data['thumb'])
|
thumb = PhotoSize.de_json(data['thumb'])
|
||||||
else:
|
else:
|
||||||
thumb = None
|
thumb = None
|
||||||
|
|
||||||
|
|
|
@ -12,10 +12,10 @@ class Update(object):
|
||||||
setattr(self, param, kwargs.get(param, default))
|
setattr(self, param, kwargs.get(param, default))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def newFromJsonDict(data):
|
def de_json(data):
|
||||||
if 'message' in data:
|
if 'message' in data:
|
||||||
from telegram import Message
|
from telegram import Message
|
||||||
message = Message.newFromJsonDict(data['message'])
|
message = Message.de_json(data['message'])
|
||||||
else:
|
else:
|
||||||
message = None
|
message = None
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ class User(object):
|
||||||
setattr(self, param, kwargs.get(param, default))
|
setattr(self, param, kwargs.get(param, default))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def newFromJsonDict(data):
|
def de_json(data):
|
||||||
return User(id=data.get('id', None),
|
return User(id=data.get('id', None),
|
||||||
first_name=data.get('first_name', None),
|
first_name=data.get('first_name', None),
|
||||||
last_name=data.get('last_name', None),
|
last_name=data.get('last_name', None),
|
||||||
|
|
|
@ -12,12 +12,12 @@ class UserProfilePhotos(object):
|
||||||
setattr(self, param, kwargs.get(param, default))
|
setattr(self, param, kwargs.get(param, default))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def newFromJsonDict(data):
|
def de_json(data):
|
||||||
if 'photos' in data:
|
if 'photos' in data:
|
||||||
from telegram import PhotoSize
|
from telegram import PhotoSize
|
||||||
photos = []
|
photos = []
|
||||||
for photo in data['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:
|
else:
|
||||||
photos = None
|
photos = None
|
||||||
|
|
||||||
|
|
|
@ -18,10 +18,10 @@ class Video(object):
|
||||||
setattr(self, param, kwargs.get(param, default))
|
setattr(self, param, kwargs.get(param, default))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def newFromJsonDict(data):
|
def de_json(data):
|
||||||
if 'thumb' in data:
|
if 'thumb' in data:
|
||||||
from telegram import PhotoSize
|
from telegram import PhotoSize
|
||||||
thumb = PhotoSize.newFromJsonDict(data['thumb'])
|
thumb = PhotoSize.de_json(data['thumb'])
|
||||||
else:
|
else:
|
||||||
thumb = None
|
thumb = None
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ class BotTest(unittest.TestCase):
|
||||||
'''Test the telegram.Bot getUpdates method'''
|
'''Test the telegram.Bot getUpdates method'''
|
||||||
print 'Testing getUpdates'
|
print 'Testing getUpdates'
|
||||||
updates = self._bot.getUpdates()
|
updates = self._bot.getUpdates()
|
||||||
self.assertEqual(129566520, updates[0].update_id)
|
self.assertEqual(129566524, updates[0].update_id)
|
||||||
|
|
||||||
def testForwardMessage(self):
|
def testForwardMessage(self):
|
||||||
'''Test the telegram.Bot forwardMessage method'''
|
'''Test the telegram.Bot forwardMessage method'''
|
||||||
|
|
Loading…
Reference in a new issue