Skip to content

Commit 845312d

Browse files
committed
Merge branch 'master' of github.com:python-telegram-bot/python-telegram-bot
2 parents 179fc14 + 3863b4f commit 845312d

File tree

12 files changed

+212
-63
lines changed

12 files changed

+212
-63
lines changed

.github/CONTRIBUTING.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Setting things up
1212

1313
.. code-block:: bash
1414
15-
$ git clone https://github.com/<your username>/python-telegram-bot
15+
$ git clone https://github.com/<your username>/python-telegram-bot --recursive
1616
$ cd python-telegram-bot
1717
1818
3. Add a track to the original repository:

AUTHORS.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Contributors
1515

1616
The following wonderful people contributed directly or indirectly to this project:
1717

18+
- `Alateas <https://github.com/alateas>`_
1819
- `Avanatiker <https://github.com/Avanatiker>`_
1920
- `Anton Tagunov <https://github.com/anton-tagunov>`_
2021
- `Balduro <https://github.com/Balduro>`_
@@ -50,6 +51,7 @@ The following wonderful people contributed directly or indirectly to this projec
5051
- `Pieter Schutz <https://github.com/eldinnie>`_
5152
- `Rahiel Kasim <https://github.com/rahiel>`_
5253
- `Joscha Götzer <https://github.com/Rostgnom>`_
54+
- `Sascha <https://github.com/saschalalala>`_
5355
- `Shelomentsev D <https://github.com/shelomentsevd>`_
5456
- `sooyhwang <https://github.com/sooyhwang>`_
5557
- `thodnev <https://github.com/thodnev>`_

examples/paymentbot.py

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Basic example for a bot that can receive payment from user.
5+
# This program is dedicated to the public domain under the CC0 license.
6+
7+
from telegram import (LabeledPrice, ShippingOption)
8+
from telegram.ext import (Updater, CommandHandler, MessageHandler,
9+
Filters, PreCheckoutQueryHandler, ShippingQueryHandler)
10+
import logging
11+
12+
# Enable logging
13+
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
14+
15+
logger = logging.getLogger(__name__)
16+
17+
18+
def error(bot, update, error):
19+
logger.warn('Update "%s" caused error "%s"' % (update, error))
20+
21+
22+
def start_callback(bot, update):
23+
msg = "Use /shipping to get an invoice for shipping-payment, "
24+
msg += "or /noshipping for an invoice without shipping."
25+
update.message.reply_text(msg)
26+
27+
28+
def start_with_shipping_callback(bot, update):
29+
chat_id = update.message.chat_id
30+
title = "Payment Example"
31+
description = "Payment Example using python-telegram-bot"
32+
# select a payload just for you to recognize its the donation from your bot
33+
payload = "Custom-Payload"
34+
# get your provider_token at @botfather, see https://core.telegram.org/bots/payments#getting-a-token
35+
provider_token = "PROVIDER_TOKEN"
36+
start_parameter = "test-payment"
37+
currency = "USD"
38+
# price in dollars
39+
price = 1
40+
# price * 100 so as to include 2 d.p.
41+
# check https://core.telegram.org/bots/payments#supported-currencies for more details
42+
prices = [LabeledPrice("Test", price * 100)]
43+
44+
# optionally pass need_name=True, need_phone_number=True,
45+
# need_email=True, need_shipping_address=True, is_flexible=True
46+
bot.sendInvoice(chat_id, title, description, payload,
47+
provider_token, start_parameter, currency, prices,
48+
need_name=True, need_phone_number=True,
49+
need_email=True, need_shipping_address=True, is_flexible=True)
50+
51+
52+
def start_without_shipping_callback(bot, update):
53+
chat_id = update.message.chat_id
54+
title = "Payment Example"
55+
description = "Payment Example using python-telegram-bot"
56+
# select a payload just for you to recognize its the donation from your bot
57+
payload = "Custom-Payload"
58+
# get your provider_token at @botfather, see https://core.telegram.org/bots/payments#getting-a-token
59+
provider_token = "PROVIDER_TOKEN"
60+
start_parameter = "test-payment"
61+
currency = "USD"
62+
# price in dollars
63+
price = 1
64+
# price * 100 so as to include 2 d.p.
65+
prices = [LabeledPrice("Test", price * 100)]
66+
67+
# optionally pass need_name=True, need_phone_number=True,
68+
# need_email=True, need_shipping_address=True, is_flexible=True
69+
bot.sendInvoice(chat_id, title, description, payload,
70+
provider_token, start_parameter, currency, prices)
71+
72+
73+
def shipping_callback(bot, update):
74+
query = update.shipping_query
75+
# check the payload, is this from your bot?
76+
if query.invoice_payload != 'Custom-Payload':
77+
# answer False pre_checkout_query
78+
bot.answer_shipping_query(shipping_query_id=query.id, ok=False,
79+
error_message="Something went wrong...")
80+
return
81+
else:
82+
options = list()
83+
# a single LabeledPrice
84+
options.append(ShippingOption('1', 'Shipping Option A', [LabeledPrice('A', 100)]))
85+
# an array of LabeledPrice objects
86+
price_list = [LabeledPrice('B1', 150), LabeledPrice('B2', 200)]
87+
options.append(ShippingOption('2', 'Shipping Option B', price_list))
88+
bot.answer_shipping_query(shipping_query_id=query.id, ok=True,
89+
shipping_options=options)
90+
91+
92+
# after (optional) shipping, it's the pre-checkout
93+
def precheckout_callback(bot, update):
94+
query = update.pre_checkout_query
95+
# check the payload, is this from your bot?
96+
if query.invoice_payload != 'Custom-Payload':
97+
# answer False pre_checkout_query
98+
bot.answer_pre_checkout_query(pre_checkout_query_id=query.id, ok=False,
99+
error_message="Something went wrong...")
100+
else:
101+
bot.answer_pre_checkout_query(pre_checkout_query_id=query.id, ok=True)
102+
103+
104+
# finally, after contacting to the payment provider...
105+
def successful_payment_callback(bot, update):
106+
# do something after successful receive of payment?
107+
update.message.reply_text("Thank you for your payment!")
108+
109+
110+
def main():
111+
# Create the EventHandler and pass it your bot's token.
112+
updater = Updater(token="BOT_TOKEN")
113+
114+
# Get the dispatcher to register handlers
115+
dp = updater.dispatcher
116+
117+
# simple start function
118+
dp.add_handler(CommandHandler("start", start_callback))
119+
120+
# Add command handler to start the payment invoice
121+
dp.add_handler(CommandHandler("shipping", start_with_shipping_callback))
122+
dp.add_handler(CommandHandler("noshipping", start_without_shipping_callback))
123+
124+
# Optional handler if your product requires shipping
125+
dp.add_handler(ShippingQueryHandler(shipping_callback))
126+
127+
# Pre-checkout handler to final check
128+
dp.add_handler(PreCheckoutQueryHandler(precheckout_callback))
129+
130+
# Success! Notify your user!
131+
dp.add_handler(MessageHandler(Filters.successful_payment, successful_payment_callback))
132+
133+
# log all errors
134+
dp.add_error_handler(error)
135+
136+
# Start the Bot
137+
updater.start_polling()
138+
139+
# Run the bot until you press Ctrl-C or the process receives SIGINT,
140+
# SIGTERM or SIGABRT. This should be used most of the time, since
141+
# start_polling() is non-blocking and will stop the bot gracefully.
142+
updater.idle()
143+
144+
145+
if __name__ == '__main__':
146+
main()

telegram/bot.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def info(func):
8585
@functools.wraps(func)
8686
def decorator(self, *args, **kwargs):
8787
if not self.bot:
88-
self.getMe()
88+
self.get_me()
8989

9090
result = func(self, *args, **kwargs)
9191
return result
@@ -443,7 +443,7 @@ def send_audio(self,
443443
disable_notification=disable_notification,
444444
reply_to_message_id=reply_to_message_id,
445445
reply_markup=reply_markup,
446-
timeout=20.,
446+
timeout=timeout,
447447
**kwargs)
448448

449449
@log
@@ -1975,7 +1975,7 @@ def answer_shipping_query(self,
19751975
data = {'shipping_query_id': shipping_query_id, 'ok': ok}
19761976

19771977
if ok is True:
1978-
data['shipping_options'] = shipping_options
1978+
data['shipping_options'] = [option.to_dict() for option in shipping_options]
19791979
if error_message is not None:
19801980
data['error_message'] = error_message
19811981

@@ -2009,7 +2009,7 @@ def answer_pre_checkout_query(self, pre_checkout_query_id, ok,
20092009
20102010
"""
20112011

2012-
if not (ok ^ (error_message is None)):
2012+
if not (ok ^ (error_message is not None)):
20132013
raise TelegramError(
20142014
'answerPreCheckoutQuery: If ok is True, there should '
20152015
'not be error_message; if ok is False, error_message '

telegram/chat.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,29 +89,29 @@ def de_json(data, bot):
8989
return Chat(bot=bot, **data)
9090

9191
def send_action(self, *args, **kwargs):
92-
"""Shortcut for ``bot.sendChatAction(update.message.chat.id, *args, **kwargs)``"""
93-
return self.bot.sendChatAction(self.id, *args, **kwargs)
92+
"""Shortcut for ``bot.send_chat_action(update.message.chat.id, *args, **kwargs)``"""
93+
return self.bot.send_chat_action(self.id, *args, **kwargs)
9494

9595
def leave(self, *args, **kwargs):
96-
"""Shortcut for ``bot.leaveChat(update.message.chat.id, *args, **kwargs)``"""
97-
return self.bot.leaveChat(self.id, *args, **kwargs)
96+
"""Shortcut for ``bot.leave_chat(update.message.chat.id, *args, **kwargs)``"""
97+
return self.bot.leave_chat(self.id, *args, **kwargs)
9898

9999
def get_administrators(self, *args, **kwargs):
100-
"""Shortcut for ``bot.getChatAdministrators(update.message.chat.id, *args, **kwargs)``"""
101-
return self.bot.getChatAdministrators(self.id, *args, **kwargs)
100+
"""Shortcut for ``bot.get_chat_administrators(update.message.chat.id, *args, **kwargs)``"""
101+
return self.bot.get_chat_administrators(self.id, *args, **kwargs)
102102

103103
def get_members_count(self, *args, **kwargs):
104-
"""Shortcut for ``bot.getChatMembersCount(update.message.chat.id, *args, **kwargs)``"""
105-
return self.bot.getChatMembersCount(self.id, *args, **kwargs)
104+
"""Shortcut for ``bot.get_chat_members_count(update.message.chat.id, *args, **kwargs)``"""
105+
return self.bot.get_chat_members_count(self.id, *args, **kwargs)
106106

107107
def get_member(self, *args, **kwargs):
108-
"""Shortcut for ``bot.getChatMember(update.message.chat.id, *args, **kwargs)``"""
109-
return self.bot.getChatMember(self.id, *args, **kwargs)
108+
"""Shortcut for ``bot.get_chat_member(update.message.chat.id, *args, **kwargs)``"""
109+
return self.bot.get_chat_member(self.id, *args, **kwargs)
110110

111111
def kick_member(self, *args, **kwargs):
112-
"""Shortcut for ``bot.kickChatMember(update.message.chat.id, *args, **kwargs)``"""
113-
return self.bot.kickChatMember(self.id, *args, **kwargs)
112+
"""Shortcut for ``bot.kick_chat_member(update.message.chat.id, *args, **kwargs)``"""
113+
return self.bot.kick_chat_member(self.id, *args, **kwargs)
114114

115115
def unban_member(self, *args, **kwargs):
116-
"""Shortcut for ``bot.unbanChatMember(update.message.chat.id, *args, **kwargs)``"""
117-
return self.bot.unbanChatMember(self.id, *args, **kwargs)
116+
"""Shortcut for ``bot.unban_chat_member(update.message.chat.id, *args, **kwargs)``"""
117+
return self.bot.unban_chat_member(self.id, *args, **kwargs)

telegram/ext/updater.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def _start_polling(self, poll_interval, timeout, read_latency, bootstrap_retries
264264

265265
while self.running:
266266
try:
267-
updates = self.bot.getUpdates(
267+
updates = self.bot.get_updates(
268268
self.last_update_id,
269269
timeout=timeout,
270270
read_latency=read_latency,
@@ -367,11 +367,11 @@ def _bootstrap(self, max_retries, clean, webhook_url, allowed_updates, cert=None
367367
try:
368368
if clean:
369369
# Disable webhook for cleaning
370-
self.bot.deleteWebhook()
370+
self.bot.delete_webhook()
371371
self._clean_updates()
372372
sleep(1)
373373

374-
self.bot.setWebhook(
374+
self.bot.set_webhook(
375375
url=webhook_url, certificate=cert, allowed_updates=allowed_updates)
376376
except (Unauthorized, InvalidToken):
377377
raise
@@ -390,9 +390,9 @@ def _bootstrap(self, max_retries, clean, webhook_url, allowed_updates, cert=None
390390

391391
def _clean_updates(self):
392392
self.logger.debug('Cleaning updates from Telegram server')
393-
updates = self.bot.getUpdates()
393+
updates = self.bot.get_updates()
394394
while updates:
395-
updates = self.bot.getUpdates(updates[-1].update_id + 1)
395+
updates = self.bot.get_updates(updates[-1].update_id + 1)
396396

397397
def stop(self):
398398
"""

telegram/inlinequery.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,5 @@ def to_dict(self):
9191
return data
9292

9393
def answer(self, *args, **kwargs):
94-
"""Shortcut for ``bot.answerInlineQuery(update.inline_query.id, *args, **kwargs)``"""
95-
return self.bot.answerInlineQuery(self.id, *args, **kwargs)
94+
"""Shortcut for ``bot.answer_inline_query(update.inline_query.id, *args, **kwargs)``"""
95+
return self.bot.answer_inline_query(self.id, *args, **kwargs)

0 commit comments

Comments
 (0)