ForcePilot/backend/package/yuxi/channel/extensions/tencent_im/usersig.py
Kris 331daa1216 feat(channel): 添加腾讯 IM 渠道扩展
新增腾讯 IM(Tencent IM)渠道扩展,支持在 Yuxi 平台中集成腾讯即时通讯 IM 渠道。

包含以下功能模块:
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- webhook: Webhook 事件处理
- outbound: 外发消息管理
- pairing: 用户配对与绑定
- security: 安全校验
- signature: 请求签名验证
- usersig: UserSig 生成
- dedupe: 消息去重
- status: 会话状态管理
- group: 群组管理
- types: 类型定义
2026-05-21 11:49:29 +08:00

37 lines
886 B
Python

import base64
import hashlib
import hmac
import json
import time
import zlib
def gen_user_sig(
sdk_appid: int,
secret_key: str,
userid: str,
expire: int = 180 * 86400,
) -> str:
curr_time = int(time.time())
sig_doc = {
"TLS.ver": "2.0",
"TLS.identifier": str(userid),
"TLS.sdkappid": sdk_appid,
"TLS.expire": expire,
"TLS.time": curr_time,
}
sig_bytes = json.dumps(sig_doc, separators=(",", ":")).encode("utf-8")
compressed = zlib.compress(sig_bytes)
base64_userbuf = base64.urlsafe_b64encode(compressed).rstrip(b"=").decode("utf-8")
signature = hmac.new(
secret_key.encode("utf-8"),
base64_userbuf.encode("utf-8"),
hashlib.sha256,
).digest()
base64_sig = base64.urlsafe_b64encode(signature).rstrip(b"=").decode("utf-8")
return f"{base64_sig}{base64_userbuf}"