Webhooks¶
Caution
To correctly run the webhook you need a VPS server or configure the local machine for response to requests of QIWI API.
Caution
Firstly, you need to register webhook to your server using glQiwiApi
🐺So, to “bind” webhook and gracefully work with glQiwiApi webhook API, you need to follow this instructions:
Step one(bind webhook).
import asyncio
from glQiwiApi import QiwiWrapper
from glQiwiApi.ext.url_builder import WebhookURL
wrapper = QiwiWrapper(
api_access_token="",
secret_p2p="",
phone_number="+"
)
async def main():
# some tips:
# - webhook_path have to starts with "/"
url = WebhookURL.create(host="https://github.com", webhook_path="/GLEF1X/glQiwiApi") # equal to https://github.com/GLEF1X/glQiwiApi
# Also, you can pass url as string, but it's highly recomended to use WebhookURL extension
await wrapper.bind_webhook(url=url, delete_old=True)
asyncio.run(main())
Step two(use handlers and start webhook).
Using glQiwiApi you can use two types of handlers transaction_handler
and bill_handler
respectively.
Also, you can pass lambda filters to miss unnecessary updates from webhook.
1import logging
2
3from aiogram import Bot
4
5from glQiwiApi import QiwiWrapper, types
6from glQiwiApi.core.dispatcher.webhooks.config import Path
7from glQiwiApi.utils import executor
8
9TOKEN = "token from https://qiwi.com/api/"
10QIWI_SECRET = "secret token from https://qiwi.com/p2p-admin/"
11
12wallet = QiwiWrapper(api_access_token=TOKEN, secret_p2p=QIWI_SECRET)
13
14bot = Bot(token="BOT_TOKEN")
15
16logger = logging.getLogger(__name__)
17
18
19# There is a lambda expression for "cutting off" test payments
20@wallet.transaction_handler(lambda event: event.payment is not None)
21async def main(event: types.WebHook):
22 logger.info("New transaction: {}", event)
23 await bot.send_message(chat_id="1219185039", text=event.hook_id)
24
25
26@wallet.bill_handler()
27async def main2(event: types.Notification):
28 logger.info("P2P EVENT {}", event)
29
30
31# Also, you can specify a path for webhook
32# Example: http://127.0.0.1/your_path/
33# If you don't pass path in `start_webhook`
34# or dont pass on transaction_path or bill_path
35# Its ok, because it will take a default paths
36# default transaction_path = /dispatcher/qiwi/
37# default bill_path = /webhooks/qiwi/bills/
38# So, if you dont pass on paths
39# you need to register webhook with url like
40# on this example: http://your_ip:port/web_hooks/qiwi/ - for transactions
41# or http://your_ip:port/webhooks/qiwi/bills/ - for bills
42path = Path(transaction_path="/dispatcher/qiwi", bill_path="/my_webhook/")
43
44executor.start_webhook(
45 wallet,
46 # You can pass on any port, but it must be open for web
47 # You can use any VPS server to catching webhook or
48 # your configured local machine
49 path=path,
50)