- 登录Telegram并加入你创建的群组。
- 打开BotFather并创建一个新的bot并获取其API token。
- 使用Telegram API的Python库pyTelegramBotAPI创建一个bot并将其添加到你的群组中。
- 创建一个新的消息处理器函数来处理所有通过链接加入群组的新用户。
- 创建一个命令处理器函数来响应你的查询请求。
下面是一个代码示例:
import telebot
TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN'
bot = telebot.TeleBot(TOKEN)
@bot.message_handler(commands=['start'])
def send_welcome(message):
bot.reply_to(message, "Welcome to my bot! Use /count_users command to get the number of users who joined through your invite link.")
@bot.message_handler(commands=['count_users'])
def count_users(message):
chat_id = message.chat.id
link = bot.export_chat_invite_link(chat_id)
users = bot.get_chat_members_count(chat_id)
bot.send_message(chat_id, f"{users} users have joined through invite link: {link}")
@bot.message_handler(func=lambda m: True, content_types=['new_chat_members'])
def on_new_member(message):
chat_id = message.chat.id
users = bot.get_chat_members_count(chat_id)
bot.send_message(chat_id, f"Welcome to the group, {message.new_chat_members[0].first_name}! Now we have {users} members.")
bot.polling()
你只需要替换YOUR_TELEGRAM_BOT_TOKEN为你的bot API token,在命令行运行代码,然后使用/count_users命令获取群组中通过你的邀请链接加入的用户数量。