Module for creating a referral system for a bot with Telegraf.js
Node.js 12.0.0 or newer is required
yarn add telegraf-referral-system
npm i telegraf-referral-system
WARNING!!! Highly recommended use this module with transactions!!! MongoDB supports transactions starting from version 4 and requires a set of replicas to use transactions, a single server is not suitable. You can use MongoDB atlas
const ReferralsLib = require('telegraf-referral-system')
//..connection to db and etc...
const Referrals = new ReferralsLib({
collectionName: 'referrals', //By default "referrals" (optional)
referralLevels: 3, //By default 3 (optional),
db: dbClient // Connected MongoClient object
});
const session = client.startSession();
const transactionResults = await session.withTransaction(async () => {
//...some user register logic...
await Referrals.createReferral(
'2', // User id
'payload', //Payload or undefined (optional)
'1', //Parent ID, for create withot parent - undefined (optional)
{ session } //MongoDB options
)
});
For getting MongoClient form mongoose, you can do like this:
const mongoose = require('mongoose')
const dbClient = mongoose.connection.getClient().db()