Payments 2.0 - The Telegram messenger now has the ability to accept payments in any chat, including groups and channels. You can connect your store to the platform on your own, without agreement with Telegram.
And in this tutorial, we will create a simple bot with Node.js on the Telegraf.js framework that can accept money from a client and send it to your account through Sberbank.
Payments 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.
: