botcore

package
v0.0.0-...-9d86306 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoResponse = struct{}{}

NoResponse 是一个哨兵值,用于标记不需要被动回复。 当 StreamChunk.Payload == NoResponse 时,Bot 层应直接返回 HTTP 200 OK 空包。

Functions

This section is empty.

Types

type Attachment

type Attachment struct {
	Type AttachmentType // 附件类型: image/file
	URL  string         // 可下载的资源地址(当 Data 为空时使用)
	// Data 存储已解密/已下载的原始字节数据。
	// 当此字段非空时,SaveAttachments 将直接使用此数据而不是下载 URL。
	// 由平台协议层(如 wecom)自动填充已解密的附件数据。
	Data []byte
}

Attachment 描述平台无关的附件信息。

type AttachmentType

type AttachmentType string

AttachmentType 描述附件类型。

const (
	// AttachmentTypeImage 表示图片附件。
	AttachmentTypeImage AttachmentType = "image"
	// AttachmentTypeFile 表示文件附件。
	AttachmentTypeFile AttachmentType = "file"
)

type Bot

type Bot interface {
	// BuildFirstSnapshot 生成首包快照。
	BuildFirstSnapshot(raw any) (RequestSnapshot, error)

	// BuildReply 将流式片段编码为平台响应。
	BuildReply(firstSnapshot RequestSnapshot, chunk StreamChunk) (any, error)

	// Response 向指定的 response_url 发送主动回复消息。
	Response(responseURL string, msg any) error

	// ResponseMarkdown 发送 Markdown 消息。
	ResponseMarkdown(responseURL, content string) error

	// ResponseTemplateCard 发送模板卡片消息。
	ResponseTemplateCard(responseURL string, card any) error
}

Bot 抽象首包快照构建与响应编码能力。

type Chain

type Chain struct {
	// contains filtered or unexported fields
}

Chain 实现了一个基于责任链/路由表的 PipelineInvoker。 它按顺序检查路由,一旦匹配成功,就移交给对应的 PipelineInvoker,并停止后续匹配。 如果所有路由都不匹配,且设置了 defaultHandler,则调用 defaultHandler。

func NewChain

func NewChain(defaultHandler PipelineInvoker) *Chain

NewChain 创建一个新的责任链路由器。 Parameters:

  • defaultHandler: 默认处理器;为 nil 表示无默认处理

Returns:

  • *Chain: 初始化后的责任链路由器

func (*Chain) AddRoute

func (c *Chain) AddRoute(name string, matcher Matcher, handler PipelineInvoker)

AddRoute 添加一条路由规则。 Parameters:

  • name: 路由名称(便于调试与日志)
  • matcher: 匹配规则
  • handler: 命中后执行的 PipelineInvoker

func (*Chain) Trigger

func (c *Chain) Trigger(ctx PipelineContext) <-chan StreamChunk

Trigger 实现 PipelineInvoker 接口。 Parameters:

  • ctx: Pipeline 执行上下文(包含 Snapshot 与 Responser)

Returns:

  • <-chan StreamChunk: 流式输出片段通道(无匹配时可能返回 nil)

type ChatType

type ChatType string

ChatType 描述会话类型枚举。

const (
	ChatTypeSingle   ChatType = "single"   // 单聊
	ChatTypeChatroom ChatType = "chatroom" // 群聊
)

type Matcher

type Matcher func(update RequestSnapshot) bool

Matcher 定义路由匹配逻辑。 返回 true 表示该路由应该处理此首包快照。

func MatchAny

func MatchAny() Matcher

MatchAny 返回一个总是匹配的 Matcher。 Returns:

  • Matcher: 永远返回 true 的匹配器

func MatchPrefix

func MatchPrefix(prefix string) Matcher

MatchPrefix 返回一个匹配文本前缀的 Matcher。 Parameters:

  • prefix: 需要匹配的文本前缀

Returns:

  • Matcher: 当前前缀匹配器

type PipelineContext

type PipelineContext struct {
	Snapshot  RequestSnapshot
	Responser Responser
}

PipelineContext 承载 Pipeline 执行所需的显式上下文。 Fields:

  • Snapshot: 标准化首包快照
  • Responser: 主动回复能力(可为空,代表不支持主动回复)

type PipelineFunc

type PipelineFunc func(ctx PipelineContext) <-chan StreamChunk

PipelineFunc 便于直接以函数充当 PipelineInvoker。

func (PipelineFunc) Trigger

func (f PipelineFunc) Trigger(ctx PipelineContext) <-chan StreamChunk

Trigger 实现 PipelineInvoker 接口。

type PipelineInvoker

type PipelineInvoker interface {
	Trigger(ctx PipelineContext) <-chan StreamChunk
}

PipelineInvoker 抽象命令/业务执行器。

type RequestSnapshot

type RequestSnapshot struct {
	ID       string   // 平台内的唯一消息、事件或流会话 ID
	SenderID string   // 触发用户标识
	ChatID   string   // 会话 ID(群、私聊等)
	ChatType ChatType // 会话类型,示例:single/chatroom(企业微信为 single/group,内部映射为 chatroom)

	Text        string            // 主要文本内容(若适用)
	Attachments []Attachment      // 标准化附件列表(图片/文件等)
	Raw         any               // 平台原始结构引用,便于 Pipeline 深度使用
	ResponseURL string            // 主动回复 URL(部分平台返回)
	Metadata    map[string]string // 扩展键值,如语言、平台等
}

RequestSnapshot 描述首包请求的标准化快照。

func (RequestSnapshot) SaveAttachments

func (r RequestSnapshot) SaveAttachments(dir string) ([]SavedAttachment, error)

SaveAttachments 下载并保存所有附件到指定目录。 Parameters:

  • dir: 保存目录(不存在会创建)

Returns:

  • []SavedAttachment: 每个附件的保存结果
  • error: 只要有任意附件失败则返回非空错误

type Responser

type Responser interface {
	Response(responseURL string, msg any) error
	ResponseMarkdown(responseURL, content string) error
	ResponseTemplateCard(responseURL string, card any) error
}

Responser 定义主动发送能力的抽象接口。 Parameters:

  • responseURL: 平台回调中提供的 response_url
  • msg/content/card: 待发送内容

Returns:

  • error: 发送失败时返回

该接口用于将平台实现(如 wecom.Bot)注入到 Manager 中,避免构造期循环依赖。

type Route

type Route struct {
	Name    string
	Matcher Matcher
	Handler PipelineInvoker
}

Route 定义单条路由规则。

type SavedAttachment

type SavedAttachment struct {
	Attachment Attachment // 原始附件信息
	Path       string     // 保存后的本地路径
	Err        error      // 单个附件的错误(若有)
}

SavedAttachment 表示附件保存结果。

type StreamChunk

type StreamChunk struct {
	Content string
	Payload any // 扩展:支持携带复杂对象(如 TemplateCard),用于非流式回复
	IsFinal bool
}

StreamChunk 描述流式输出片段。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL