You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Telegram新成员加入事件触发及引流跳转机器人开发工作流咨询

Hey there! Let's walk through exactly how to build this Telegram bot workflow since you're new to the platform—this is totally achievable with the Telegram Bot API and some basic backend code. Here's a step-by-step breakdown tailored to your needs:

Step 1: Set Up Your Telegram Bot First

Before anything else, you need a bot to handle events and interactions:

  • Head over to @BotFather on Telegram (it's the official bot for creating/managing bots). Send /newbot and follow the prompts to name your bot and set its username. Save the API token it gives you—this is your bot's "password" for talking to Telegram's API.
  • Add your new bot to your target Telegram group. Once it's in, go to the group's settings → Administrators → add your bot, and grant it at least these permissions: Manage chat and Read message history. Without these, the bot won't be able to detect when new members join.

Telegram uses deep links to direct users straight to chats/groups while passing extra data to bots. For your website, use this link format:

https://t.me/your_group_username?start=website_visitor
  • Replace your_group_username with your group's actual handle (the one starting with @, like @MyCoolGroup).
  • The start=website_visitor part is a marker—when users click this link, they'll be taken to your group's join page, and if they interact with the bot (which happens automatically with the deep link), your bot can recognize they came from your website.
  • On your website, add a clear button or banner pointing to this link. When users click it, Telegram will open (or prompt them to install it if they don't have it) and take them directly to your group.
Step 3: Detect New Members & Send the Redirect URL

The core part is making the bot trigger the URL when a new member joins. Telegram sends chat_member updates to your bot whenever someone joins/leaves a group. You can handle these with either long polling (great for testing) or webhooks (better for production).

Option 1: Long Polling (Quick & Easy for Testing)

Long polling means your bot repeatedly checks Telegram for new updates. Here's a simplified Python example using the python-telegram-bot library:
First, install the library (stick to this stable version for reliable chat_member support):

pip install python-telegram-bot==13.7

Then the code:

from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, ChatMemberHandler, CallbackContext

# Replace these with your own values
TARGET_URL = "https://your-specified-url.com"
BOT_TOKEN = "your_bot_api_token_here"
# Optional: Use a simple database (like SQLite) to track website visitors
# For this example, we'll use a set to store user IDs temporarily
website_visitors = set()

def handle_start(update: Update, context: CallbackContext) -> None:
    # This runs when a user clicks your website's deep link
    user_id = update.effective_user.id
    website_visitors.add(user_id)
    # You can send a quick welcome message here if you want
    update.message.reply_text("Thanks for checking out our group! We'll send you a link once you join.")

def check_new_member(update: Update, context: CallbackContext) -> None:
    chat_member_update = update.chat_member
    # Check if the user just joined the group
    if chat_member_update.new_chat_member.status == "member":
        user = chat_member_update.new_chat_member.user
        # Only send the link if they came from your website
        if user.id in website_visitors:
            # Create an inline button for better UX (users can click directly)
            keyboard = [[InlineKeyboardButton("Continue to Our Page", url=TARGET_URL)]]
            reply_markup = InlineKeyboardMarkup(keyboard)
            
            context.bot.send_message(
                chat_id=user.id,
                text="Welcome to the group! Click the button below to continue:",
                reply_markup=reply_markup
            )
            # Remove them from the set so we don't send the link again if they rejoin
            website_visitors.discard(user.id)

def main() -> None:
    updater = Updater(BOT_TOKEN)
    dp = updater.dispatcher
    # Handle the /start command triggered by the deep link
    dp.add_handler(CommandHandler("start", handle_start))
    # Handle new member joins
    dp.add_handler(ChatMemberHandler(check_new_member, ChatMemberHandler.CHAT_MEMBER))
    # Start polling Telegram for updates
    updater.start_polling()
    updater.idle()

if __name__ == "__main__":
    main()

Option 2: Webhooks (Better for Production)

Webhooks are more efficient—instead of your bot checking Telegram, Telegram sends updates directly to a public HTTPS URL of your backend. You'll need a server with a public HTTPS address (services like Heroku, AWS EC2, or even ngrok for temporary testing work). The logic for handling new members is identical to the example above; you just need to configure the webhook instead of starting polling.

Key Tips to Keep in Mind
  • Telegram Security Rules: You can't force a user to open a URL automatically—you have to send them a message with a link or inline button they choose to click. This is a non-negotiable security measure from Telegram.
  • Private vs Public Groups: If your group is private, users will need to request to join, and an admin has to approve them. Your bot will only detect the join after approval, so the link will be sent then.
  • Persistence for Tracking: In the example above, we use a set to track website visitors, but this will reset when the bot restarts. For production, use a database (like SQLite, PostgreSQL, or even Firebase) to store user IDs permanently.
  • Test Everything: Always test the flow end-to-end—click your website link, join the group, and make sure the bot sends the link as expected. Double-check bot permissions if updates aren't coming through.

内容的提问来源于stack exchange,提问作者Tripti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:18:21