Payments 2.0 -Telegramメッセンジャーは、グループやチャネルを含むすべてのチャットで支払いを受け入れることができるようになりました。Telegramに同意しなくても、自分でストアをプラットフォームに接続できます。
このチュートリアルでは、Telegraf.jsフレームワーク上にNode.jsを使用して単純なボットを作成します。このボットは、クライアントからお金を受け取り、Sberbankを介してアカウントに送金できます。
支払い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.
: