Skip to content

Commit 754ca77

Browse files
committed
Add payments bot example
1 parent 5ffd9c5 commit 754ca77

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

‎examples/payments_example.py‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import telebot
2+
from telebot.types import LabeledPrice
3+
from telebot.types import ShippingOption
4+
5+
token = '1234567890:AAAABBBBCCCCDDDDeeeeFFFFgggGHHHH'
6+
provider_token = '1234567890:TEST:AAAABBBBCCCCDDDD' # @BotFather -> Bot Settings -> Payments
7+
bot = telebot.TeleBot(token)
8+
9+
# More about Payments: https://core.telegram.org/bots/payments
10+
11+
prices = [LabeledPrice(label='Working Time Machine', amount=5750), LabeledPrice('Gift wrapping', 500)]
12+
13+
shipping_options = [
14+
ShippingOption(id='instant', title='WorldWide Teleporter').add_price(LabeledPrice('Teleporter', 1000)),
15+
ShippingOption(id='pickup', title='Local pickup').add_price(LabeledPrice('Pickup', 300))]
16+
17+
18+
@bot.message_handler(commands=['start'])
19+
def command_start(message):
20+
bot.send_message(message.chat.id,
21+
"Hello, I'm the demo merchant bot."
22+
" I can sell you a Time Machine."
23+
" Use /buy to order one, /terms for Terms and Conditions")
24+
25+
26+
@bot.message_handler(commands=['terms'])
27+
def command_terms(message):
28+
bot.send_message(message.chat.id,
29+
'Thank you for shopping with our demo bot. We hope you like your new time machine!\n'
30+
'1. If your time machine was not delivered on time, please rethink your concept of time and try again.\n'
31+
'2. If you find that your time machine is not working, kindly contact our future service workshops on Trappist-1e.'
32+
' They will be accessible anywhere between May 2075 and November 4000 C.E.\n'
33+
'3. If you would like a refund, kindly apply for one yesterday and we will have sent it to you immediately.')
34+
35+
36+
@bot.message_handler(commands=['buy'])
37+
def command_pay(message):
38+
bot.send_message(message.chat.id,
39+
"Real cards won't work with me, no money will be debited from your account."
40+
" Use this test card number to pay for your Time Machine: `4242 4242 4242 4242`"
41+
"\n\nThis is your demo invoice:", parse_mode='Markdown')
42+
bot.send_invoice(message.chat.id, title='Working Time Machine',
43+
description='Want to visit your great-great-great-grandparents?'
44+
' Make a fortune at the races?'
45+
' Shake hands with Hammurabi and take a stroll in the Hanging Gardens?'
46+
' Order our Working Time Machine today!',
47+
provider_token=provider_token,
48+
currency='usd',
49+
photo_url='http://erkelzaar.tsudao.com/models/perrotta/TIME_MACHINE.jpg',
50+
photo_height=512, # !=0/None or picture won't be shown
51+
photo_width=512,
52+
photo_size=512,
53+
is_flexible=False, # True If you need to set up Shipping Fee
54+
prices=prices,
55+
start_parameter='time-machine-example',
56+
invoice_payload='HAPPY FRIDAYS COUPON')
57+
58+
59+
@bot.shipping_query_handler(func=lambda query: True)
60+
def shipping(shipping_query):
61+
print(shipping_query)
62+
bot.answer_shipping_query(shipping_query.id, ok=True, shipping_options=shipping_options,
63+
error_message='Oh, seems like our Dog couriers are having a lunch right now. Try again later!')
64+
65+
66+
@bot.pre_checkout_query_handler(func=lambda query: True)
67+
def checkout(pre_checkout_query):
68+
bot.answer_pre_checkout_query(pre_checkout_query.id, ok=True,
69+
error_message="Aliens tried to steal your card's CVV, but we successfully protected your credentials,"
70+
" try to pay again in a few minutes, we need a small rest.")
71+
72+
73+
@bot.message_handler(content_types=['successful_payment'])
74+
def got_payment(message):
75+
bot.send_message(message.chat.id,
76+
'Hoooooray! Thanks for payment! We will proceed your order for `{} {}` as fast as possible! '
77+
'Stay in touch.\n\nUse /buy again to get a Time Machine for your friend!'.format(
78+
message.successful_payment.total_amount / 100, message.successful_payment.currency),
79+
parse_mode='Markdown')
80+
81+
82+
bot.skip_pending = True
83+
bot.polling(none_stop=True, interval=0)

0 commit comments

Comments
 (0)