Hello everyone! Surely you have seen in various bots a referral link like https://t.me/ <username_bot>? Start = <number>. Usually, the Telegram ID of the referrer is indicated as a number. In this article, I will explain how to handle such links in my bot.
To develop bots, I use the pytelegrambotapi Python library.
I will omit the process of creating a bot via @BotFather , I will proceed directly to the code. There is a file config.py
with the TOKEN variable, which stores the bot's token. bot.py
We will "chemize" the file . Let's write a handler for the / start command and display everything that is stored in the message object.
import telebot
import config
bot = telebot.TeleBot(config.TOKEN)
@bot.message_handler(commands=["start"])
def start_command_handler(msg):
print(msg)
if __name__ == '__main__':
bot.polling(none_stop=True)
'text': '/start'.
https://t.me/<_>?start=test 10- print(msg.text)
/start test
. , msg.text
, .
. , /start , /start
. ( /start test
). ID .
@bot.message_handler(commands=["start"])
def start_command_handler(msg):
user_id = msg.from_user.id
referrer = None
if " " in msg.text:
referrer_candidate = msg.text.split()[1]
#
https://t.me/<_>?start=test test2, test2 msg.text, , , test2 . msg.text
. , . (msg.text.split()
), ( , , ?).
, . , , . - "" , ID - . .
@bot.message_handler(commands=["start"])
def start_command_handler(msg):
user_id = msg.from_user.id
referrer = None
# -
if " " in msg.text:
referrer_candidate = msg.text.split()[1]
#
try:
referrer_candidate = int(referrer_candidate)
#
except ValueError:
pass
, try... except
int()
. referer
, int()
ValueError
referer . : int()
"-101" -101. referer . , .
TG ID. , . . ID , user_id = msg.from_user.id
@bot.message_handler(commands=["start"])
def start_command_handler(msg):
user_id = msg.from_user.id
referrer = None
# -
if " " in msg.text:
referrer_candidate = msg.text.split()[1]
#
try:
referrer_candidate = int(referrer_candidate)
# TG ID TG ID
if user_id != referrer_candidate:
#
pass
except ValueError:
pass
, get_all_users(), . , , .
@bot.message_handler(commands=["start"])
def start_command_handler(msg):
user_id = msg.from_user.id
referrer = None
# -
if " " in msg.text:
referrer_candidate = msg.text.split()[1]
#
try:
referrer_candidate = int(referrer_candidate)
# TG ID TG ID
# ,
if user_id != referrer_candidate and referrer_candidate in get_all_users():
referer = referrer_candidate
except ValueError:
pass
What should be done if the user already has a referrer? After all, we will not give out a bonus for the referred referral every time you use a referral link, it will simply ruin us. Therefore it is necessary to write a check. Let's write a function has_referrer()
that will return True if the user has a referrer and False if it doesn't. It is logical to write it at the beginning for optimization.
@bot.message_handler(commands=["start"])
def start_command_handler(msg):
user_id = msg.from_user.id
#
if not has_referrer():
referrer = None
# -
if " " in msg.text:
referrer_candidate = msg.text.split()[1]
#
try:
referrer_candidate = int(referrer_candidate)
# TG ID TG ID
# ,
if user_id != referrer_candidate and referrer_candidate in get_all_users():
referer = referrer_candidate
except ValueError:
pass
Hope you find this article helpful!