infoflow

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: MIT Imports: 16 Imported by: 0

README

如流机器人非官方 Go SDK

百度如流(InfoFlow)消息机器人 Go SDK。

参考文档: https://qy.baidu.com/doc/index.html#/inner_serverapi/robot

功能

  • 消息发送:支持文本、链接、图片、Markdown、@提醒等多种消息类型
  • 消息接收:支持接收和处理百度如流消息,命令路由
  • 签名验证:自动验证请求签名,保证安全性
  • AES 加密:支持消息内容 AES 加密传输

安装

go get github.com/smallnest/infoflow

快速开始

使用 Robot(推荐)
package main

import (
    "github.com/smallnest/infoflow"
)

func main() {
    // 创建机器人,同时支持发送和接收
    robot := infoflow.NewRobot(
        "https://your-webhook-url",  // webhook 地址
        ":8080",                      // 监听地址
        "your-token",                 // 验证令牌
        "your-aes-key",               // AES 解密密钥
    )

    // 注册命令处理器
    robot.AddHandler("hello", func(cmd, fromUserID string, body infoflow.Body, msg infoflow.HiMessage) error {
        // 回复消息
        return robot.Sender.SendMsg2Group(msg.GroupID,
            infoflow.CreateText("Hello, " + fromUserID),
        )
    })

    // 启动服务
    robot.Run()
}
仅发送消息
package main

import (
    "github.com/smallnest/infoflow"
)

func main() {
    // 创建发送器
    sender := infoflow.NewSender("https://your-webhook-url")

    // 发送文本消息到群组
    err := sender.SendMsg2Group(123456,
        infoflow.CreateText("Hello, 世界!"),
    )
    if err != nil {
        panic(err)
    }

    // 发送多种类型消息
    err = sender.SendMsg2Group(123456,
        infoflow.CreateText("重要通知"),
        infoflow.CreateLink("https://example.com"),
        infoflow.CreateAtAll(),
    )
}
仅接收消息
package main

import (
    "github.com/smallnest/infoflow"
)

func main() {
    // 创建接收器
    receiver := infoflow.NewReceiver(":8080", "your-token", "your-aes-key")

    // 注册命令处理器
    receiver.AddHandler("hello", func(cmd, fromUserID string, body infoflow.Body, msg infoflow.HiMessage) error {
        // 处理 hello 命令
        return nil
    })

    // 设置未知命令处理器
    receiver.SetUnknownHandler(func(cmd, fromUserID string, body infoflow.Body, msg infoflow.HiMessage) error {
        // 处理未知命令
        return nil
    })

    // 启动服务
    receiver.Run()
}

消息类型

文本消息
// 简单文本
infoflow.CreateText("Hello")

// 格式化文本
infoflow.CreateTextf("Hello, %s", name)

// 带颜色的文本
infoflow.MarkRed("红色文字")
infoflow.MarkGreen("绿色文字")
infoflow.MarkGray("灰色文字")
infoflow.MarkColor("自定义颜色", "blue")
链接消息
infoflow.CreateLink("https://example.com")
图片消息
// 从 image.Image 创建
img := createImage()
infoflow.CreateImage(img)

// 从 URL 创建
infoflow.CreateImageFromURL("https://example.com/image.png")

// 从文件创建
infoflow.CreateImageFromFile("/path/to/image.png")
@消息
// @所有人
infoflow.CreateAtAll()

// @指定用户
infoflow.CreateAtSome("user1", "user2")
Markdown 消息
infoflow.CreateMarkdown("**粗体** 和 *斜体*")

API 参考

Robot
方法 说明
NewRobot(webhook, addr, token, aesKey string) *Robot 创建机器人
Run() 启动机器人接收服务
AddHandler(cmd string, handler Handler) 注册命令处理器
RemoveHandler(cmd string) 移除命令处理器
SetUnknownHandler(handler Handler) 设置未知命令处理器
Sender
方法 说明
NewSender(webhook string) *Sender 创建发送器
SendMsg(toIDs []int64, messages ...any) error 发送消息到指定接收者
SendMsg2Group(toID int64, messages ...any) error 发送消息到指定群组
Receiver
方法 说明
NewReceiver(addr, token, aesKey string) *Receiver 创建接收器
Run() 启动接收器服务
AddHandler(cmd string, handler Handler) 注册命令处理器
RemoveHandler(cmd string) 移除命令处理器
SetUnknownHandler(handler Handler) 设置未知命令处理器
GetHandler(name string) Handler 获取命令处理器

许可证

MIT License

Documentation

Overview

Package infoflow 提供百度如流信息流 API 的客户端和服务端实现

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateAtAll

func CreateAtAll() any

CreateAtAll 创建@所有人消息

func CreateAtSome

func CreateAtSome(atUserIDs ...string) any

CreateAtSome 创建@指定用户消息

func CreateImage

func CreateImage(img image.Image) (any, error)

CreateImage 从 image.Image 创建图片消息

func CreateImageFromFile

func CreateImageFromFile(f string) (any, error)

CreateImageFromFile 从文件创建图片消息

func CreateImageFromURL

func CreateImageFromURL(u string) (any, error)

CreateImageFromURL 从 URL 创建图片消息

func CreateLink(href string) any

CreateLink 创建链接消息

func CreateMarkdown

func CreateMarkdown(content string) any

CreateMarkdown 创建 Markdown 消息

func CreateText

func CreateText(content string) any

CreateText 创建文本消息

func CreateTextf

func CreateTextf(format string, a ...any) any

CreateTextf 创建格式化文本消息

func MarkColor

func MarkColor(s, color string) string

MarkColor 设置文字颜色

func MarkGray

func MarkGray(s string) string

MarkGray 标记灰色文字

func MarkGreen

func MarkGreen(s string) string

MarkGreen 标记绿色文字

func MarkRed

func MarkRed(s string) string

MarkRed 标记红色文字

Types

type At

type At struct {
	AtRobotIDs []int64  `json:"atrobotids"`
	AtUserIDs  []string `json:"atuserids"`
}

At @信息

type AtBody

type AtBody struct {
	Atall     bool     `json:"atall"`
	AtUserIDs []string `json:"atuserids"`
	Type      string   `json:"type"`
}

AtBody @消息体

type Body

type Body struct {
	Type        string `json:"type"`
	UserID      string `json:"userid"`
	Name        string `json:"name"`
	RobotID     int64  `json:"robotid"`
	Content     string `json:"content"`
	Label       string `json:"label"`
	CommandName string `json:"commandname"`
	DownloadURL string `json:"downloadurl"`
}

Body 消息体

type Handler

type Handler func(cmd string, fromUserID string, body Body, hiMsg HiMessage) error

Handler 消息处理器函数类型

type Header struct {
	At            At     `json:"at"`
	ClientMsgID   int64  `json:"clientmsgid"`
	ClientTime    int64  `json:"clienttime"`
	Compatible    string `json:"compatible"`
	Extra         string `json:"extra"`
	FromUserID    string `json:"fromuserid"`
	MessageID     int64  `json:"messageid"`
	MsgSeqID      string `json:"msgseqid"`
	MsgType       string `json:"msgtype"`
	OfflineNotify string `json:"offlinenotify"`
	ServerTime    int64  `json:"servertime"`
	ToID          int64  `json:"toid"`
	ToType        string `json:"totype"`
	UpdateTime    int64  `json:"updatetime"`
}

Header 消息头部

type HiMessage

type HiMessage struct {
	AgentID   int64   `json:"agentid"`
	CorpID    string  `json:"corpid"`
	EventType string  `json:"eventtype"`
	GroupID   int64   `json:"groupid"`
	Message   Message `json:"message"`
	Time      int64   `json:"time"`
}

HiMessage 百度如流消息结构

func (HiMessage) AllContent

func (m HiMessage) AllContent() string

AllContent 获取消息中的所有文本内容

type ImageBody

type ImageBody struct {
	Type    string `json:"type"`
	Content string `json:"content"` // Base64 编码的图片数据
}

