botcore

package
v0.0.0-...-d113ed5 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 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         // 可下载的资源地址
}

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)

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

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

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

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

type Chain

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

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

func NewChain

func NewChain(defaultHandler Handler) *Chain

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

func (*Chain) AddRoute

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

AddRoute 添加一条路由规则。

func (*Chain) Trigger

func (c *Chain) Trigger(update RequestSnapshot) <-chan StreamChunk

Trigger 实现 PipelineInvoker 接口。

type ChatType

type ChatType string

ChatType 描述会话类型枚举。

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

type Handler

type Handler PipelineInvoker

Handler 定义路由处理逻辑。 实际上就是 PipelineInvoker,为了语义清晰起见定义别名。

type Matcher

type Matcher func(update RequestSnapshot) bool

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

func MatchAny

func MatchAny() Matcher

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

func MatchPrefix

func MatchPrefix(prefix string) Matcher

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

type PipelineFunc

type PipelineFunc func(update RequestSnapshot) <-chan StreamChunk

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

func (PipelineFunc) Trigger

func (f PipelineFunc) Trigger(update RequestSnapshot) <-chan StreamChunk

Trigger 实现 PipelineInvoker 接口。

type PipelineInvoker

type PipelineInvoker interface {
	Trigger(update RequestSnapshot) <-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               // 平台原始结构引用,便于 Handler 深度使用
	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 Route

type Route struct {
	Name    string
	Matcher Matcher
	Handler Handler
}

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