Skip to content

消息段

Seg 模块提供类型安全的消息段构造器,用于构建复杂的 QQ 消息内容。

基本使用

ts
import { Seg } from '@aemeath-projects/napcat'

// 发送组合消息
await msg.sendGroupMsg(groupId, [
  Seg.at(123456),
  Seg.text(' 你好!'),
  Seg.image('http://example.com/img.png'),
])

消息段类型

方法说明
Seg.text(text: string)纯文本
Seg.face(id: number)QQ 表情
Seg.image(file: string, opts?)图片
Seg.record(file: string, opts?)语音
Seg.video(file: string, opts?)视频
Seg.at(qq: number | 'all')@某人(传 'all' @全体成员)
Seg.reply(id: number)引用回复
Seg.forward(id: string)合并转发
Seg.mface(opts)商城表情(NapCat 扩展)
Seg.dice()骰子
Seg.rps()猜拳
Seg.poke(type: string, id: string)戳一戳
Seg.file(file: string, opts?)文件
Seg.json(data: string)JSON 富文本
Seg.music(type: string, id: string)平台音乐分享
Seg.customMusic(url, audio, title, singer?, image?)自定义音乐分享
Seg.node(content, userId?, nickname?)转发消息节点
Seg.contact(type: 'qq' | 'group', id: string)联系人名片
Seg.markdown(content: string)Markdown
Seg.share(url, title, opts?)链接分享
Seg.location(lat, lon, opts?)位置

参数说明

Seg.text

纯文本消息段,最简单的消息类型。

参数类型必填说明
textstring文本内容

Seg.face

QQ 表情消息段。

参数类型必填说明
idnumberQQ 表情 ID

Seg.image

图片消息段。file 支持本地路径、网络 URL、base64 编码。

参数类型必填说明
filestring图片文件路径/URL/base64
opts.namestring图片名称
opts.summarystring图片摘要
opts.sub_typenumber图片子类型
opts.file_idstring文件 ID(群文件复用)
opts.urlstring图片 URL
opts.pathstring本地路径
opts.file_sizenumber文件大小
opts.file_uniquestring文件唯一标识

Seg.record

语音消息段。

参数类型必填说明
filestring语音文件路径/URL/base64
opts.namestring文件名称
opts.file_idstring文件 ID
opts.urlstring语音 URL
opts.pathstring本地路径
opts.file_sizenumber文件大小
opts.file_uniquestring文件唯一标识

Seg.video

视频消息段。

参数类型必填说明
filestring视频文件路径/URL/base64
opts.namestring文件名称
opts.thumbstring缩略图
opts.file_idstring文件 ID
opts.urlstring视频 URL
opts.pathstring本地路径
opts.file_sizenumber文件大小
opts.file_uniquestring文件唯一标识

Seg.at

@提及消息段。

参数类型必填说明
qqnumber | 'all'QQ 号(数字自动转字符串),传 'all' 表示 @全体成员

Seg.reply

引用回复消息段。

参数类型必填说明
idnumber被回复消息的 message_id

Seg.forward

合并转发消息段。

参数类型必填说明
idstring合并转发 ID(通过 getForwardMsg 获取)

Seg.mface

商城表情消息段(NapCat 扩展)。

参数类型必填说明
opts.emoji_idstring表情 ID
opts.emoji_package_idstring表情包 ID
opts.keystring表情 key
opts.summarystring表情摘要

Seg.dice

骰子消息段,无参数,随机生成点数。

Seg.rps

猜拳消息段,无参数,随机生成石头剪刀布。

Seg.poke

戳一戳消息段。

参数类型必填说明
typestring戳一戳类型
idstring戳一戳 ID

Seg.file

文件消息段。

参数类型必填说明
filestring文件路径/URL
opts.namestring文件名称
opts.file_idstring文件 ID
opts.file_sizenumber文件大小
opts.file_uniquestring文件唯一标识
opts.pathstring本地路径
opts.urlstring文件 URL

Seg.json

JSON 富文本消息段。

参数类型必填说明
datastringJSON 字符串

Seg.music

平台音乐分享消息段。

参数类型必填说明
typestring音乐平台类型:qq163kugoukuwomigu
idstring音乐 ID

Seg.customMusic

自定义音乐分享消息段。

参数类型必填说明
urlstring跳转链接
audiostring音乐文件 URL
titlestring标题
singerstring歌手
imagestring封面图片 URL

Seg.node

转发消息节点(用于构建合并转发消息)。

参数类型必填说明
contentMessageSegment[]消息段数组
userIdnumber发送者 QQ
nicknamestring发送者昵称

Seg.contact

联系人名片消息段。

参数类型必填说明
type'qq' | 'group'名片类型:QQ 好友或群聊
idstringQQ 号或群号

Seg.markdown

Markdown 消息段。

参数类型必填说明
contentstringMarkdown 内容

Seg.share

链接分享消息段。

参数类型必填说明
urlstring分享链接
titlestring标题
opts.contentstring内容描述
opts.imagestring缩略图 URL

Seg.location

位置消息段。

参数类型必填说明
latnumber纬度
lonnumber经度
opts.titlestring位置标题
opts.contentstring位置描述

完整示例

ts
import { Seg } from '@aemeath-projects/napcat'

// 组合消息:@某人 + 文本 + 图片
const segments = [
  Seg.at(123456789),
  Seg.text(' 这是一张图片:'),
  Seg.image('https://example.com/image.png', { summary: '示例图片' }),
]

// 引用回复
const withReply = [
  Seg.reply(12345),
  Seg.text('已收到您的消息'),
]

// 构建合并转发节点
const forwardNodes = [
  Seg.node([Seg.text('第一条消息')], 10001, '用户A'),
  Seg.node([Seg.text('第二条消息')], 10002, '用户B'),
]

// 音乐分享
Seg.music('163', '123456')        // 网易云音乐
Seg.customMusic('https://...', 'https://.../audio.mp3', '我的歌', '歌手名')

// 其他
Seg.dice()                    // 骰子
Seg.rps()                     // 猜拳
Seg.contact('qq', '123456')   // 联系人名片
Seg.location(39.9, 116.4, { title: '北京', content: '天安门' })

以 MIT 协议开源