From 0dbf677006deba432b8d01710979232214e07059 Mon Sep 17 00:00:00 2001 From: Snehashish06 Date: Fri, 15 Nov 2024 23:07:31 +0530 Subject: [PATCH 1/4] Improved comments for clarity and details :) --- AUTHORS.rst | 1 + examples/paymentbot.py | 83 ++++++++++++++++++++++-------------------- 2 files changed, 44 insertions(+), 40 deletions(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index 1106c1e7dd0..74b80ac091e 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -111,6 +111,7 @@ The following wonderful people contributed directly or indirectly to this projec - `Rahiel Kasim `_ - `Riko Naka `_ - `Rizlas `_ +- `Snehashish Biswas `_ - `Sahil Sharma `_ - `Sam Mosleh `_ - `Sascha `_ diff --git a/examples/paymentbot.py b/examples/paymentbot.py index a18ee6c2827..c72c0b02748 100644 --- a/examples/paymentbot.py +++ b/examples/paymentbot.py @@ -2,7 +2,7 @@ # pylint: disable=unused-argument # This program is dedicated to the public domain under the CC0 license. -"""Basic example for a bot that can receive payment from user.""" +"""Basic example for a bot that can receive payments from users.""" import logging @@ -21,41 +21,44 @@ logging.basicConfig( format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO ) -# set higher logging level for httpx to avoid all GET and POST requests being logged +# Set a higher logging level for httpx to avoid logging every GET and POST request logging.getLogger("httpx").setLevel(logging.WARNING) logger = logging.getLogger(__name__) +# Insert the token from your payment provider. In order to get a provider_token see https://core.telegram.org/bots/payments#getting-a-token PAYMENT_PROVIDER_TOKEN = "PAYMENT_PROVIDER_TOKEN" async def start_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - """Displays info on how to use the bot.""" + """Provides instructions on how to use the bot.""" msg = ( - "Use /shipping to get an invoice for shipping-payment, or /noshipping for an " + "Use /shipping to receive an invoice with shipping included, or /noshipping for an " "invoice without shipping." ) - await update.message.reply_text(msg) async def start_with_shipping_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - """Sends an invoice with shipping-payment.""" + """Sends an invoice which triggers a shipping query because it requests a shipping address and has flexible shipping.""" chat_id = update.message.chat_id title = "Payment Example" - description = "Payment Example using python-telegram-bot" - # select a payload just for you to recognize its the donation from your bot + description = "Example of a payment process using the python-telegram-bot library." + # Unique payload to identify this payment request as being from your bot payload = "Custom-Payload" - # In order to get a provider_token see https://core.telegram.org/bots/payments#getting-a-token + # Set up the currency (e.g., "USD"), to get a list of supported currencies see https://core.telegram.org/bots/payments#supported-currencies currency = "USD" - # price in dollars + # Price in dollars price = 1 - # price * 100 so as to include 2 decimal points - # check https://core.telegram.org/bots/payments#supported-currencies for more details + # Convert price to cents from dollars. prices = [LabeledPrice("Test", price * 100)] - # optionally pass need_name=True, need_phone_number=True, - # need_email=True, need_shipping_address=True, is_flexible=True + # The following optional parameters can be used to request additional information: + # - need_name=True: Requests the user's name. + # - need_phone_number=True: Requests the user's phone number. + # - need_email=True: Requests the user's email address. + # - need_shipping_address=True: Requests the user's shipping address. + # - is_flexible=True: Allows flexible shipping prices. await context.bot.send_invoice( chat_id, title, @@ -75,17 +78,17 @@ async def start_with_shipping_callback(update: Update, context: ContextTypes.DEF async def start_without_shipping_callback( update: Update, context: ContextTypes.DEFAULT_TYPE ) -> None: - """Sends an invoice without shipping-payment.""" + """Sends an invoice without requiring shipping details.""" chat_id = update.message.chat_id title = "Payment Example" - description = "Payment Example using python-telegram-bot" - # select a payload just for you to recognize its the donation from your bot + description = "Example of a payment process using the python-telegram-bot library." + # Unique payload to identify this payment request as being from your bot payload = "Custom-Payload" - # In order to get a provider_token see https://core.telegram.org/bots/payments#getting-a-token + # Check line 49 for supported currencies currency = "USD" - # price in dollars + # Price in dollars price = 1 - # price * 100 so as to include 2 decimal points + # Convert price to cents from dollars. prices = [LabeledPrice("Test", price * 100)] # optionally pass need_name=True, need_phone_number=True, @@ -96,65 +99,65 @@ async def start_without_shipping_callback( async def shipping_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - """Answers the ShippingQuery with ShippingOptions""" + """Handles the ShippingQuery with available shipping options.""" query = update.shipping_query - # check the payload, is this from your bot? + # Verify if the payload matches, ensure it's from your bot if query.invoice_payload != "Custom-Payload": - # answer False pre_checkout_query + # If not, respond with an error await query.answer(ok=False, error_message="Something went wrong...") return - # First option has a single LabeledPrice + # Define available shipping options + # First option with a single price entry options = [ShippingOption("1", "Shipping Option A", [LabeledPrice("A", 100)])] - # second option has an array of LabeledPrice objects + # Second option with multiple price entries price_list = [LabeledPrice("B1", 150), LabeledPrice("B2", 200)] options.append(ShippingOption("2", "Shipping Option B", price_list)) await query.answer(ok=True, shipping_options=options) -# after (optional) shipping, it's the pre-checkout +# After (optional) shipping, process the pre-checkout step async def precheckout_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - """Answers the PreQecheckoutQuery""" + """Responds to the PreCheckoutQuery as the final confirmation for checkout.""" query = update.pre_checkout_query - # check the payload, is this from your bot? + # Verify if the payload matches, ensure it's from your bot if query.invoice_payload != "Custom-Payload": - # answer False pre_checkout_query + # If not, respond with an error await query.answer(ok=False, error_message="Something went wrong...") else: await query.answer(ok=True) -# finally, after contacting the payment provider... +# Final callback after successful payment async def successful_payment_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - """Confirms the successful payment.""" - # do something after successfully receiving payment? - await update.message.reply_text("Thank you for your payment!") + """Acknowledges successful payment and thanks the user.""" + await update.message.reply_text("Thank you for your payment.") def main() -> None: - """Run the bot.""" + """Starts the bot and sets up handlers.""" # Create the Application and pass it your bot's token. application = Application.builder().token("TOKEN").build() - # simple start function + # Start command to display usage instructions application.add_handler(CommandHandler("start", start_callback)) - # Add command handler to start the payment invoice + # Command handlers for starting the payment process application.add_handler(CommandHandler("shipping", start_with_shipping_callback)) application.add_handler(CommandHandler("noshipping", start_without_shipping_callback)) - # Optional handler if your product requires shipping + # Handler for shipping query (if product requires shipping) application.add_handler(ShippingQueryHandler(shipping_callback)) - # Pre-checkout handler to final check + # Pre-checkout handler for verifying payment details. application.add_handler(PreCheckoutQueryHandler(precheckout_callback)) - # Success! Notify your user! + # Handler for successful payment. Notify the user that the payment was successful. application.add_handler( MessageHandler(filters.SUCCESSFUL_PAYMENT, successful_payment_callback) ) - # Run the bot until the user presses Ctrl-C + # Start polling for updates until interrupted (CTRL+C) application.run_polling(allowed_updates=Update.ALL_TYPES) From 150a26c0119e3b408fd1b279c5bb52f6fe596172 Mon Sep 17 00:00:00 2001 From: Snehashish Biswas Date: Sat, 16 Nov 2024 14:39:20 +0530 Subject: [PATCH 2/4] Update paymentbot.py --- examples/paymentbot.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/examples/paymentbot.py b/examples/paymentbot.py index c72c0b02748..a4afdd08a48 100644 --- a/examples/paymentbot.py +++ b/examples/paymentbot.py @@ -26,7 +26,8 @@ logger = logging.getLogger(__name__) -# Insert the token from your payment provider. In order to get a provider_token see https://core.telegram.org/bots/payments#getting-a-token +# Insert the token from your payment provider. +# In order to get a provider_token see https://core.telegram.org/bots/payments#getting-a-token PAYMENT_PROVIDER_TOKEN = "PAYMENT_PROVIDER_TOKEN" @@ -40,25 +41,21 @@ async def start_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> async def start_with_shipping_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - """Sends an invoice which triggers a shipping query because it requests a shipping address and has flexible shipping.""" + """Sends an invoice which triggers a shipping query.""" chat_id = update.message.chat_id title = "Payment Example" description = "Example of a payment process using the python-telegram-bot library." # Unique payload to identify this payment request as being from your bot payload = "Custom-Payload" - # Set up the currency (e.g., "USD"), to get a list of supported currencies see https://core.telegram.org/bots/payments#supported-currencies + # Set up the currency. + # List of supported currencies: https://core.telegram.org/bots/payments#supported-currencies currency = "USD" # Price in dollars price = 1 # Convert price to cents from dollars. prices = [LabeledPrice("Test", price * 100)] - - # The following optional parameters can be used to request additional information: - # - need_name=True: Requests the user's name. - # - need_phone_number=True: Requests the user's phone number. - # - need_email=True: Requests the user's email address. - # - need_shipping_address=True: Requests the user's shipping address. - # - is_flexible=True: Allows flexible shipping prices. + # Optional parameters like need_shipping_address and is_flexible trigger additional user prompts + # https://docs.python-telegram-bot.org/en/stable/telegram.bot.html#telegram.Bot.send_invoice await context.bot.send_invoice( chat_id, title, @@ -84,7 +81,6 @@ async def start_without_shipping_callback( description = "Example of a payment process using the python-telegram-bot library." # Unique payload to identify this payment request as being from your bot payload = "Custom-Payload" - # Check line 49 for supported currencies currency = "USD" # Price in dollars price = 1 From 3f70a4312d8a5f8c7912095fa35e111534cb774c Mon Sep 17 00:00:00 2001 From: Snehashish Biswas Date: Tue, 19 Nov 2024 00:24:08 +0530 Subject: [PATCH 3/4] Update paymentbot.py --- examples/paymentbot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/paymentbot.py b/examples/paymentbot.py index a4afdd08a48..f04ce635ea6 100644 --- a/examples/paymentbot.py +++ b/examples/paymentbot.py @@ -21,7 +21,7 @@ logging.basicConfig( format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO ) -# Set a higher logging level for httpx to avoid logging every GET and POST request +# set higher logging level for httpx to avoid all GET and POST requests being logged logging.getLogger("httpx").setLevel(logging.WARNING) logger = logging.getLogger(__name__) From 08f433ebb3504caa891ee7991e3b6157fe487f87 Mon Sep 17 00:00:00 2001 From: Snehashish Biswas Date: Wed, 20 Nov 2024 11:53:56 +0530 Subject: [PATCH 4/4] Update paymentbot.py --- examples/paymentbot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/paymentbot.py b/examples/paymentbot.py index f04ce635ea6..90daa44ce2a 100644 --- a/examples/paymentbot.py +++ b/examples/paymentbot.py @@ -54,7 +54,7 @@ async def start_with_shipping_callback(update: Update, context: ContextTypes.DEF price = 1 # Convert price to cents from dollars. prices = [LabeledPrice("Test", price * 100)] - # Optional parameters like need_shipping_address and is_flexible trigger additional user prompts + # Optional parameters like need_shipping_address and is_flexible trigger extra user prompts # https://docs.python-telegram-bot.org/en/stable/telegram.bot.html#telegram.Bot.send_invoice await context.bot.send_invoice( chat_id, pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy