mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
add PollAnswer, poll_answer_handler; make User Serializable and Dictionaryble; some pep fixes
This commit is contained in:
parent
aacc494a55
commit
75a018e18b
@ -119,6 +119,7 @@ class TeleBot:
|
|||||||
self.shipping_query_handlers = []
|
self.shipping_query_handlers = []
|
||||||
self.pre_checkout_query_handlers = []
|
self.pre_checkout_query_handlers = []
|
||||||
self.poll_handlers = []
|
self.poll_handlers = []
|
||||||
|
self.poll_answer_handlers = []
|
||||||
|
|
||||||
self.typed_middleware_handlers = {
|
self.typed_middleware_handlers = {
|
||||||
'message': [],
|
'message': [],
|
||||||
@ -293,6 +294,7 @@ class TeleBot:
|
|||||||
new_shipping_querys = []
|
new_shipping_querys = []
|
||||||
new_pre_checkout_querys = []
|
new_pre_checkout_querys = []
|
||||||
new_polls = []
|
new_polls = []
|
||||||
|
new_poll_answers = []
|
||||||
|
|
||||||
for update in updates:
|
for update in updates:
|
||||||
if apihelper.ENABLE_MIDDLEWARE:
|
if apihelper.ENABLE_MIDDLEWARE:
|
||||||
@ -320,6 +322,8 @@ class TeleBot:
|
|||||||
new_pre_checkout_querys.append(update.pre_checkout_query)
|
new_pre_checkout_querys.append(update.pre_checkout_query)
|
||||||
if update.poll:
|
if update.poll:
|
||||||
new_polls.append(update.poll)
|
new_polls.append(update.poll)
|
||||||
|
if update.poll_answer:
|
||||||
|
new_poll_answers.append(update.poll_answer)
|
||||||
|
|
||||||
logger.debug('Received {0} new updates'.format(len(updates)))
|
logger.debug('Received {0} new updates'.format(len(updates)))
|
||||||
if len(new_messages) > 0:
|
if len(new_messages) > 0:
|
||||||
@ -342,6 +346,8 @@ class TeleBot:
|
|||||||
self.process_new_pre_checkout_query(new_pre_checkout_querys)
|
self.process_new_pre_checkout_query(new_pre_checkout_querys)
|
||||||
if len(new_polls) > 0:
|
if len(new_polls) > 0:
|
||||||
self.process_new_poll(new_polls)
|
self.process_new_poll(new_polls)
|
||||||
|
if len(new_poll_answers) > 0:
|
||||||
|
self.process_new_poll_answer(new_poll_answers)
|
||||||
|
|
||||||
def process_new_messages(self, new_messages):
|
def process_new_messages(self, new_messages):
|
||||||
self._notify_next_handlers(new_messages)
|
self._notify_next_handlers(new_messages)
|
||||||
@ -376,6 +382,9 @@ class TeleBot:
|
|||||||
def process_new_poll(self, polls):
|
def process_new_poll(self, polls):
|
||||||
self._notify_command_handlers(self.poll_handlers, polls)
|
self._notify_command_handlers(self.poll_handlers, polls)
|
||||||
|
|
||||||
|
def process_new_poll_answer(self, poll_answers):
|
||||||
|
self._notify_command_handlers(self.poll_answer_handlers, poll_answers)
|
||||||
|
|
||||||
def process_middlewares(self, update):
|
def process_middlewares(self, update):
|
||||||
for update_type, middlewares in self.typed_middleware_handlers.items():
|
for update_type, middlewares in self.typed_middleware_handlers.items():
|
||||||
if getattr(update, update_type) is not None:
|
if getattr(update, update_type) is not None:
|
||||||
@ -1561,7 +1570,7 @@ class TeleBot:
|
|||||||
"""
|
"""
|
||||||
return {
|
return {
|
||||||
'function': handler,
|
'function': handler,
|
||||||
'filters' : filters
|
'filters': filters
|
||||||
}
|
}
|
||||||
|
|
||||||
def middleware_handler(self, update_types=None):
|
def middleware_handler(self, update_types=None):
|
||||||
@ -1900,6 +1909,28 @@ class TeleBot:
|
|||||||
"""
|
"""
|
||||||
self.poll_handlers.append(handler_dict)
|
self.poll_handlers.append(handler_dict)
|
||||||
|
|
||||||
|
def poll_answer_handler(self, func=None, **kwargs):
|
||||||
|
"""
|
||||||
|
Poll_answer request handler
|
||||||
|
:param func:
|
||||||
|
:param kwargs:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
def decorator(handler):
|
||||||
|
handler_dict = self._build_handler_dict(handler, func=func, **kwargs)
|
||||||
|
self.add_poll_answer_handler(handler_dict)
|
||||||
|
return handler
|
||||||
|
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
def add_poll_answer_handler(self, handler_dict):
|
||||||
|
"""
|
||||||
|
Adds a poll_answer request handler
|
||||||
|
:param handler_dict:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
self.poll_answer_handlers.append(handler_dict)
|
||||||
|
|
||||||
def _test_message_handler(self, message_handler, message):
|
def _test_message_handler(self, message_handler, message):
|
||||||
"""
|
"""
|
||||||
Test message handler
|
Test message handler
|
||||||
|
@ -100,11 +100,12 @@ class Update(JsonDeserializable):
|
|||||||
shipping_query = ShippingQuery.de_json(obj.get('shipping_query'))
|
shipping_query = ShippingQuery.de_json(obj.get('shipping_query'))
|
||||||
pre_checkout_query = PreCheckoutQuery.de_json(obj.get('pre_checkout_query'))
|
pre_checkout_query = PreCheckoutQuery.de_json(obj.get('pre_checkout_query'))
|
||||||
poll = Poll.de_json(obj.get('poll'))
|
poll = Poll.de_json(obj.get('poll'))
|
||||||
|
poll_answer = PollAnswer.de_json(obj.get('poll_answer'))
|
||||||
return cls(update_id, message, edited_message, channel_post, edited_channel_post, inline_query,
|
return cls(update_id, message, edited_message, channel_post, edited_channel_post, inline_query,
|
||||||
chosen_inline_result, callback_query, shipping_query, pre_checkout_query, poll)
|
chosen_inline_result, callback_query, shipping_query, pre_checkout_query, poll, poll_answer)
|
||||||
|
|
||||||
def __init__(self, update_id, message, edited_message, channel_post, edited_channel_post, inline_query,
|
def __init__(self, update_id, message, edited_message, channel_post, edited_channel_post, inline_query,
|
||||||
chosen_inline_result, callback_query, shipping_query, pre_checkout_query, poll):
|
chosen_inline_result, callback_query, shipping_query, pre_checkout_query, poll, poll_answer):
|
||||||
self.update_id = update_id
|
self.update_id = update_id
|
||||||
self.message = message
|
self.message = message
|
||||||
self.edited_message = edited_message
|
self.edited_message = edited_message
|
||||||
@ -116,6 +117,7 @@ class Update(JsonDeserializable):
|
|||||||
self.shipping_query = shipping_query
|
self.shipping_query = shipping_query
|
||||||
self.pre_checkout_query = pre_checkout_query
|
self.pre_checkout_query = pre_checkout_query
|
||||||
self.poll = poll
|
self.poll = poll
|
||||||
|
self.poll_answer = poll_answer
|
||||||
|
|
||||||
|
|
||||||
class WebhookInfo(JsonDeserializable):
|
class WebhookInfo(JsonDeserializable):
|
||||||
@ -144,7 +146,7 @@ class WebhookInfo(JsonDeserializable):
|
|||||||
self.allowed_updates = allowed_updates
|
self.allowed_updates = allowed_updates
|
||||||
|
|
||||||
|
|
||||||
class User(JsonDeserializable):
|
class User(JsonDeserializable, Dictionaryable, JsonSerializable):
|
||||||
@classmethod
|
@classmethod
|
||||||
def de_json(cls, json_string):
|
def de_json(cls, json_string):
|
||||||
if (json_string is None): return None
|
if (json_string is None): return None
|
||||||
@ -165,6 +167,17 @@ class User(JsonDeserializable):
|
|||||||
self.last_name = last_name
|
self.last_name = last_name
|
||||||
self.language_code = language_code
|
self.language_code = language_code
|
||||||
|
|
||||||
|
def to_json(self):
|
||||||
|
return json.dumps(self.to_dict())
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
return {'id': self.id,
|
||||||
|
'is_bot': self.is_bot,
|
||||||
|
'first_name': self.first_name,
|
||||||
|
'last_name': self.last_name,
|
||||||
|
'username': self.username,
|
||||||
|
'language_code': self.language_code}
|
||||||
|
|
||||||
|
|
||||||
class GroupChat(JsonDeserializable):
|
class GroupChat(JsonDeserializable):
|
||||||
@classmethod
|
@classmethod
|
||||||
@ -2411,3 +2424,27 @@ class Poll(JsonDeserializable):
|
|||||||
self.options.append(option)
|
self.options.append(option)
|
||||||
else:
|
else:
|
||||||
self.options.append(PollOption(option))
|
self.options.append(PollOption(option))
|
||||||
|
|
||||||
|
|
||||||
|
class PollAnswer(JsonSerializable, JsonDeserializable, Dictionaryable):
|
||||||
|
@classmethod
|
||||||
|
def de_json(cls, json_string):
|
||||||
|
if (json_string is None): return None
|
||||||
|
obj = cls.check_json(json_string)
|
||||||
|
poll_id = obj['poll_id']
|
||||||
|
user = User.de_json(obj['user'])
|
||||||
|
options_ids = obj['option_ids']
|
||||||
|
return cls(poll_id, user, options_ids)
|
||||||
|
|
||||||
|
def __init__(self, poll_id, user, options_ids):
|
||||||
|
self.poll_id = poll_id
|
||||||
|
self.user = user
|
||||||
|
self.options_ids = options_ids
|
||||||
|
|
||||||
|
def to_json(self):
|
||||||
|
return json.dumps(self.to_dict())
|
||||||
|
|
||||||
|
def to_dict(self):
|
||||||
|
return {'poll_id': self.poll_id,
|
||||||
|
'user': self.user.to_dict(),
|
||||||
|
'options_ids': self.options_ids}
|
||||||
|
@ -178,4 +178,13 @@ def test_json_poll_1():
|
|||||||
assert msg.poll.question is not None
|
assert msg.poll.question is not None
|
||||||
assert msg.poll.options is not None
|
assert msg.poll.options is not None
|
||||||
assert len(msg.poll.options) == 2
|
assert len(msg.poll.options) == 2
|
||||||
assert msg.poll.allows_multiple_answers == True
|
assert msg.poll.allows_multiple_answers is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_json_poll_answer():
|
||||||
|
jsonstring = r'{"poll_id": "5895675970559410186", "user": {"id": 329343347, "is_bot": false, "first_name": "Test", "username": "test_user", "last_name": "User", "language_code": "en"}, "option_ids": [1]}'
|
||||||
|
__import__('pprint').pprint(__import__('json').loads(jsonstring))
|
||||||
|
poll_answer = types.PollAnswer.de_json(jsonstring)
|
||||||
|
assert poll_answer.poll_id == '5895675970559410186'
|
||||||
|
assert isinstance(poll_answer.user, types.User)
|
||||||
|
assert poll_answer.options_ids == [1]
|
||||||
|
Loading…
Reference in New Issue
Block a user