ImageBody 图片消息体

type LinkBody

type LinkBody struct {
	Type string `json:"type"`
	Href string `json:"href"`
}

LinkBody 链接消息体

type MarkdownBody

type MarkdownBody struct {
	Type    string `json:"type"`
	Content string `json:"content"`
}

MarkdownBody Markdown 消息体

type Message

type Message struct {
	Body   []Body `json:"body"`
	Header Header `json:"header"`
}

Message 消息内容

type Receiver

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

Receiver 消息接收器,用于接收和处理百度如流消息

func NewReceiver

func NewReceiver(addr, token string, aesKey string) *Receiver

NewReceiver 创建消息接收器

func (*Receiver) AddHandler

func (r *Receiver) AddHandler(cmd string, handler Handler)

AddHandler 添加命令处理器

func (*Receiver) GetHandler

func (r *Receiver) GetHandler(name string) Handler

GetHandler 获取指定命令的处理器

func (*Receiver) Msg

func (r *Receiver) Msg(w http.ResponseWriter, req *http.Request)

Msg 处理 HTTP 请求,验证签名并分发消息到对应的处理器

func (*Receiver) RemoveHandler

func (r *Receiver) RemoveHandler(cmd string)

RemoveHandler 移除命令处理器

func (*Receiver) Run

func (r *Receiver) Run()

Run 启动接收器服务

func (*Receiver) SetUnknownHandler

func (r *Receiver) SetUnknownHandler(handler Handler)

SetUnknownHandler 设置未知命令处理器

func (*Receiver) VisitHandlers

func (r *Receiver) VisitHandlers(fn func(name string, handler Handler) bool)

VisitHandlers 遍历所有命令处理器

type Request

type Request struct {
	Message SenderMessage `json:"message"`
}

Request API 请求结构

type Response

type Response struct {
	ErrCode int64  `json:"errcode"`
	ErrMsg  string `json:"errmsg"`
	Data    struct {
		Fail map[string]any `json:"fail"`
	} `json:"data"`
}

Response API 响应结构

type Robot

type Robot struct {
	Sender   *Sender   // 消息发送器
	Receiver *Receiver // 消息接收器
}

Robot 机器人,集成了发送和接收功能

func NewRobot

func NewRobot(webhook, addr, token, aesKey string) *Robot

NewRobot 创建机器人 webhook: 消息发送 Webhook 地址,从如流机器人的设置中获取 addr: 消息接收服务监听地址,如 ":8080",需要提供给在如流机器人都配置中 token: 签名验证令牌,从如流机器人的设置中获取 aesKey: 消息解密密钥, 从如流机器人的设置中获取

func (*Robot) AddHandler

func (r *Robot) AddHandler(cmd string, handler Handler)

AddHandler 添加命令处理器

func (*Robot) RemoveHandler

func (r *Robot) RemoveHandler(cmd string)

RemoveHandler 移除命令处理器

func (*Robot) Run

func (r *Robot) Run()

Run 启动机器人接收服务

func (*Robot) SetUnknownHandler

func (r *Robot) SetUnknownHandler(handler Handler)

SetUnknownHandler 设置未知命令处理器

type Sender

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

Sender 消息发送器

func NewSender

func NewSender(u string) *Sender

NewSender 创建消息发送器

func (*Sender) SendMsg

func (s *Sender) SendMsg(toIDs []int64, messages ...any) error

SendMsg 发送消息到指定接收者

func (*Sender) SendMsg2Group

func (s *Sender) SendMsg2Group(toID int64, messages ...any) error

SendMsg2Group 发送消息到指定群组

type SenderHeader

type SenderHeader struct {
	ToID []int64 `json:"toid"` // 接收者 ID 列表
}

SenderHeader 发送消息头部

type SenderMessage

type SenderMessage struct {
	Header SenderHeader `json:"header"`
	Body   []any        `json:"body"`
}

SenderMessage 发送消息结构

type TextBody

type TextBody struct {
	Type    string `json:"type"`
	Content string `json:"content"`
}

TextBody 文本消息体

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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