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

如何通过Telegram聊天ID生成群组邀请链接?Telethon按钮绑定方法

生成Telegram群组邀请链接及Telethon绑定按钮方案

一、生成群组邀请链接

1. 手动生成(客户端操作)

  • 打开目标Telegram群组,点击右上角「⋮」→「管理群组」→「链接」
  • 点击「创建链接」,可按需设置链接有效期、最大加入人数等限制,生成后直接复制即可

2. 使用Telethon代码生成

确保已完成Telethon客户端认证,执行以下代码生成链接:

from telethon import TelegramClient
from telethon.tl.functions.messages import CreateChatInviteRequest

# 替换为你的API信息和会话文件
api_id = 123456
api_hash = 'your_api_hash_here'
client = TelegramClient('your_session', api_id, api_hash)

async def generate_invite_link(chat_id):
    async with client:
        # 可添加expire_date(Unix时间戳)、usage_limit(整数)设置链接限制
        result = await client(CreateChatInviteRequest(
            peer=chat_id,
            expire_date=None,
            usage_limit=None
        ))
        print(f"生成的邀请链接:{result.link}")
        return result.link

# chat_id支持群组用户名(如'@mygroup')或数字ID
client.loop.run_until_complete(generate_invite_link('@your_group_username'))

二、Telethon绑定跳转按钮的方式

通过Telethon发送带内联键盘按钮的消息,直接将按钮绑定到邀请链接,示例代码如下:

from telethon import TelegramClient
from telethon.tl.types import InlineKeyboardMarkup, InlineKeyboardButton

api_id = 123456
api_hash = 'your_api_hash_here'
client = TelegramClient('your_session', api_id, api_hash)

async def send_message_with_button(chat_id, invite_link):
    async with client:
        # 构建内联键盘,按钮点击跳转至邀请链接
        keyboard = InlineKeyboardMarkup([
            [InlineKeyboardButton(text='加入群组', url=invite_link)]
        ])
        # 发送带按钮的消息
        await client.send_message(
            chat_id=chat_id,
            message='点击下方按钮加入我们的群组',
            reply_markup=keyboard
        )

# 替换为目标聊天ID和生成的邀请链接
client.loop.run_until_complete(send_message_with_button('@target_chat', 'your_invite_link_here'))

补充说明

  • 若使用机器人账号发送按钮消息,需确保机器人已加入目标群组并拥有发消息权限
  • 如需生成有限制的链接,可在CreateChatInviteRequest中添加expire_date(指定有效期)和usage_limit(限制加入人数)参数

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 05:31:38