2
2
# pylint: disable=unused-argument
3
3
# This program is dedicated to the public domain under the CC0 license.
4
4
5
- """Basic example for a bot that can receivepayment fromuser ."""
5
+ """Basic example for a bot that can receivepayments fromusers ."""
6
6
7
7
import logging
8
8
26
26
27
27
logger = logging .getLogger (__name__ )
28
28
29
+ # Insert the token from your payment provider.
30
+ # In order to get a provider_token see https://core.telegram.org/bots/payments#getting-a-token
29
31
PAYMENT_PROVIDER_TOKEN = "PAYMENT_PROVIDER_TOKEN"
30
32
31
33
32
34
async def start_callback (update :Update ,context :ContextTypes .DEFAULT_TYPE )-> None :
33
- """Displays info on how to use the bot."""
35
+ """Provides instructions on how to use the bot."""
34
36
msg = (
35
- "Use /shipping toget an invoicefor shipping-payment , or /noshipping for an "
37
+ "Use /shipping toreceive an invoicewith shipping included , or /noshipping for an "
36
38
"invoice without shipping."
37
39
)
38
-
39
40
await update .message .reply_text (msg )
40
41
41
42
42
43
async def start_with_shipping_callback (update :Update ,context :ContextTypes .DEFAULT_TYPE )-> None :
43
- """Sends an invoicewith shipping-payment ."""
44
+ """Sends an invoicewhich triggers a shipping query ."""
44
45
chat_id = update .message .chat_id
45
46
title = "Payment Example"
46
- description = "Payment Example using python-telegram-bot"
47
- #select a payloadjust for you to recognize its the donation from your bot
47
+ description = "Exampleof a payment process usingthe python-telegram-bot library. "
48
+ #Unique payloadto identify this payment request as being from your bot
48
49
payload = "Custom-Payload"
49
- # In order to get a provider_token see https://core.telegram.org/bots/payments#getting-a-token
50
+ # Set up the currency.
51
+ # List of supported currencies: https://core.telegram.org/bots/payments#supported-currencies
50
52
currency = "USD"
51
- #price in dollars
53
+ #Price in dollars
52
54
price = 1
53
- # price * 100 so as to include 2 decimal points
54
- # check https://core.telegram.org/bots/payments#supported-currencies for more details
55
+ # Convert price to cents from dollars.
55
56
prices = [LabeledPrice ("Test" ,price * 100 )]
56
-
57
- # optionally pass need_name=True, need_phone_number=True,
58
- # need_email=True, need_shipping_address=True, is_flexible=True
57
+ # Optional parameters like need_shipping_address and is_flexible trigger extra user prompts
58
+ # https://docs.python-telegram-bot.org/en/stable/telegram.bot.html#telegram.Bot.send_invoice
59
59
await context .bot .send_invoice (
60
60
chat_id ,
61
61
title ,
@@ -75,17 +75,16 @@ async def start_with_shipping_callback(update: Update, context: ContextTypes.DEF
75
75
async def start_without_shipping_callback (
76
76
update :Update ,context :ContextTypes .DEFAULT_TYPE
77
77
)-> None :
78
- """Sends an invoice without shipping-payment ."""
78
+ """Sends an invoice withoutrequiring shipping details ."""
79
79
chat_id = update .message .chat_id
80
80
title = "Payment Example"
81
- description = "Payment Example using python-telegram-bot"
82
- #select a payloadjust for you to recognize its the donation from your bot
81
+ description = "Exampleof a payment process usingthe python-telegram-bot library. "
82
+ #Unique payloadto identify this payment request as being from your bot
83
83
payload = "Custom-Payload"
84
- # In order to get a provider_token see https://core.telegram.org/bots/payments#getting-a-token
85
84
currency = "USD"
86
- #price in dollars
85
+ #Price in dollars
87
86
price = 1
88
- # price* 100 so as toinclude 2 decimal points
87
+ #Convert price tocents from dollars.
89
88
prices = [LabeledPrice ("Test" ,price * 100 )]
90
89
91
90
# optionally pass need_name=True, need_phone_number=True,
@@ -96,65 +95,65 @@ async def start_without_shipping_callback(
96
95
97
96
98
97
async def shipping_callback (update :Update ,context :ContextTypes .DEFAULT_TYPE )-> None :
99
- """Answers the ShippingQuery withShippingOptions """
98
+ """Handles the ShippingQuery withavailable shipping options. """
100
99
query = update .shipping_query
101
- #check the payload, is this from your bot?
100
+ #Verify if the payload matches, ensure it's from your bot
102
101
if query .invoice_payload != "Custom-Payload" :
103
- #answer False pre_checkout_query
102
+ #If not, respond with an error
104
103
await query .answer (ok = False ,error_message = "Something went wrong..." )
105
104
return
106
105
107
- # First option has a single LabeledPrice
106
+ # Define available shipping options
107
+ # First option with a single price entry
108
108
options = [ShippingOption ("1" ,"Shipping Option A" , [LabeledPrice ("A" ,100 )])]
109
- #second optionhas an array of LabeledPrice objects
109
+ #Second optionwith multiple price entries
110
110
price_list = [LabeledPrice ("B1" ,150 ),LabeledPrice ("B2" ,200 )]
111
111
options .append (ShippingOption ("2" ,"Shipping Option B" ,price_list ))
112
112
await query .answer (ok = True ,shipping_options = options )
113
113
114
114
115
- #after (optional) shipping,it's the pre-checkout
115
+ #After (optional) shipping,process the pre-checkout step
116
116
async def precheckout_callback (update :Update ,context :ContextTypes .DEFAULT_TYPE )-> None :
117
- """Answers thePreQecheckoutQuery """
117
+ """Responds to thePreCheckoutQuery as the final confirmation for checkout. """
118
118
query = update .pre_checkout_query
119
- #check the payload, is this from your bot?
119
+ #Verify if the payload matches, ensure it's from your bot
120
120
if query .invoice_payload != "Custom-Payload" :
121
- #answer False pre_checkout_query
121
+ #If not, respond with an error
122
122
await query .answer (ok = False ,error_message = "Something went wrong..." )
123
123
else :
124
124
await query .answer (ok = True )
125
125
126
126
127
- #finally, aftercontacting the payment provider...
127
+ #Final callback aftersuccessful payment
128
128
async def successful_payment_callback (update :Update ,context :ContextTypes .DEFAULT_TYPE )-> None :
129
- """Confirms the successful payment."""
130
- # do something after successfully receiving payment?
131
- await update .message .reply_text ("Thank you for your payment!" )
129
+ """Acknowledges successful payment and thanks the user."""
130
+ await update .message .reply_text ("Thank you for your payment." )
132
131
133
132
134
133
def main ()-> None :
135
- """Run the bot."""
134
+ """Starts the bot and sets up handlers ."""
136
135
# Create the Application and pass it your bot's token.
137
136
application = Application .builder ().token ("TOKEN" ).build ()
138
137
139
- #simple start function
138
+ #Start command to display usage instructions
140
139
application .add_handler (CommandHandler ("start" ,start_callback ))
141
140
142
- #Add command handler to start the paymentinvoice
141
+ #Command handlers for starting the paymentprocess
143
142
application .add_handler (CommandHandler ("shipping" ,start_with_shipping_callback ))
144
143
application .add_handler (CommandHandler ("noshipping" ,start_without_shipping_callback ))
145
144
146
- #Optional handler if your product requires shipping
145
+ #Handler for shipping query (if product requires shipping)
147
146
application .add_handler (ShippingQueryHandler (shipping_callback ))
148
147
149
- # Pre-checkout handlerto final check
148
+ # Pre-checkout handlerfor verifying payment details.
150
149
application .add_handler (PreCheckoutQueryHandler (precheckout_callback ))
151
150
152
- #Success! Notifyyour user!
151
+ #Handler for successful payment. Notifythe user that the payment was successful.
153
152
application .add_handler (
154
153
MessageHandler (filters .SUCCESSFUL_PAYMENT ,successful_payment_callback )
155
154
)
156
155
157
- #Run the bot untilthe user presses Ctrl-C
156
+ #Start polling for updates untilinterrupted (CTRL+C)
158
157
application .run_polling (allowed_updates = Update .ALL_TYPES )
159
158
160
159