Paiements 2.0 - Le messager Telegram a désormais la possibilité d'accepter des paiements dans n'importe quel chat, y compris les groupes et les canaux. Vous pouvez connecter votre boutique à la plateforme par vous-même, sans accord avec Telegram.
Et dans ce tutoriel, nous allons créer un robot simple avec Node.js sur le framework Telegraf.js qui peut accepter de l'argent d'un client et l'envoyer sur votre compte via Sberbank.
Paiements 2.0
Telegram 2017 . , .
8- , .
200 :
Telegram . , . Telegram, .
– Telegram . , , .
Telegram
Telegram @BotFather. /newbot
, , , . , Telegram
sber_pay_test_bot
.
, , BotFather — «» .
! .
Node.js
. .
:
mkdir sber_pay_test_bot && cd sber_pay_test_bot
:
npm init
package.json
, , , , . enter
index.js
.
touch index.js
Telegraf.js
C telegraf.js — .
npm install telegraf@3.38
dotenv
— , .env
process.env.
, nodemon
— , node.js node
.
npm install dotenv nodemon
package.json
"scripts": {
"start": "nodemon index"
}
telegraf.js, .
const { Telegraf } = require('telegraf')
require('dotenv').config()
const bot = new Telegraf(process.env.BOT_TOKEN) // , botFather
bot.start((ctx) => ctx.reply('Welcome')) // /start
bot.help((ctx) => ctx.reply('Send me a sticker')) // /help
bot.on('sticker', (ctx) => ctx.reply('')) //bot.on , ,
bot.hears('hi', (ctx) => ctx.reply('Hey there')) // bot.hears , - "hi"
bot.launch() //
.env
BOT_TOKEN
, @BotFather
BOT_TOKEN=''
npm run start
PROVIDER_TOKEN @SberbankPaymentBot
PROVIDER_TOKEN
merchantLogin
. -.
@BotFather /mybots
, .
Payments
Connect Live
@SberbankPaymentBot
, merchantLogin
, -api
-operator
. : P71XXXXXXX21
.
@BotFather , PROVIDER_TOKEN
.env
PROVIDER_TOKEN='41018XXXX:LIVE:XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
index.js
:
const { Telegraf } = require('telegraf')
require('dotenv').config()
const bot = new Telegraf(process.env.BOT_TOKEN) // , botFather
const getInvoice = (id) => {
const invoice = {
chat_id: id, //
provider_token: process.env.PROVIDER_TOKEN, // @SberbankPaymentBot
start_parameter: 'get_access', // . , «», , . , URL ( ) , .
title: 'InvoiceTitle', // , 1-32
description: 'InvoiceDescription', // , 1-255
currency: 'RUB', // ISO 4217
prices: [{ label: 'Invoice Title', amount: 100 * 100 }], // , JSON 100 * 100 = 100
photo_url: 'https://s3.eu-central-1.wasabisys.com/ghashtag/JavaScriptBot/Unlock.png', // URL -. . , , .
photo_width: 500, //
photo_height: 281, //
payload: { // -, , 1–128 . , .
unique_id: `${id}_${Number(new Date())}`,
provider_token: process.env.PROVIDER_TOKEN
}
}
return invoice
}
bot.use(Telegraf.log())
bot.hears('pay', (ctx) => { . // , - "pay"
return ctx.replyWithInvoice(getInvoice(ctx.from.id)) // replyWithInvoice
})
bot.on('pre_checkout_query', (ctx) => ctx.answerPreCheckoutQuery(true)) //
bot.on('successful_payment', async (ctx, next) => { //
await ctx.reply('SuccessfulPayment')
})
bot.launch()
Telegraf replyWithInvoice telegram.sendInvoice.
. .
yarn start
.
pay
.
JavaScript Bot — JavaScript, React Native, TypeScript.
:
Englishmoji
?
Telegraf
.