From 6e1cf24946a8683cf3d91d6534c54d8e5aa022d6 Mon Sep 17 00:00:00 2001 From: Badiboy Date: Sat, 2 May 2020 13:09:52 +0300 Subject: [PATCH] send_poll revised to standart signature --- README.md | 3 ++- telebot/__init__.py | 35 ++++++++++++++++++++++++++++++----- telebot/apihelper.py | 43 ++++++++++++++++++++++++------------------- telebot/util.py | 20 +++++++++++++++++++- 4 files changed, 75 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 042e686..6fd8c53 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,8 @@

A simple, but extensible Python implementation for the Telegram Bot API. -[![Download Month](https://img.shields.io/pypi/v/pyTelegramBotAPI.svg)](https://pypi.python.org/pypi/pyTelegramBotAPI) +[![PyPi Package Version](https://img.shields.io/pypi/v/pyTelegramBotAPI.svg)](https://pypi.python.org/pypi/pyTelegramBotAPI) +[![Supported Python versions](https://img.shields.io/pypi/pyversions/pyTelegramBotAPI.svg)](https://pypi.python.org/pypi/pyTelegramBotAPI) [![Build Status](https://travis-ci.org/eternnoir/pyTelegramBotAPI.svg?branch=master)](https://travis-ci.org/eternnoir/pyTelegramBotAPI) * [Getting started.](#getting-started) diff --git a/telebot/__init__.py b/telebot/__init__.py index 33d459d..c1d7511 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -1207,17 +1207,42 @@ class TeleBot: disable_notification, reply_to_message_id, reply_markup, provider_data) return types.Message.de_json(result) - def send_poll(self, chat_id, poll, explanation_parse_mode=None, disable_notifications=False, reply_to_message=None, reply_markup=None): + def send_poll( + self, chat_id, + question, options, + is_anonymous=None, type=None, allows_multiple_answers=None, correct_option_id=None, + explanation=None, explanation_parse_mode=None, open_period=None, close_date=None, is_closed=None, + disable_notifications=False, reply_to_message_id=None, reply_markup=None): """ - Sends poll + Send polls :param chat_id: - :param poll: + :param question: + :param options: + :param is_anonymous: + :param type: + :param allows_multiple_answers: + :param correct_option_id: + :param explanation: + :param explanation_parse_mode: + :param open_period: + :param close_date: + :param is_closed: :param disable_notifications: - :param reply_to_message: + :param reply_to_message_id: :param reply_markup: :return: """ - return types.Message.de_json(apihelper.send_poll(self.token, chat_id, poll, explanation_parse_mode, disable_notifications, reply_to_message, reply_markup)) + + if isinstance(question, types.Poll): + raise Exception("The send_poll signature was changed, please see send_poll function details.") + + return types.Message.de_json( + apihelper.send_poll( + self.token, chat_id, + question, options, + is_anonymous, type, allows_multiple_answers, correct_option_id, + explanation, explanation_parse_mode, open_period, close_date, is_closed, + disable_notifications, reply_to_message_id, reply_markup)) def stop_poll(self, chat_id, message_id): """ diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 8bda798..e59515a 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -998,31 +998,36 @@ def delete_sticker_from_set(token, sticker): return _make_request(token, method_url, params=payload, method='post') -def send_poll(token, chat_id, poll, explanation_parse_mode=None, disable_notifications=False, reply_to_message_id=None, reply_markup=None): +def send_poll( + token, chat_id, + question, options, + is_anonymous = None, type = None, allows_multiple_answers = None, correct_option_id = None, + explanation = None, explanation_parse_mode=None, open_period = None, close_date = None, is_closed = None, + disable_notifications=False, reply_to_message_id=None, reply_markup=None): method_url = r'sendPoll' payload = { 'chat_id': str(chat_id), - 'question': poll.question, - 'options': _convert_list_json_serializable(poll.options)} + 'question': question, + 'options': _convert_list_json_serializable(options)} - if poll.is_anonymous is not None: - payload['is_anonymous'] = poll.is_anonymous - if poll.type is not None: - payload['type'] = poll.type - if poll.allows_multiple_answers is not None: - payload['allows_multiple_answers'] = poll.allows_multiple_answers - if poll.correct_option_id is not None: - payload['correct_option_id'] = poll.correct_option_id - if poll.explanation is not None: - payload['explanation'] = poll.explanation + if is_anonymous is not None: + payload['is_anonymous'] = is_anonymous + if type is not None: + payload['type'] = type + if allows_multiple_answers is not None: + payload['allows_multiple_answers'] = allows_multiple_answers + if correct_option_id is not None: + payload['correct_option_id'] = correct_option_id + if explanation is not None: + payload['explanation'] = explanation if explanation_parse_mode is not None: payload['explanation_parse_mode'] = explanation_parse_mode - if poll.open_period is not None: - payload['open_period'] = poll.open_period - if poll.close_date is not None: - payload['close_date'] = poll.close_date - if poll.is_closed is not None: - payload['is_closed'] = poll.is_closed + if open_period is not None: + payload['open_period'] = open_period + if close_date is not None: + payload['close_date'] = close_date + if is_closed is not None: + payload['is_closed'] = is_closed if disable_notifications: payload['disable_notification'] = disable_notifications diff --git a/telebot/util.py b/telebot/util.py index 31d2d35..9e97ad3 100644 --- a/telebot/util.py +++ b/telebot/util.py @@ -5,6 +5,8 @@ import string import sys import threading import traceback +import warnings +import functools import six from six import string_types @@ -258,4 +260,20 @@ def per_thread(key, construct_value, reset=False): def generate_random_token(): - return ''.join(random.sample(string.ascii_letters, 16)) \ No newline at end of file + return ''.join(random.sample(string.ascii_letters, 16)) + + +def deprecated(func): + """This is a decorator which can be used to mark functions + as deprecated. It will result in a warning being emitted + when the function is used.""" + # https://stackoverflow.com/a/30253848/441814 + @functools.wraps(func) + def new_func(*args, **kwargs): + warnings.simplefilter('always', DeprecationWarning) # turn off filter + warnings.warn("Call to deprecated function {}.".format(func.__name__), + category=DeprecationWarning, + stacklevel=2) + warnings.simplefilter('default', DeprecationWarning) # reset filter + return func(*args, **kwargs) + return new_func