GuidesTelegram bot
Accept KHQR payments in a Telegram bot
To take KHQR payments in a Telegram bot, create a Dalo payment when the customer orders, send the QR as a photo with a "Pay" button that opens the checkout, and message the customer when Dalo's signed payment.completed webhook arrives. Store the chat id in the payment's metadata so the webhook knows whom to tell. The example uses Node.js and grammY.
Why a button and a photo
Most of your customers are on their phone, inside Telegram, so they cannot scan a QR on their own screen. The Pay button opens Dalo's checkout, which has "Open in ABA Mobile" and "Save QR" for paying on the same phone. The photo is for a customer who pays from a second phone, or at the counter.
1. Setup
Create a bot with @BotFather, a store and a test key at dalodesk.com/app, and a webhook endpoint pointing at https://your-server/webhooks/dalo.
npm install grammy qrcode express
export BOT_TOKEN=123456:ABC...
export DALO_API_KEY=dk_test_...
export DALO_WEBHOOK_SECRET=whsec_...
node bot.js
2. The bot
// bot.js (Node 18+, "type": "module"): npm install grammy qrcode express
import { Bot, InlineKeyboard, InputFile } from "grammy";
import QRCode from "qrcode";
import express from "express";
import crypto from "node:crypto";
const DALO = "https://dalodesk.com";
const { BOT_TOKEN, DALO_API_KEY, DALO_WEBHOOK_SECRET } = process.env;
const bot = new Bot(BOT_TOKEN);
// /buy: create a KHQR payment for this chat and send it as a photo with a "Pay" button.
bot.command("buy", async ctx => {
const orderId = `order_${Date.now()}`;
const response = await fetch(`${DALO}/v1/payments`, {
method: "POST",
headers: { Authorization: `Bearer ${DALO_API_KEY}`, "Content-Type": "application/json", "Idempotency-Key": `${orderId}_1` },
body: JSON.stringify({ amount: "2.50", reference_id: orderId, metadata: { chat_id: ctx.chat.id } }),
signal: AbortSignal.timeout(20000),
});
const payment = await response.json();
if (!response.ok) return ctx.reply("Sorry, the payment could not start. Please try /buy again.");
// Telegram photos cannot be SVG, so draw a PNG from qr_string. Customers on this phone tap the button instead.
const png = await QRCode.toBuffer(payment.qr_string || payment.checkout_url, { width: 640, margin: 2 });
await ctx.replyWithPhoto(new InputFile(png, "khqr.png"), {
caption: `Iced coffee, $${payment.amount}. Scan with any KHQR banking app, or tap Pay on this phone.`,
reply_markup: new InlineKeyboard().url("Pay with ABA / KHQR", payment.checkout_url),
});
});
// Dalo's webhook: when the money arrives, tell the customer in the same chat.
const app = express();
app.post("/webhooks/dalo", express.raw({ type: "application/json" }), async (req, res) => {
const raw = req.body.toString("utf8");
if (!verifySignature(raw, req.get("X-KHQR-Signature"), DALO_WEBHOOK_SECRET)) return res.sendStatus(401);
const event = JSON.parse(raw);
res.sendStatus(200);
const payment = event.data.payment;
if (event.type === "payment.completed" && event.livemode === true && !payment.test && payment.metadata?.chat_id) {
await bot.api.sendMessage(payment.metadata.chat_id, `Paid, thank you! Order ${payment.reference_id} is confirmed.`);
}
});
function verifySignature(rawBody, header, secret) {
const m = /^t=(\d+),v1=([a-f0-9]{64})$/.exec(header || "");
if (!m || Math.abs(Date.now() / 1000 - Number(m[1])) > 300) return false;
const expected = crypto.createHmac("sha256", secret).update(`${m[1]}.${rawBody}`).digest("hex");
return crypto.timingSafeEqual(Buffer.from(expected, "hex"), Buffer.from(m[2], "hex"));
}
app.listen(3000);
bot.start();
3. Details that matter
- Telegram cannot show SVG photos.
qr_image_urlis usually SVG, so the bot draws a PNG fromqr_stringwith theqrcodepackage. - The QR expires after 5 minutes by default (1 to 30 minutes in store settings, or
expires_inper payment). Onpayment.expired, offer the customer a fresh/buy. - Only
payment.completedwithlivemode: truemeans money arrived. Test events havelivemode: false; to try the full flow in test mode, allow them on your machine only. - The shop owner gets told too. Connect Telegram in the Dalo dashboard and every paid order is announced to the owner, with no code.
4. Test, then go live
Send /buy to your bot, tap Pay, and press Simulate paid on the test checkout. When it works, verify the store in the dashboard and switch to a dk_live_ key.
Questions
Do I need Telegram Payments or a payment provider token?
No. The bot only sends a photo and a link button. Dalo creates the KHQR and tells your server when it is paid.
Can I use Python instead of Node.js?
Yes. The same three steps work with python-telegram-bot or aiogram: create the payment with requests, send the PNG with send_photo and an InlineKeyboardButton url, and verify the webhook as in the Django guide.
Where do I host the bot?
Anywhere that can receive an HTTPS request for the webhook: a small VPS, Railway, Fly.io or a home server behind a tunnel.
Get a test key in two minutes.
Sign in with your email, create a store in test mode and copy a dk_test_ key. No merchant application, no paperwork. Free up to 50 sales a month.
More guidesKHQR payment APILaravelNode.jsNext.jsDjangoTest modeCheck payment statusFlask and FastAPIStatic websiteChoosing an API