go_libonebot

package module
v0.0.0-...-7a6c0c8 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2023 License: Apache-2.0 Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go-LibOneBot

什么是OneBot?

来自onebot.dev的定义:

OneBot 是一个聊天机器人应用接口标准,旨在统一不同聊天平台上的机器人应用开发接口,使开发者只需编写一次业务逻辑代码即可应用到多种机器人平台。

OneBot协议是一个通用的机器人标准, 可以通过这套标准在机器人和各种框架间进行通信.

如go-cqhttp就是一个onebot实现, 可以通过这个实现在QQ上使用OneBot协议.

又如nonebot就是一个onebot框架, 开发者可以通过这个框架编写你的机器人逻辑.

通过onebot协议, 可以使这两者连接起来, 例如实现在QQ上使用nonebot框架编写的机器人逻辑.

目前使用较多的OneBot协议有两个版本, 分别是OneBot11和OneBot12, 本项目以OneBot12为主要实现. OneBot11以兼容的方式实现 (正在适配...).

OneBot协议内容?

事件: 机器人平台对框架上报的事件: 如收到消息, 消息撤回, 群成员增加或减少等

动作: 框架要求机器人平台执行的动作: 如发送消息, 撤回消息, 群组踢人等

动作请求: 即框架向机器人平台发送的动作请求

动作响应: 即机器人平台对框架发送的动作请求的响应 表示动作是否执行成功

消息段: 机器人平台对消息的格式化表示, 如纯文本, 图片, 视频, 音频, 文件, 链接, 表情等

连接: 框架和机器人平台之间的连接, 用于传输以上内容, OneBot协议包含了HTTP, Webhook, Websocket, 反向Websocket四种连接方式, 在本项目中,你也可以自行定义OneBot连接方式

什么是Libonebot?

来自onebot.dev的定义:

LibOneBot 指的是不同 OneBot 实现可以复用的部分,可以帮助 OneBot 实现者快速在新的聊天机器人平台实现 OneBot 标准

简单来说, LibOneBot已经帮你实现了基本的连接和消息处理, 你只需要实现你的业务逻辑, 就可以快速制作出一个OneBot实现, 做出一个像go-cqhttp这样的机器人客户端.

什么是Go-LibOneBot?

Go-LibOneBot 是一个go语言的LibOneBot, 可以通过这个库用go语言快速制作出一个OneBot实现.

内置了所有的OneBot 12协议的事件和动作, 上手简单, 你只需要实现你的业务逻辑, 就可以快速制作出一个OneBot实现.

支持 & TODO:

OneBot12 协议:

  • OneBot12 所有事件
  • OneBot12 所有动作 (请求和响应)
  • OneBot12 消息段
  • HTTP 连接
  • Webhook 连接
  • Websocket 连接
  • 反向Websocket 连接
  • 扩展Onebot12 协议: 事件 / 动作 / 消息段

OneBot11 协议:

  • 通用转换器
  • OneBot11 所有事件
  • OneBot11 所有API
  • OneBot11 CQ码
  • HTTP 连接
  • Webhook 连接
  • Websocket 连接
  • 反向Websocket 连接

开发者自行扩展:

  • 自定义事件
  • 自定义动作接口
  • 自定义协议和连接方式
  • 自定义Json解析器和Logger

示例代码

package main

import (
	onebot "github.com/FishZe/go-libonebot"
	v12 "github.com/FishZe/go-libonebot/connect/v12"
	"github.com/FishZe/go-libonebot/protocol"
	"github.com/FishZe/go-libonebot/util"
	"log"
	"time"
)

func main() {
	// 创建一份配置
	onebotConfig := onebot.OneBotConfig{PlatForm: "QQ", Version: "1.0.0", Implementation: "MyQQImpl"}
	// 创建一个bot
	bot := onebot.NewOneBot(onebotConfig, "123456")
	// 创建一个连接
	conn, _ := v12.NewOneBotV12Connect(v12.OneBotV12Config{
		// 连接类型
		// Http
		ConnectType: v12.ConnectTypeHttp,
		HttpConfig: v12.OneBotV12HttpConfig{
			Host:            "127.0.0.1",
			Port:            20003,
			EventEnable:     true,
			EventBufferSize: 500,
		},
	})
	// 把连接加入到bot
	_ = bot.AddConnection(conn)
	// 创建一个事件处理器
	mux := onebot.NewActionMux()
	mux.AddRequestInterface(protocol.HandleActionSendMessage(func(e *protocol.RequestSendMessage) *protocol.ResponseSendMessage {
		// 处理发送消息动作
		log.Println("SendMessage: ", e.Message)
		msg := protocol.NewResponseSendMessage(0)
		msg.MessageId = util.GetUUID()
		return msg
	}))
	// 把事件处理器加入到bot
	bot.Handle(mux)
	for {
		// 每20秒发送一个收到私聊信息的事件
		e := protocol.NewMessageEventPrivate()
		// 消息为 "今天吃什么"
		e.Message = append(e.Message, protocol.GetSegmentText("今天吃什么"))
		e.UserId = "123456"
		err = bot.SendEvent(e)
		if err != nil {
			log.Println(err)
		}
		time.Sleep(20 * time.Second)
	}
}

使用文档

使用文档

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrorActionExist action已存在
	ErrorActionExist = errors.New("action already exist")
	// ErrorActionEmpty action为空
	ErrorActionEmpty = errors.New("action is empty")
)
View Source
var (
	// ErrorConnectionUUIDNotFound 连接UUID未找到
	ErrorConnectionUUIDNotFound = errors.New("connection uuid not found")
)

Functions

This section is empty.

Types

type ActionMux

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

ActionMux 请求处理接口

func NewActionMux

func NewActionMux() (a *ActionMux)

NewActionMux 获取一个ActionMux

func (*ActionMux) AddRegexRequestInterface

func (a *ActionMux) AddRegexRequestInterface(actionRegex string, f protocol.RequestInterface) error

AddRegexRequestInterface 添加一个正则匹配动作请求接口

func (*ActionMux) AddRequestInterface

func (a *ActionMux) AddRequestInterface(f protocol.RequestInterface) error

AddRequestInterface 添加一个请求处理接口

action: 请求的action名称

f: 请求处理接口 protocol.RequestInterface 的实现

func (*ActionMux) DeleteRegexRequestInterface

func (a *ActionMux) DeleteRegexRequestInterface(actionRegex string)

func (*ActionMux) DeleteRequestInterface

func (a *ActionMux) DeleteRequestInterface(action string)

DeleteRequestInterface 删除一个请求处理接口

func (*ActionMux) GetRequestInterface

func (a *ActionMux) GetRequestInterface(action string) (f protocol.RequestInterface)

GetRequestInterface 获取一个请求处理接口

type Bot

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

Bot 一个oneBot实例

func NewOneBot

func NewOneBot(config protocol.OneBotConfig, userId string) (b *Bot)

NewOneBot 获取一个OneBot实例

需要传入config配置和userId

func (*Bot) AddConnection

func (b *Bot) AddConnection(c connect.Connection) error

AddConnection 添加一个连接

func (*Bot) Handle

func (b *Bot) Handle(mux *ActionMux)

Handle 绑定处理器

func (*Bot) SendEvent

func (b *Bot) SendEvent(e any) error

SendEvent 发送一个事件

e: 事件 需要包含*Event字段且满足EventCheck

Directories

Path Synopsis
v11
v12

Jump to

Keyboard shortcuts

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