Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d220ff4
Merge branch 'payment' into beta
jsmnbom May 21, 2017
48fa3d9
Merge branch 'videonote' into beta
jsmnbom May 21, 2017
acda19b
Merge branch 'may18minor' into beta
jsmnbom May 21, 2017
01430a2
Import order fix
jsmnbom May 21, 2017
2c05b03
add handlers for new payment API
jeffffc May 22, 2017
ee057e7
fix typo
jeffffc May 22, 2017
cc03649
fix docstring mistakes
jeffffc May 22, 2017
a78f72c
added missing 'from_user'
jeffffc May 22, 2017
3767d26
Add both handlers for queries from new Payment API (#630)
jeffffc May 22, 2017
8f2f29c
fix typo, add bot and kwargs to class init
jeffffc May 22, 2017
1e250f2
add kwargs to answer* methods
jeffffc May 22, 2017
1142953
add checks for answer* methods
jeffffc May 22, 2017
43f4128
fix answer* methods not sending api requests
jeffffc May 22, 2017
27238c5
add successful_payment filter under message
jeffffc May 22, 2017
9617111
fix crucial typo in filters
jeffffc May 22, 2017
73ac259
fix typo...
jeffffc May 22, 2017
18822d1
Merge branch 'fix-paymenthandlers' into paymenthandlers-final
jeffffc May 22, 2017
05ed693
fix authors typo
jeffffc May 22, 2017
1210e4e
Finalize Payment API
jeffffc May 22, 2017
1bf0078
add missing "need_email" in sendInvoice, fix pep8/flake
jeffffc May 23, 2017
f314915
fix typo again
jeffffc May 23, 2017
e9d08c6
Merge branch 'master' into paymenthandlers-new
jeffffc Jun 2, 2017
f735a37
Merge from master and resolve conflicts
jeffffc Jun 2, 2017
eaf765d
Remove duplicated/useless codes, added Filters.invoice test
jeffffc Jun 2, 2017
b6ba66b
Fix typo in filter test
jeffffc Jun 2, 2017
4247dc0
tiny changes upon PR review
jeffffc Jun 8, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The following wonderful people contributed directly or indirectly to this projec
- `Hugo Damer <https://github.com/HakimusGIT>`_
- `Jacob Bom <https://github.com/bomjacob>`_
- `JASON0916 <https://github.com/JASON0916>`_
- `jeffffc <https://github.com/jeffffc>`_
- `jh0ker <https://github.com/jh0ker>`_
- `John Yong <https://github.com/whipermr5>`_
- `jossalgon <https://github.com/jossalgon>`_
Expand Down
46 changes: 39 additions & 7 deletions telegram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,7 @@ def send_invoice(self,
photo_height=None,
need_name=None,
need_phone_number=None,
need_email=None,
need_shipping_address=None,
is_flexible=None,
disable_notification=False,
Expand Down Expand Up @@ -1867,6 +1868,8 @@ def send_invoice(self,
the order
need_phone_number (Optional[bool]): Pass True, if you require the user's phone number
to complete the order
need_email (Optional[bool]): Pass True, if you require the user's email to
complete the order
need_shipping_address (Optional[bool]): Pass True, if you require the user's shipping
address to complete the order
is_flexible (Optional[bool]): Pass True, if the final price depends on the shipping
Expand Down Expand Up @@ -1915,6 +1918,8 @@ def send_invoice(self,
data['need_name'] = need_name
if need_phone_number is not None:
data['need_phone_number'] = need_phone_number
if need_email is not None:
data['need_email'] = need_email
if need_shipping_address is not None:
data['need_shipping_address'] = need_shipping_address
if is_flexible is not None:
Expand All @@ -1926,7 +1931,9 @@ def answer_shipping_query(self,
shipping_query_id,
ok,
shipping_options=None,
error_message=None):
error_message=None,
timeout=None,
**kwargs):
"""
If you sent an invoice requesting a shipping address and the parameter is_flexible was
specified, the Bot API will send an Update with a shipping_query field to the bot. Use
Expand All @@ -1943,6 +1950,7 @@ def answer_shipping_query(self,
form that explains why it is impossible to complete the order (e.g. "Sorry,
delivery to your desired address is unavailable'). Telegram will display this
message to the user.
**kwargs (dict): Arbitrary keyword arguments.

Returns:
bool: On success, `True` is returned.
Expand All @@ -1951,18 +1959,32 @@ def answer_shipping_query(self,
:class:`telegram.TelegramError`

"""
url = '{0]/answerShippingQuery'.format(self.base_url)

if ok is True and (shipping_options is None or error_message is not None):
raise TelegramError(
'answerShippingQuery: If ok is True, shipping_options '
'should not be empty and there should not be error_message')

if ok is False and (shipping_options is not None or error_message is None):
raise TelegramError(
'answerShippingQuery: If ok is False, error_message '
'should not be empty and there should not be shipping_options')

url_ = '{0}/answerShippingQuery'.format(self.base_url)

data = {'shipping_query_id': shipping_query_id, 'ok': ok}

if shipping_options is not None:
if ok is True:
data['shipping_options'] = shipping_options
if error_message is not None:
data['error_message'] = error_message

return url, data
result = self._request.post(url_, data, timeout=timeout)

def answer_pre_checkout_query(self, pre_checkout_query_id, ok, error_message=None):
return result

def answer_pre_checkout_query(self, pre_checkout_query_id, ok,
error_message=None, timeout=None, **kwargs):
"""
If you sent an invoice requesting a shipping address and the parameter is_flexible was
specified, the Bot API will send an Update with a shipping_query field to the bot.
Expand All @@ -1977,6 +1999,7 @@ def answer_pre_checkout_query(self, pre_checkout_query_id, ok, error_message=Non
"Sorry, somebody just bought the last of our amazing black T-shirts while you were
busy filling out your payment details. Please choose a different color or
garment!"). Telegram will display this message to the user.
**kwargs (dict): Arbitrary keyword arguments.

Returns:
bool: On success, `True` is returned.
Expand All @@ -1985,14 +2008,23 @@ def answer_pre_checkout_query(self, pre_checkout_query_id, ok, error_message=Non
:class:`telegram.TelegramError`

"""
url = '{0]/answerPreCheckoutQuery'.format(self.base_url)

if not (ok ^ (error_message is None)):
raise TelegramError(
'answerPreCheckoutQuery: If ok is True, there should '
'not be error_message; if ok is False, error_message '
'should not be empty')

url_ = '{0}/answerPreCheckoutQuery'.format(self.base_url)

data = {'pre_checkout_query_id': pre_checkout_query_id, 'ok': ok}

if error_message is not None:
data['error_message'] = error_message

return url, data
result = self._request.post(url_, data, timeout=timeout)

return result

@staticmethod
def de_json(data, bot):
Expand Down
5 changes: 4 additions & 1 deletion telegram/ext/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@
from .stringregexhandler import StringRegexHandler
from .typehandler import TypeHandler
from .conversationhandler import ConversationHandler
from .precheckoutqueryhandler import PreCheckoutQueryHandler
from .shippingqueryhandler import ShippingQueryHandler

__all__ = ('Dispatcher', 'JobQueue', 'Job', 'Updater', 'CallbackQueryHandler',
'ChosenInlineResultHandler', 'CommandHandler', 'Handler', 'InlineQueryHandler',
'MessageHandler', 'BaseFilter', 'Filters', 'RegexHandler', 'StringCommandHandler',
'StringRegexHandler', 'TypeHandler', 'ConversationHandler')
'StringRegexHandler', 'TypeHandler', 'ConversationHandler',
'PreCheckoutQueryHandler', 'ShippingQueryHandler')
69 changes: 69 additions & 0 deletions telegram/ext/precheckoutqueryhandler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2017
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
""" This module contains the PreCheckoutQueryHandler class """

from telegram import Update
from .handler import Handler


class PreCheckoutQueryHandler(Handler):
"""
Handler class to handle Telegram PreCheckout callback queries.

Args:
callback (function): A function that takes ``bot, update`` as
positional arguments. It will be called when the ``check_update``
has determined that an update should be processed by this handler.
pass_update_queue (optional[bool]): If set to ``True``, a keyword argument called
``update_queue`` will be passed to the callback function. It will be the ``Queue``
instance used by the ``Updater`` and ``Dispatcher`` that contains new updates which can
be used to insert updates. Default is ``False``.
pass_job_queue (optional[bool]): If set to ``True``, a keyword argument called
``job_queue`` will be passed to the callback function. It will be a ``JobQueue``
instance created by the ``Updater`` which can be used to schedule new jobs.
Default is ``False``.
pass_user_data (optional[bool]): If set to ``True``, a keyword argument called
``user_data`` will be passed to the callback function. It will be a ``dict`` you
can use to keep any data related to the user that sent the update. For each update of
the same user, it will be the same ``dict``. Default is ``False``.
pass_chat_data (optional[bool]): If set to ``True``, a keyword argument called
``chat_data`` will be passed to the callback function. It will be a ``dict`` you
can use to keep any data related to the chat that the update was sent in.
For each update in the same chat, it will be the same ``dict``. Default is ``False``.
"""

def __init__(self,
callback,
pass_update_queue=False,
pass_job_queue=False,
pass_user_data=False,
pass_chat_data=False):
super(PreCheckoutQueryHandler, self).__init__(
callback,
pass_update_queue=pass_update_queue,
pass_job_queue=pass_job_queue,
pass_user_data=pass_user_data,
pass_chat_data=pass_chat_data)

def check_update(self, update):
return isinstance(update, Update) and update.pre_checkout_query

def handle_update(self, update, dispatcher):
optional_args = self.collect_optional_args(dispatcher, update)
return self.callback(dispatcher.bot, update, **optional_args)
69 changes: 69 additions & 0 deletions telegram/ext/shippingqueryhandler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2017
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
""" This module contains the ShippingQueryHandler class """

from telegram import Update
from .handler import Handler


class ShippingQueryHandler(Handler):
"""
Handler class to handle Telegram shipping callback queries.

Args:
callback (function): A function that takes ``bot, update`` as
positional arguments. It will be called when the ``check_update``
has determined that an update should be processed by this handler.
pass_update_queue (optional[bool]): If set to ``True``, a keyword argument called
``update_queue`` will be passed to the callback function. It will be the ``Queue``
instance used by the ``Updater`` and ``Dispatcher`` that contains new updates which can
be used to insert updates. Default is ``False``.
pass_job_queue (optional[bool]): If set to ``True``, a keyword argument called
``job_queue`` will be passed to the callback function. It will be a ``JobQueue``
instance created by the ``Updater`` which can be used to schedule new jobs.
Default is ``False``.
pass_user_data (optional[bool]): If set to ``True``, a keyword argument called
``user_data`` will be passed to the callback function. It will be a ``dict`` you
can use to keep any data related to the user that sent the update. For each update of
the same user, it will be the same ``dict``. Default is ``False``.
pass_chat_data (optional[bool]): If set to ``True``, a keyword argument called
``chat_data`` will be passed to the callback function. It will be a ``dict`` you
can use to keep any data related to the chat that the update was sent in.
For each update in the same chat, it will be the same ``dict``. Default is ``False``.
"""

def __init__(self,
callback,
pass_update_queue=False,
pass_job_queue=False,
pass_user_data=False,
pass_chat_data=False):
super(ShippingQueryHandler, self).__init__(
callback,
pass_update_queue=pass_update_queue,
pass_job_queue=pass_job_queue,
pass_user_data=pass_user_data,
pass_chat_data=pass_chat_data)

def check_update(self, update):
return isinstance(update, Update) and update.shipping_query

def handle_update(self, update, dispatcher):
optional_args = self.collect_optional_args(dispatcher, update)
return self.callback(dispatcher.bot, update, **optional_args)
10 changes: 10 additions & 0 deletions telegram/precheckoutquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class PreCheckoutQuery(TelegramObject):
invoice_payload (str): Bot specified invoice payload
shipping_option_id (Optional[str]): Identifier of the shipping option chosen by the user
order_info (Optional[:class:`telegram.OrderInfo`]): Order info provided by the user
bot (Optional[Bot]): The Bot to use for instance methods
**kwargs (dict): Arbitrary keyword arguments.

"""
Expand All @@ -47,6 +48,7 @@ def __init__(self,
invoice_payload,
shipping_option_id=None,
order_info=None,
bot=None,
**kwargs):
self.id = id
self.from_user = from_user
Expand All @@ -56,6 +58,8 @@ def __init__(self,
self.shipping_option_id = shipping_option_id
self.order_info = order_info

self.bot = bot

self._id_attrs = (self.id,)

@staticmethod
Expand Down Expand Up @@ -88,3 +92,9 @@ def to_dict(self):
data['from'] = data.pop('from_user', None)

return data

def answer(self, *args, **kwargs):
"""
Shortcut for ``bot.answerPreCheckoutQuery(update.pre_checkout_query.id, *args, **kwargs)``
"""
return self.bot.answerPreCheckoutQuery(self.id, *args, **kwargs)
9 changes: 8 additions & 1 deletion telegram/shippingquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,19 @@ class ShippingQuery(TelegramObject):
from_user (:class:`telegram.User`): User who sent the query
invoice_payload (str): Bot specified invoice payload
shipping_address (:class:`telegram.ShippingQuery`): User specified shipping address
bot (Optional[Bot]): The Bot to use for instance methods
**kwargs (dict): Arbitrary keyword arguments.

"""

def __init__(self, id, from_user, invoice_payload, shipping_address, **kwargs):
def __init__(self, id, from_user, invoice_payload, shipping_address, bot=None, **kwargs):
self.id = id
self.from_user = from_user
self.invoice_payload = invoice_payload
self.shipping_address = shipping_address

self.bot = bot

self._id_attrs = (self.id,)

@staticmethod
Expand Down Expand Up @@ -74,3 +77,7 @@ def to_dict(self):
data['from'] = data.pop('from_user', None)

return data

def answer(self, *args, **kwargs):
"""Shortcut for ``bot.answerShippingQuery(update.shipping_query.id, *args, **kwargs)``"""
return self.bot.answerShippingQuery(self.id, *args, **kwargs)
Loading