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

基于Pyrogram开发专属带文本贴纸Telegram Bot的实现指南咨询

Telegram 自定义贴纸权限控制开发指南

核心功能拆解

要实现你需要的权限控制逻辑,核心要完成三个模块:

  • 存储自定义贴纸时绑定专属权限标识(你的用户ID、秘密指令)
  • 处理贴纸使用请求时做权限校验
  • 提供基础的权限管理操作(比如更新秘密指令、新增允许用户)

具体实现步骤

1. 数据存储设计

用轻量型数据库(比如SQLite,小体量场景足够)存储贴纸和权限信息,表结构示例:

CREATE TABLE custom_stickers (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    sticker_file_id TEXT NOT NULL, -- Telegram返回的贴纸唯一标识
    sticker_name TEXT NOT NULL, -- 自定义贴纸名称,用于调用
    uploader_id INTEGER NOT NULL, -- 上传者ID(你的ID)
    allowed_user_ids TEXT NOT NULL, -- 用JSON存储允许使用的用户ID数组,比如 "[123456789]"
    secret_code TEXT -- 秘密指令,可为空,留空则仅允许指定ID使用
);

2. 贴纸上传逻辑

仅允许你自己上传贴纸,上传时绑定初始权限:

  • 校验上传者ID是否为你的用户ID,非本人直接拒绝
  • 接收贴纸文件,获取Telegram返回的file_id
  • 可选接收你设置的秘密指令,存入数据库

示例伪代码(基于Python的python-telegram-bot库):

from telegram import Update
from telegram.ext import ContextTypes
import json

# 替换成你的用户ID
YOUR_USER_ID = 123456789

async def upload_sticker(update: Update, context: ContextTypes.DEFAULT_TYPE):
    # 校验上传权限
    if update.effective_user.id != YOUR_USER_ID:
        await update.message.reply_text("无上传权限")
        return
    
    sticker = update.message.sticker
    if not sticker:
        await update.message.reply_text("请发送贴纸文件")
        return
    
    # 处理秘密指令参数
    secret_code = context.args[0] if context.args else None
    # 初始允许用户仅包含你自己
    allowed_ids = json.dumps([YOUR_USER_ID])
    
    # 写入数据库
    db.execute(
        "INSERT INTO custom_stickers (sticker_file_id, sticker_name, uploader_id, allowed_user_ids, secret_code) VALUES (?, ?, ?, ?, ?)",
        (sticker.file_id, sticker.set_name, update.effective_user.id, allowed_ids, secret_code)
    )
    db.commit()
    await update.message.reply_text("贴纸上传成功")

3. 权限校验与贴纸调用逻辑

用户请求使用贴纸时,先做权限校验:

  • 检查用户ID是否在allowed_user_ids列表中,是则直接发送贴纸
  • 若不在,检查用户是否提供了正确的secret_code,匹配则发送贴纸
  • 两者都不满足则拒绝请求

示例伪代码:

async def use_custom_sticker(update: Update, context: ContextTypes.DEFAULT_TYPE):
    if len(context.args) < 1:
        await update.message.reply_text("请指定贴纸名称")
        return
    
    sticker_name = context.args[0]
    user_input_secret = context.args[1] if len(context.args) > 1 else None
    
    # 查询贴纸信息
    cursor = db.execute("SELECT allowed_user_ids, secret_code, sticker_file_id FROM custom_stickers WHERE sticker_name = ?", (sticker_name,))
    sticker_data = cursor.fetchone()
    if not sticker_data:
        await update.message.reply_text("该贴纸不存在")
        return
    
    allowed_ids = json.loads(sticker_data[0])
    secret_code = sticker_data[1]
    sticker_file_id = sticker_data[2]
    
    user_id = update.effective_user.id
    # 权限校验
    if user_id in allowed_ids:
        await update.message.reply_sticker(sticker=sticker_file_id)
        return
    if secret_code and user_input_secret == secret_code:
        await update.message.reply_sticker(sticker=sticker_file_id)
        return
    
    await update.message.reply_text("无使用权限")

4. 额外优化建议

  • 添加权限管理指令:允许你新增/删除allowed_user_ids中的用户,或更新secret_code
  • 给秘密指令加使用次数限制,防止泄露后被滥用
  • 敏感操作(比如删除贴纸、修改权限)增加二次确认步骤

技术栈推荐

  • 机器人框架:Python选python-telegram-bot,Node.js选telegraf,都是成熟的Telegram Bot开发工具
  • 数据库:小项目用SQLite,多实例部署场景用PostgreSQL/MySQL

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 11:20:28