X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions examples/paymentbot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Basic example for a bot that can receive payment from user.
# This program is dedicated to the public domain under the CC0 license.

from telegram import (LabeledPrice, ShippingOption)
from telegram.ext import (Updater, CommandHandler, MessageHandler,
Filters, PreCheckoutQueryHandler, ShippingQueryHandler)
import logging

# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)

logger = logging.getLogger(__name__)


def error(bot, update, error):
logger.warn('Update "%s" caused error "%s"' % (update, error))


def start_callback(bot, update):
msg = "Use /shipping to get an invoice for shipping-payment, "
msg += "or /noshipping for an invoice without shipping."
update.message.reply_text(msg)


def start_with_shipping_callback(bot, update):
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
payload = "Custom-Payload"
# get your provider_token at @botfather, see https://core.telegram.org/bots/payments#getting-a-token
provider_token = "PROVIDER_TOKEN"
start_parameter = "test-payment"
currency = "USD"
# price in dollars
price = 1
# price * 100 so as to include 2 d.p.
# check https://core.telegram.org/bots/payments#supported-currencies for more details
prices = [LabeledPrice("Test", price * 100)]

# optionally pass need_name=True, need_phone_number=True,
# need_email=True, need_shipping_address=True, is_flexible=True
bot.sendInvoice(chat_id, title, description, payload,
provider_token, start_parameter, currency, prices,
need_name=True, need_phone_number=True,
need_email=True, need_shipping_address=True, is_flexible=True)


def start_without_shipping_callback(bot, update):
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
payload = "Custom-Payload"
# get your provider_token at @botfather, see https://core.telegram.org/bots/payments#getting-a-token
provider_token = "PROVIDER_TOKEN"
start_parameter = "test-payment"
currency = "USD"
# price in dollars
price = 1
# price * 100 so as to include 2 d.p.
prices = [LabeledPrice("Test", price * 100)]

# optionally pass need_name=True, need_phone_number=True,
# need_email=True, need_shipping_address=True, is_flexible=True
bot.sendInvoice(chat_id, title, description, payload,
provider_token, start_parameter, currency, prices)


def shipping_callback(bot, update):
query = update.shipping_query
# check the payload, is this from your bot?
if query.invoice_payload != 'Custom-Payload':
# answer False pre_checkout_query
bot.answerShippingQuery(shipping_query_id=query.id, ok=False,
error_message="Something went wrong...")
return
else:
options = list()
# a single LabeledPrice
options.append(ShippingOption('1', 'Shipping Option A', [LabeledPrice('A', 100)]))
# an array of LabeledPrice objects
price_list = [LabeledPrice('B1', 150), LabeledPrice('B2', 200)]
options.append(ShippingOption('2', 'Shipping Option B', price_list))
bot.answerShippingQuery(shipping_query_id=query.id, ok=True,
shipping_options=options)


# after (optional) shipping, it's the pre-checkout
def precheckout_callback(bot, update):
query = update.pre_checkout_query
# check the payload, is this from your bot?
if query.invoice_payload != 'Custom-Payload':
# answer False pre_checkout_query
bot.answerPreCheckoutQuery(pre_checkout_query_id=query.id, ok=False,
error_message="Something went wrong...")
else:
bot.answerPreCheckoutQuery(pre_checkout_query_id=query.id, ok=True)


# finally, after contacting to the payment provider...
def successful_payment_callback(bot, update):
# do something after successful receive of payment?
update.message.reply_text("Thank you for your payment!")


def main():
# Create the EventHandler and pass it your bot's token.
updater = Updater(token="BOT_TOKEN")

# Get the dispatcher to register handlers
dp = updater.dispatcher

# simple start function
dp.add_handler(CommandHandler("start", start_callback))

# Add command handler to start the payment invoice
dp.add_handler(CommandHandler("shipping", start_with_shipping_callback))
dp.add_handler(CommandHandler("noshipping", start_without_shipping_callback))

# Optional handler if your product requires shipping
dp.add_handler(ShippingQueryHandler(shipping_callback))

# Pre-checkout handler to final check
dp.add_handler(PreCheckoutQueryHandler(precheckout_callback))

# Success! Notify your user!
dp.add_handler(MessageHandler(Filters.successful_payment, successful_payment_callback))

# log all errors
dp.add_error_handler(error)

# Start the Bot
updater.start_polling()

# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()


if __name__ == '__main__':
main()
4 changes: 2 additions & 2 deletions telegram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1975,7 +1975,7 @@ def answer_shipping_query(self,
data = {'shipping_query_id': shipping_query_id, 'ok': ok}

if ok is True:
data['shipping_options'] = shipping_options
data['shipping_options'] = [option.to_dict() for option in shipping_options]
if error_message is not None:
data['error_message'] = error_message

Expand Down Expand Up @@ -2009,7 +2009,7 @@ def answer_pre_checkout_query(self, pre_checkout_query_id, ok,

"""

if not (ok ^ (error_message is None)):
if not (ok ^ (error_message is not None)):
raise TelegramError(
'answerPreCheckoutQuery: If ok is True, there should '
'not be error_message; if ok is False, error_message '
Expand Down
X Tutup