跳到主要内容

Segments - 消息片段

消息片段是构成消息链的基本单元,每种片段类型代表不同的消息内容。

基类 Segment

所有消息片段的抽象基类。

declare abstract class Segment {
abstract AsString(): string; // 转换为字符串表示
}

文本片段 (TextSegment)

表示纯文本类型的消息片段。

declare class TextSegment extends Segment {
Text: string; // 文本内容
AsString(): string; // 返回原始文本
}

属性

  • Text: string - 文本内容

方法

  • AsString(): 返回未修改的文本字符串

示例:

const textSegment = new TextSegment();
textSegment.Text = "Hello World";
logger.info(textSegment.AsString()); // "Hello World"

@用户片段 (AtSegment)

表示@用户类型的消息片段。

declare class AtSegment extends Segment {
Id: string; // 被@的用户ID
AsString(): string; // 返回"@用户ID"格式
}

属性

  • Id: string - 被@的用户ID

方法

  • AsString(): 生成格式为"@用户ID"的字符串

示例:

const atSegment = new AtSegment();
atSegment.Id = "123456789";
logger.info(atSegment.AsString()); // "@123456789"

图片片段 (ImageSegment)

表示图片类型的消息片段。

declare class ImageSegment extends Segment {
Uri: string; // 图片URI地址
AsString(): string; // 返回"[图片]"
}

属性

  • Uri: string - 图片资源的URI地址

方法

  • AsString(): 生成格式为"[图片]"的字符串

示例:

const imageSegment = new ImageSegment();
imageSegment.Uri = "https://example.com/image.jpg";
logger.info(imageSegment.AsString()); // "[图片]"

表情片段 (FaceSegment)

表示表情类型的消息片段。

declare class FaceSegment extends Segment {
id: string; // 表情ID (注意小写)
AsString(): string; // 返回"[表情ID]"
}

属性

  • id: string - 表情的唯一标识符(注意属性名为小写)

方法

  • AsString(): 生成格式为"[表情ID]"的字符串

示例:

const faceSegment = new FaceSegment();
faceSegment.id = "1";
logger.info(faceSegment.AsString()); // "[1]"

回复片段 (ReplySegment)

表示回复引用类型的消息片段。

declare class ReplySegment extends Segment {
Id: string; // 被回复的消息ID
AsString(): string; // 返回"[回复消息ID]"
}

属性

  • Id: string - 被引用消息的ID

方法

  • AsString(): 生成格式为"[回复消息ID]"的字符串

示例:

const replySegment = new ReplySegment();
replySegment.Id = "msg123";
logger.info(replySegment.AsString()); // "[回复msg123]"

未支持片段 (UnsupportedSegment)

表示无法识别的消息片段类型。

declare class UnsupportedSegment extends Segment {
Type: string; // 原始片段类型
Data: string; // 原始片段数据
AsString(): string; // 返回"[未知类型]"
}

属性

  • Type: string - 原始片段类型标识
  • Data: string - 原始片段数据

方法

  • AsString(): 生成格式为"[未知类型]"的字符串

示例:

const unsupportedSegment = new UnsupportedSegment();
unsupportedSegment.Type = "custom";
unsupportedSegment.Data = "some data";
logger.info(unsupportedSegment.AsString()); // "[未知类型]"

使用示例

创建不同类型的片段

// 文本片段
const text = new TextSegment();
text.Text = "Hello";

// @用户片段
const at = new AtSegment();
at.Id = "123456";

// 图片片段
const image = new ImageSegment();
image.Uri = "https://example.com/pic.jpg";

// 表情片段
const face = new FaceSegment();
face.id = "1";

// 回复片段
const reply = new ReplySegment();
reply.Id = "msg123";

添加到消息链

const chain = new MessageChain();
chain.Add(text);
chain.Add(at);
chain.Add(image);
chain.Add(face);
chain.Add(reply);

logger.info(chain.AsString());

注意事项

  1. 类型识别: 每种片段类型都有特定的用途和格式
  2. 字符串转换: AsString() 方法用于调试和日志输出
  3. 属性命名: 注意 FaceSegment 的 id 属性是小写
  4. 扩展性: UnsupportedSegment 用于处理未来可能的新片段类型
  5. 继承关系: 所有片段都继承自 Segment 基类