RefferalLib
Use this Lib for referral tracking.

Getting started

Basic function is track. Prefer to call it on /start:
Libs.ReferralLib.track(trackOptions);
params trackOptions - it is object with callback functions for:
Callback function
Description
onTouchOwnLink()
user touch own ref link
onAlreadyAttracted()
user already attracted
onAttracted(byUser)
user was attracted by other user byUser - it is common user data (fields: nickname, first_name and etc)
See @DemoReferalTrackingBot for details

Functions

​

Get Referral link for current user

Libs.ReferralLib.getLink();
will generate link kind http://t.me/botname?start=userUSER_ID
Also you can pass other bot name and change prefix from ""user. For example - it is link for current bot weithout prefix "user":
Libs.ReferralLib.getRefLink(bot.name, "");
will generate link kind http://t.me/botname?start=USER_ID

​

Get attractor for current user

Libs.ReferralLib.getAttractedBy()
return attractor user data
​
​

Get refList

Libs.ReferralLib.getRefList();
return list with attracted users.
​
Or get for Ref List for another user:
Libs.ReferralLib.getRefList(another_user_id);
This method return users list. You can paginate it, sort, recount it and etc.
then code for /reflist can be:
1
let refList = Libs.ReferralLib.getRefList();
2
​
3
if (!refList.exist) {
4
Bot.sendMessage("No any affiliated users")
5
return
6
}
7
​
8
let users_rows = ""
9
​
10
// only 100 first users here
11
// for other users you need use pagination:
12
// https://help.bots.business/bjs/lists#paginating
13
let users = refList.getUsers();
14
​
15
for (var ind in users) {
16
users_rows = users_rows + "\nπŸ‘€ " + Libs.commonLib.getLinkFor( users[ind] )
17
}
18
​
19
let msg =
20
"*Total users:* " +
21
Libs.ReferralLib.getRefCount() +
22
"\n _the first user was tracked:_ \n" +
23
" _" +
24
refList.created_at +
25
"_" +
26
"\n----" +
27
users_rows
28
29
Bot.sendMessage(msg);
Copied!
​

Get refferals count

Libs.ReferralLib.getRefCount()
or for another user:
Libs.ReferralLib.getRefCount(another_user_id)
​

Get Top Refferal List

Libs.ReferralLib.getTopList()
1
// It is just List
2
// you can order, paginate it!
3
// https://help.bots.business/bjs/lists#getting-data
4
let list = Libs.ReferralLib.getTopList();
5
​
6
list.order_by = "integer_value";
7
// olso it is possible get newest members:
8
list.order_ascending = false;
9
​
10
var items = list.get();
11
//Bot.inspect(items);
12
​
13
var msg = 'Top list: ';
14
var prop;
15
for(var ind in items){
16
prop = items[ind]
17
msg = msg + "\n" +
18
String( parseInt(ind) + 1 ) + ". " +
19
Libs.commonLib.getLinkFor(prop.user) + ": πŸ‘¨" +
20
String(prop.value)
21
}
22
​
23
Bot.sendMessage(msg);
Copied!

How to

Q: How to give bonus to user for attracted friend?
Answer:
We can use ResourcesLib for this.
on /start
1
function onAttracted(refUser){
2
// access to Bonus Res of refUser
3
let refUserBonus = Libs.ResourcesLib.anotherUserRes("money", refUser.telegramid);
4
refUserBonus.add(100); // add 100 bonus for friend
5
}
6
​
7
Libs.ReferralLib.track({
8
onAttracted: onAttracted
9
});
Copied!
​
Q: how to give to referrer 5% of referral user deposit?
Answer:
  1. 1.
    You need setup track in first
  2. 2.
    Seems you need use ResourcesLib​
  3. 3.
    On user set balance:
1
let res = Libs.ResourcesLib.userRes("money");
2
let referrer = Libs.ReferralLib.getAttractedBy();
3
​
4
// if current user was attracted by referrer
5
if(referrer){
6
let referrerRes = Libs.ResourcesLib.anotherUserRes(
7
"money", referrer.telegramid);
8
9
let amount = res.value * 0.05; // it is 5%
10
referrerRes.takeFromAnother(res, amount);
11
}
Copied!
In this example we use userRes. Also it is possible use chatRes. See ResourcesLib for details