Skip to content

消息段 API

消息段(Segment)是 QQ 官方机器人 SDK 中用于构建富媒体消息的核心概念。通过组合不同类型的消息段,您可以发送包含文本、图片、视频、音频、表情等多种内容的复杂消息。

📋 目录

🎯 基础概念

消息段结构

每个消息段都遵循统一的数据结构:

typescript
interface MessageElem {
    type: string        // 消息段类型
    [key: string]: any  // 消息段数据
}

发送消息类型

typescript
// 可发送的内容类型
type Sendable = string | MessageElem | (string | MessageElem)[]

// 可引用的消息(用于回复)
interface Quotable {
    id?: string         // 消息 ID
    event_id?: string   // 事件 ID
}

📝 类型定义

可重复组合的消息元素

这些消息段可以在同一条消息中多次使用:

typescript
type RepeatableCombineElem = 
    | TextElem      // 文本消息段
    | FaceElem      // 表情消息段
    | LinkElem      // 链接消息段(仅频道支持)
    | AtElem        // @提及消息段
    | ButtonElem    // 按钮消息段

独立消息元素

这些消息段通常独立使用,不与其他同类型消息段组合:

typescript
type IndependentElem = 
    | ImageElem     // 图片消息段
    | VideoElem     // 视频消息段
    | AudioElem     // 音频消息段
    | MDElem        // Markdown 消息段
    | ArkElem       // Ark 卡片消息段
    | EmbedElem     // Embed 嵌入消息段

带回复的消息元素

typescript
type WithReply<T extends MessageElem> =
    | T
    | [T]
    | [ReplyElem, T]
    | [ReplyElem, ...RepeatableCombineElem[]]

完整的 Sendable 定义

typescript
export type Sendable =
    | string                                    // 纯文本
    | RepeatableCombineElem                    // 单个可重复元素
    | (RepeatableCombineElem | string)[]       // 可重复元素数组
    | WithReply<IndependentElem>               // 带回复的独立元素

📐 组合规则

1. 可重复消息段

  • ✅ 可以多个同时使用
  • ✅ 可以与其他可重复消息段组合
  • ✅ 可以添加回复引用

包括:

2. 独立消息段

  • ⚠️ 每条消息只能包含一个
  • ✅ 可以添加回复引用
  • ⚠️ 不能与其他独立消息段组合

包括:

3. 特殊消息段

💡 使用示例

基础文本消息

typescript
// 发送纯文本
await bot.sendMessage(channelId, "这是一条文本消息")

// 发送文本消息段
await bot.sendMessage(channelId, {
    type: "text",
    text: "这是一条文本消息"
})

组合消息

typescript
// 文本 + 表情 + @提及
await bot.sendMessage(channelId, [
    { type: "text", text: "你好 " },
    { type: "at", user_id: "123456" },
    { type: "text", text: " " },
    { type: "face", id: "1" }
])

带回复的消息

typescript
// 回复 + 图片
await bot.sendMessage(channelId, [
    { type: "reply", message_id: "原消息ID" },
    { type: "image", file: "https://example.com/image.jpg" }
])

服务模块使用

typescript
// 使用消息服务发送
const result = await bot.messageService.sendGuildMessage(
    channelId,
    [
        { type: "text", text: "查看这张图片:" },
        { type: "image", file: "local_image.jpg" }
    ]
)

// 使用消息构建器
const message = bot.messageBuilder
    .text("欢迎 ")
    .at(userId)
    .text(" 加入频道!")
    .face("1")
    .build()

await bot.sendMessage(channelId, message)

📚 消息段列表

类型说明可重复支持平台文档链接
text文本消息全部文档
face表情消息全部文档
at@提及消息全部文档
link链接消息频道文档
button按钮消息全部文档
image图片消息全部文档
video视频消息全部文档
audio音频消息全部文档
markdownMarkdown频道文档
arkArk 卡片频道文档
embed嵌入消息频道文档
reply回复引用特殊全部文档

⚡ 快速开始

1. 安装和导入

typescript
import { Bot, MessageBuilder } from 'qq-official-bot'

const bot = new Bot({
    appid: 'your_app_id',
    secret: 'your_secret',
    mode: 'websocket'
})

2. 发送基础消息

typescript
// 文本消息
await bot.sendMessage(channelId, "Hello, World!")

// 图片消息
await bot.sendMessage(channelId, {
    type: "image",
    file: "https://example.com/image.jpg"
})

3. 发送复合消息

typescript
await bot.sendMessage(channelId, [
    { type: "text", text: "欢迎新成员 " },
    { type: "at", user_id: newMemberId },
    { type: "text", text: "!" },
    { type: "face", id: "1" }
])

4. 使用消息构建器

typescript
const message = new MessageBuilder()
    .text("这是一条包含多种元素的消息:")
    .image("image.jpg")
    .reply(sourceMessage)
    .build()

await bot.sendMessage(channelId, message)

🔧 高级功能

消息模板

typescript
// 创建消息模板
function createWelcomeMessage(userId: string, userName: string) {
    return [
        { type: "text", text: `欢迎 ` },
        { type: "at", user_id: userId },
        { type: "text", text: ` (${userName}) 加入频道!` },
        { type: "face", id: "1" }
    ]
}

// 使用模板
await bot.sendMessage(channelId, createWelcomeMessage("123", "张三"))

条件消息

typescript
function createConditionalMessage(hasImage: boolean, text: string, imageUrl?: string) {
    const elements = [{ type: "text", text }]
    
    if (hasImage && imageUrl) {
        elements.push({ type: "image", file: imageUrl })
    }
    
    return elements
}

🚨 注意事项

  1. 类型安全: 使用 TypeScript 时,IDE 会提供智能提示和类型检查
  2. 平台限制: 某些消息段仅在特定平台(如频道)中支持
  3. 组合规则: 严格遵循消息段组合规则,避免发送失败
  4. 文件处理: 图片、视频、音频支持本地文件、URL 和 Base64 格式
  5. 权限要求: 某些消息类型需要特定的机器人权限

📖 更多资源


💡 提示: 消息段设计遵循类型安全原则,能够在编译时发现大部分消息构建错误,提升开发体验。