Nanfeng

Notes on software development, code, and curious ideas

Sending a Telegram Invoice from Cocos Creator

This example sends an invoice through the Telegram Bot API and then opens the bot chat. You need a bot token, payment-provider token, recipient chat ID, product information, and currency.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
interface InvoiceData {
chat_id: string;
title: string;
description: string;
payload: string;
provider_token: string;
start_parameter: string;
currency: string;
prices: string;
}

const apiUrl = `https://api.telegram.org/bot${BOT_TOKEN}/sendInvoice`;
const invoice: InvoiceData = {
chat_id: CHAT_ID,
title: 'Product Title',
description: 'Product Description',
payload: 'UniquePayload',
provider_token: PROVIDER_TOKEN,
start_parameter: 'start',
currency: 'USD',
prices: JSON.stringify([{ label: 'Product', amount: 1000 }]),
};

async function openBot() {
const response = await fetch(apiUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(invoice),
});
const data = await response.json();
if (data.ok) window.location.href = `https://t.me/${BOT_USERNAME}`;
else console.error('Failed to send invoice:', data);
}

Open Telegram

Invoice in the bot chat

Do not ship a bot token in public client code. In production, send invoice requests through your own authenticated server.

+