2017-06-10 21:43:38 +02:00
|
|
|
#!/usr/bin/env python
|
2023-09-09 23:12:30 +02:00
|
|
|
# pylint: disable=unused-argument
|
2019-02-13 13:38:07 +01:00
|
|
|
# This program is dedicated to the public domain under the CC0 license.
|
2017-10-20 20:24:00 +02:00
|
|
|
|
2024-12-01 11:47:38 +01:00
|
|
|
"""Basic example for a bot that can receive payments from users."""
|
2017-06-10 21:43:38 +02:00
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
import logging
|
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
from telegram import LabeledPrice, ShippingOption, Update
|
2020-10-09 17:22:07 +02:00
|
|
|
from telegram.ext import (
|
2022-05-05 09:27:54 +02:00
|
|
|
Application,
|
2020-10-09 17:22:07 +02:00
|
|
|
CommandHandler,
|
2022-05-12 19:36:25 +02:00
|
|
|
ContextTypes,
|
2020-10-09 17:22:07 +02:00
|
|
|
MessageHandler,
|
|
|
|
PreCheckoutQueryHandler,
|
|
|
|
ShippingQueryHandler,
|
2022-05-05 09:27:54 +02:00
|
|
|
filters,
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2017-06-10 21:43:38 +02:00
|
|
|
|
|
|
|
# Enable logging
|
2020-10-09 17:22:07 +02:00
|
|
|
logging.basicConfig(
|
2022-05-05 17:40:22 +02:00
|
|
|
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2023-06-07 22:32:04 +02:00
|
|
|
# set higher logging level for httpx to avoid all GET and POST requests being logged
|
|
|
|
logging.getLogger("httpx").setLevel(logging.WARNING)
|
|
|
|
|
2017-06-10 21:43:38 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2024-12-01 11:47:38 +01:00
|
|
|
# Insert the token from your payment provider.
|
|
|
|
# In order to get a provider_token see https://core.telegram.org/bots/payments#getting-a-token
|
2022-04-24 12:38:09 +02:00
|
|
|
PAYMENT_PROVIDER_TOKEN = "PAYMENT_PROVIDER_TOKEN"
|
2017-06-10 21:43:38 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def start_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2024-12-01 11:47:38 +01:00
|
|
|
"""Provides instructions on how to use the bot."""
|
2021-03-28 18:53:44 +02:00
|
|
|
msg = (
|
2024-12-01 11:47:38 +01:00
|
|
|
"Use /shipping to receive an invoice with shipping included, or /noshipping for an "
|
2021-03-28 18:53:44 +02:00
|
|
|
"invoice without shipping."
|
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_text(msg)
|
2017-06-10 21:43:38 +02:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def start_with_shipping_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2024-12-01 11:47:38 +01:00
|
|
|
"""Sends an invoice which triggers a shipping query."""
|
2017-06-10 21:43:38 +02:00
|
|
|
chat_id = update.message.chat_id
|
|
|
|
title = "Payment Example"
|
2024-12-01 11:47:38 +01:00
|
|
|
description = "Example of a payment process using the python-telegram-bot library."
|
|
|
|
# Unique payload to identify this payment request as being from your bot
|
2017-06-10 21:43:38 +02:00
|
|
|
payload = "Custom-Payload"
|
2024-12-01 11:47:38 +01:00
|
|
|
# Set up the currency.
|
|
|
|
# List of supported currencies: https://core.telegram.org/bots/payments#supported-currencies
|
2017-06-10 21:43:38 +02:00
|
|
|
currency = "USD"
|
2024-12-01 11:47:38 +01:00
|
|
|
# Price in dollars
|
2017-06-10 21:43:38 +02:00
|
|
|
price = 1
|
2024-12-01 11:47:38 +01:00
|
|
|
# Convert price to cents from dollars.
|
2017-06-10 21:43:38 +02:00
|
|
|
prices = [LabeledPrice("Test", price * 100)]
|
2024-12-01 11:47:38 +01:00
|
|
|
# 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
|
2022-04-24 12:38:09 +02:00
|
|
|
await context.bot.send_invoice(
|
2020-10-09 17:22:07 +02:00
|
|
|
chat_id,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
payload,
|
2022-04-24 12:38:09 +02:00
|
|
|
PAYMENT_PROVIDER_TOKEN,
|
2020-10-09 17:22:07 +02:00
|
|
|
currency,
|
|
|
|
prices,
|
|
|
|
need_name=True,
|
|
|
|
need_phone_number=True,
|
|
|
|
need_email=True,
|
|
|
|
need_shipping_address=True,
|
|
|
|
is_flexible=True,
|
|
|
|
)
|
2017-06-10 21:43:38 +02:00
|
|
|
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def start_without_shipping_callback(
|
2022-05-12 19:36:25 +02:00
|
|
|
update: Update, context: ContextTypes.DEFAULT_TYPE
|
2022-04-24 12:38:09 +02:00
|
|
|
) -> None:
|
2024-12-01 11:47:38 +01:00
|
|
|
"""Sends an invoice without requiring shipping details."""
|
2017-06-10 21:43:38 +02:00
|
|
|
chat_id = update.message.chat_id
|
|
|
|
title = "Payment Example"
|
2024-12-01 11:47:38 +01:00
|
|
|
description = "Example of a payment process using the python-telegram-bot library."
|
|
|
|
# Unique payload to identify this payment request as being from your bot
|
2017-06-10 21:43:38 +02:00
|
|
|
payload = "Custom-Payload"
|
|
|
|
currency = "USD"
|
2024-12-01 11:47:38 +01:00
|
|
|
# Price in dollars
|
2017-06-10 21:43:38 +02:00
|
|
|
price = 1
|
2024-12-01 11:47:38 +01:00
|
|
|
# Convert price to cents from dollars.
|
2017-06-10 21:43:38 +02:00
|
|
|
prices = [LabeledPrice("Test", price * 100)]
|
|
|
|
|
|
|
|
# optionally pass need_name=True, need_phone_number=True,
|
|
|
|
# need_email=True, need_shipping_address=True, is_flexible=True
|
2022-04-24 12:38:09 +02:00
|
|
|
await context.bot.send_invoice(
|
|
|
|
chat_id, title, description, payload, PAYMENT_PROVIDER_TOKEN, currency, prices
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2017-06-10 21:43:38 +02:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def shipping_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2024-12-01 11:47:38 +01:00
|
|
|
"""Handles the ShippingQuery with available shipping options."""
|
2017-06-10 21:43:38 +02:00
|
|
|
query = update.shipping_query
|
2024-12-01 11:47:38 +01:00
|
|
|
# Verify if the payload matches, ensure it's from your bot
|
2022-05-05 17:40:22 +02:00
|
|
|
if query.invoice_payload != "Custom-Payload":
|
2024-12-01 11:47:38 +01:00
|
|
|
# If not, respond with an error
|
2022-04-24 12:38:09 +02:00
|
|
|
await query.answer(ok=False, error_message="Something went wrong...")
|
2017-06-10 21:43:38 +02:00
|
|
|
return
|
2020-10-31 16:33:34 +01:00
|
|
|
|
2024-12-01 11:47:38 +01:00
|
|
|
# Define available shipping options
|
|
|
|
# First option with a single price entry
|
2022-05-05 17:40:22 +02:00
|
|
|
options = [ShippingOption("1", "Shipping Option A", [LabeledPrice("A", 100)])]
|
2024-12-01 11:47:38 +01:00
|
|
|
# Second option with multiple price entries
|
2022-05-05 17:40:22 +02:00
|
|
|
price_list = [LabeledPrice("B1", 150), LabeledPrice("B2", 200)]
|
|
|
|
options.append(ShippingOption("2", "Shipping Option B", price_list))
|
2022-04-24 12:38:09 +02:00
|
|
|
await query.answer(ok=True, shipping_options=options)
|
2017-06-10 21:43:38 +02:00
|
|
|
|
|
|
|
|
2024-12-01 11:47:38 +01:00
|
|
|
# After (optional) shipping, process the pre-checkout step
|
2022-05-12 19:36:25 +02:00
|
|
|
async def precheckout_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2024-12-01 11:47:38 +01:00
|
|
|
"""Responds to the PreCheckoutQuery as the final confirmation for checkout."""
|
2017-06-10 21:43:38 +02:00
|
|
|
query = update.pre_checkout_query
|
2024-12-01 11:47:38 +01:00
|
|
|
# Verify if the payload matches, ensure it's from your bot
|
2022-05-05 17:40:22 +02:00
|
|
|
if query.invoice_payload != "Custom-Payload":
|
2024-12-01 11:47:38 +01:00
|
|
|
# If not, respond with an error
|
2022-04-24 12:38:09 +02:00
|
|
|
await query.answer(ok=False, error_message="Something went wrong...")
|
2017-06-10 21:43:38 +02:00
|
|
|
else:
|
2022-04-24 12:38:09 +02:00
|
|
|
await query.answer(ok=True)
|
2017-06-10 21:43:38 +02:00
|
|
|
|
|
|
|
|
2024-12-01 11:47:38 +01:00
|
|
|
# Final callback after successful payment
|
2022-05-12 19:36:25 +02:00
|
|
|
async def successful_payment_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2024-12-01 11:47:38 +01:00
|
|
|
"""Acknowledges successful payment and thanks the user."""
|
|
|
|
await update.message.reply_text("Thank you for your payment.")
|
2017-06-10 21:43:38 +02:00
|
|
|
|
|
|
|
|
2021-03-13 16:21:03 +01:00
|
|
|
def main() -> None:
|
2024-12-01 11:47:38 +01:00
|
|
|
"""Starts the bot and sets up handlers."""
|
2022-04-24 12:38:09 +02:00
|
|
|
# Create the Application and pass it your bot's token.
|
|
|
|
application = Application.builder().token("TOKEN").build()
|
2017-06-10 21:43:38 +02:00
|
|
|
|
2024-12-01 11:47:38 +01:00
|
|
|
# Start command to display usage instructions
|
2022-04-24 12:38:09 +02:00
|
|
|
application.add_handler(CommandHandler("start", start_callback))
|
2017-06-10 21:43:38 +02:00
|
|
|
|
2024-12-01 11:47:38 +01:00
|
|
|
# Command handlers for starting the payment process
|
2022-04-24 12:38:09 +02:00
|
|
|
application.add_handler(CommandHandler("shipping", start_with_shipping_callback))
|
|
|
|
application.add_handler(CommandHandler("noshipping", start_without_shipping_callback))
|
2017-06-10 21:43:38 +02:00
|
|
|
|
2024-12-01 11:47:38 +01:00
|
|
|
# Handler for shipping query (if product requires shipping)
|
2022-04-24 12:38:09 +02:00
|
|
|
application.add_handler(ShippingQueryHandler(shipping_callback))
|
2017-06-10 21:43:38 +02:00
|
|
|
|
2024-12-01 11:47:38 +01:00
|
|
|
# Pre-checkout handler for verifying payment details.
|
2022-04-24 12:38:09 +02:00
|
|
|
application.add_handler(PreCheckoutQueryHandler(precheckout_callback))
|
2017-06-10 21:43:38 +02:00
|
|
|
|
2024-12-01 11:47:38 +01:00
|
|
|
# Handler for successful payment. Notify the user that the payment was successful.
|
2022-04-24 12:38:09 +02:00
|
|
|
application.add_handler(
|
|
|
|
MessageHandler(filters.SUCCESSFUL_PAYMENT, successful_payment_callback)
|
|
|
|
)
|
2017-06-10 21:43:38 +02:00
|
|
|
|
2024-12-01 11:47:38 +01:00
|
|
|
# Start polling for updates until interrupted (CTRL+C)
|
2023-06-04 17:11:58 +02:00
|
|
|
application.run_polling(allowed_updates=Update.ALL_TYPES)
|
2017-06-10 21:43:38 +02:00
|
|
|
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
if __name__ == "__main__":
|
2017-06-10 21:43:38 +02:00
|
|
|
main()
|