Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
41 changes: 34 additions & 7 deletions telegram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1926,7 +1926,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 +1945,7 @@ def answer_shipping_query(self,
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 +1954,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:
if ok is True:
data['shipping_options'] = shipping_options
if error_message:
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 +1994,7 @@ def answer_pre_checkout_query(self, pre_checkout_query_id, ok, error_message=Non
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 +2003,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 (ok is not True and error_message is None) or (ok is True and error_message is not 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:
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
7 changes: 7 additions & 0 deletions telegram/ext/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ def filter(self, message):

game = _Game()

class _SuccessfulPayment(BaseFilter):

def filter(self, message):
return bool(message.successful_payment)

successful_payment = _SuccessfulPayment()

class entity(BaseFilter):
"""Filters messages to only allow those which have a :class:`telegram.MessageEntity`
where their `type` matches `entity_type`.
Expand Down
6 changes: 5 additions & 1 deletion telegram/precheckoutquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ def __init__(self,
total_amount,
invoice_payload,
shipping_option_id=None,
order_info=None):
order_info=None,
bot=None,
**kwargs):
self.id = id
self.from_user = from_user
self.currency = currency
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
4 changes: 3 additions & 1 deletion telegram/shippingquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ class ShippingQuery(TelegramObject):

"""

def __init__(self, id, from_user, invoice_payload, shipping_address):
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
6 changes: 6 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ def test_filters_game(self):
self.message.game = None
self.assertFalse(Filters.game(self.message))

def test_filters_successful_payment(self):
self.message.successful_payment = 'test'
self.assertTrue(Filters.successful_payment(self.message))
self.message.successful_payment = None
self.assertFalse(Filters.successful_payment(self.message))

def test_filters_status_update(self):
self.assertFalse(Filters.status_update(self.message))

Expand Down