2017-10-23 16:24:08 +03:00
|
|
|
import asyncio
|
|
|
|
|
|
|
|
|
|
from aiogram import Bot
|
|
|
|
|
from aiogram import types
|
2018-02-17 20:41:55 +03:00
|
|
|
from aiogram.dispatcher import Dispatcher
|
2018-08-13 23:53:37 +03:00
|
|
|
from aiogram.types.message import ContentTypes
|
2019-03-24 00:15:09 +05:00
|
|
|
from aiogram.utils import executor
|
2017-10-23 16:24:08 +03:00
|
|
|
|
2019-06-29 19:53:18 +03:00
|
|
|
BOT_TOKEN = "BOT TOKEN HERE"
|
|
|
|
|
PAYMENTS_PROVIDER_TOKEN = "123456789:TEST:1234567890abcdef1234567890abcdef"
|
2017-10-23 16:24:08 +03:00
|
|
|
|
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
bot = Bot(BOT_TOKEN)
|
|
|
|
|
dp = Dispatcher(bot, loop=loop)
|
|
|
|
|
|
|
|
|
|
# Setup prices
|
|
|
|
|
prices = [
|
2019-06-29 19:53:18 +03:00
|
|
|
types.LabeledPrice(label="Working Time Machine", amount=5750),
|
|
|
|
|
types.LabeledPrice(label="Gift wrapping", amount=500),
|
2017-10-23 16:24:08 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
# Setup shipping options
|
|
|
|
|
shipping_options = [
|
2019-06-29 19:53:18 +03:00
|
|
|
types.ShippingOption(id="instant", title="WorldWide Teleporter").add(
|
|
|
|
|
types.LabeledPrice("Teleporter", 1000)
|
|
|
|
|
),
|
|
|
|
|
types.ShippingOption(id="pickup", title="Local pickup").add(types.LabeledPrice("Pickup", 300)),
|
2017-10-23 16:24:08 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2019-06-29 19:53:18 +03:00
|
|
|
@dp.message_handler(commands=["start"])
|
2017-10-23 16:24:08 +03:00
|
|
|
async def cmd_start(message: types.Message):
|
2019-06-29 19:53:18 +03:00
|
|
|
await bot.send_message(
|
|
|
|
|
message.chat.id,
|
|
|
|
|
"Hello, I'm the demo merchant bot."
|
|
|
|
|
" I can sell you a Time Machine."
|
|
|
|
|
" Use /buy to order one, /terms for Terms and Conditions",
|
|
|
|
|
)
|
2017-10-23 16:24:08 +03:00
|
|
|
|
|
|
|
|
|
2019-06-29 19:53:18 +03:00
|
|
|
@dp.message_handler(commands=["terms"])
|
2017-10-23 16:24:08 +03:00
|
|
|
async def cmd_terms(message: types.Message):
|
2019-06-29 19:53:18 +03:00
|
|
|
await bot.send_message(
|
|
|
|
|
message.chat.id,
|
|
|
|
|
"Thank you for shopping with our demo bot. We hope you like your new time machine!\n"
|
|
|
|
|
"1. If your time machine was not delivered on time, please rethink your concept of time"
|
|
|
|
|
" and try again.\n"
|
|
|
|
|
"2. If you find that your time machine is not working, kindly contact our future service"
|
|
|
|
|
" workshops on Trappist-1e. They will be accessible anywhere between"
|
|
|
|
|
" May 2075 and November 4000 C.E.\n"
|
|
|
|
|
"3. If you would like a refund, kindly apply for one yesterday and we will have sent it"
|
|
|
|
|
" to you immediately.",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dp.message_handler(commands=["buy"])
|
2017-10-23 16:24:08 +03:00
|
|
|
async def cmd_buy(message: types.Message):
|
2019-06-29 19:53:18 +03:00
|
|
|
await bot.send_message(
|
|
|
|
|
message.chat.id,
|
|
|
|
|
"Real cards won't work with me, no money will be debited from your account."
|
|
|
|
|
" Use this test card number to pay for your Time Machine: `4242 4242 4242 4242`"
|
|
|
|
|
"\n\nThis is your demo invoice:",
|
|
|
|
|
parse_mode="Markdown",
|
|
|
|
|
)
|
|
|
|
|
await bot.send_invoice(
|
|
|
|
|
message.chat.id,
|
|
|
|
|
title="Working Time Machine",
|
|
|
|
|
description="Want to visit your great-great-great-grandparents?"
|
|
|
|
|
" Make a fortune at the races?"
|
|
|
|
|
" Shake hands with Hammurabi and take a stroll in the Hanging Gardens?"
|
|
|
|
|
" Order our Working Time Machine today!",
|
|
|
|
|
provider_token=PAYMENTS_PROVIDER_TOKEN,
|
|
|
|
|
currency="usd",
|
|
|
|
|
photo_url="https://images.fineartamerica.com/images-medium-large/2-the-time-machine-dmitriy-khristenko.jpg",
|
|
|
|
|
photo_height=512, # !=0/None or picture won't be shown
|
|
|
|
|
photo_width=512,
|
|
|
|
|
photo_size=512,
|
|
|
|
|
is_flexible=True, # True If you need to set up Shipping Fee
|
|
|
|
|
prices=prices,
|
|
|
|
|
start_parameter="time-machine-example",
|
|
|
|
|
payload="HAPPY FRIDAYS COUPON",
|
|
|
|
|
)
|
2017-10-23 16:24:08 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@dp.shipping_query_handler(func=lambda query: True)
|
|
|
|
|
async def shipping(shipping_query: types.ShippingQuery):
|
2019-06-29 19:53:18 +03:00
|
|
|
await bot.answer_shipping_query(
|
|
|
|
|
shipping_query.id,
|
|
|
|
|
ok=True,
|
|
|
|
|
shipping_options=shipping_options,
|
|
|
|
|
error_message="Oh, seems like our Dog couriers are having a lunch right now."
|
|
|
|
|
" Try again later!",
|
|
|
|
|
)
|
2017-10-23 16:24:08 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@dp.pre_checkout_query_handler(func=lambda query: True)
|
|
|
|
|
async def checkout(pre_checkout_query: types.PreCheckoutQuery):
|
2019-06-29 19:53:18 +03:00
|
|
|
await bot.answer_pre_checkout_query(
|
|
|
|
|
pre_checkout_query.id,
|
|
|
|
|
ok=True,
|
|
|
|
|
error_message="Aliens tried to steal your card's CVV,"
|
|
|
|
|
" but we successfully protected your credentials,"
|
|
|
|
|
" try to pay again in a few minutes, we need a small rest.",
|
|
|
|
|
)
|
2017-10-23 16:24:08 +03:00
|
|
|
|
|
|
|
|
|
2018-08-13 23:53:37 +03:00
|
|
|
@dp.message_handler(content_types=ContentTypes.SUCCESSFUL_PAYMENT)
|
2017-10-23 16:24:08 +03:00
|
|
|
async def got_payment(message: types.Message):
|
2019-06-29 19:53:18 +03:00
|
|
|
await bot.send_message(
|
|
|
|
|
message.chat.id,
|
|
|
|
|
"Hoooooray! Thanks for payment! We will proceed your order for `{} {}`"
|
|
|
|
|
" as fast as possible! Stay in touch."
|
|
|
|
|
"\n\nUse /buy again to get a Time Machine for your friend!".format(
|
|
|
|
|
message.successful_payment.total_amount / 100, message.successful_payment.currency
|
|
|
|
|
),
|
|
|
|
|
parse_mode="Markdown",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2017-11-21 00:53:53 +02:00
|
|
|
executor.start_polling(dp, loop=loop)
|