nsxbot

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2025 License: Unlicense Imports: 14 Imported by: 0

README

NsxBot OneBot Framework

nsxbot

Go Reference Badge License qq group

⚠️ 注意: 本项目目前处于 v0.x.y 阶段,API 尚不稳定,随时可能发生变更。请勿大规模使用,或锁定到具体的 commit/tag。

简介

NsxBot 是一个使用 Go 语言编写,基于 OneBot 11 协议的聊天机器人框架。

提供类似Web框架风格的API,如果你是一个Go Web开发者,那么你可以非常方便的使用NsxBot。

特性

  • http,websocket 协议支持
  • 支持多客户端统一处理
  • 泛型支持,远离any
  • 中间件支持
  • 过滤器支持
  • 事件分组监听
  • 自由组合与可扩展性

快速开始

获取
go get -u github.com/nsxdevx/nsxbot
运行

示例:

package main

import (
	"context"
	"os"
	"strconv"

	"github.com/nsxdevx/nsxbot"
	"github.com/nsxdevx/nsxbot/driver"
	"github.com/nsxdevx/nsxbot/event"
	"github.com/nsxdevx/nsxbot/schema"
)

func main() {
	driver := driver.NewDriverHttp(":8080", "http://localhost:4000")

	bot := nsxbot.Default(driver)

	pvt := nsxbot.OnEvent[event.PrivateMessage](bot)

	adminuin, _ := strconv.ParseInt(os.Getenv("ADMIN_UIN"), 10, 64)
	pvt.Handle(func(ctx *nsxbot.Context[event.PrivateMessage]) {
		msg := ctx.Msg
		text, err := msg.TextFirst()
		if err != nil {
			ctx.Log.Error("Error parsing message", "error", err)
			return
		}
		ctx.Log.Info("Private Message", "message", text.Text)
		msg.Reply(ctx, text.Text)
		var msgchain schema.MessageChain
		ctx.SendPvtMsg(ctx, adminuin, msgchain.Text("收到回复了吗?").Br().Face("4"))
	})

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	// Run
	bot.Run(ctx)
}

参考

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Composer

type Composer[T event.Eventer] struct {
	// contains filtered or unexported fields
}

func (*Composer[T]) Compose

func (c *Composer[T]) Compose(fillers ...filter.Filter[T]) *Composer[T]

Compose creates a new Composer with the given filters.

func (*Composer[T]) Filit

func (c *Composer[T]) Filit(fillers ...filter.Filter[T])

Filit adds filters to the Composer.

func (*Composer[T]) Handle

func (c *Composer[T]) Handle(handler HandlerFunc[T], filters ...filter.Filter[T])

Handle adds a handler to the Composer.

func (*Composer[T]) Use

func (c *Composer[T]) Use(handlers ...HandlerFunc[T])

Use adds handlers to the Composer.

type Context

type Context[T any] struct {
	context.Context
	driver.Emitter
	event.Replyer

	Time   int64
	SelfId int64
	Msg    T
	Log    *slog.Logger
	// contains filtered or unexported fields
}

func NewContext

func NewContext[T any](ctx context.Context, emitter driver.Emitter, selfId int64, time int64, data T, Replyer event.Replyer) Context[T]

func (*Context[T]) Abort

func (c *Context[T]) Abort()

func (*Context[T]) Next

func (c *Context[T]) Next()

type Engine

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

func Default

func Default(driver driver.Driver) *Engine

func New

func New(listener driver.Listener, emitterMux driver.EmitterMux) *Engine

func (*Engine) Run

func (e *Engine) Run(ctx context.Context)

func (*Engine) SetConsumerNum

func (e *Engine) SetConsumerNum(consumerNum int)

func (*Engine) SetTaskLen

func (e *Engine) SetTaskLen(taskLen int)

type EventHandler

type EventHandler[T event.Eventer] struct {
	Composer[T]
	// contains filtered or unexported fields
}

func OnEvent

func OnEvent[T event.Eventer](engine *Engine) *EventHandler[T]

start handler all self event

func OnSelfsEvent

func OnSelfsEvent[T event.Eventer](engine *Engine, selfIds ...int64) *EventHandler[T]

start handler evnet by selfIds

type FilterChain

type FilterChain[T any] []filter.Filter[T]

type HandlerEnd

type HandlerEnd[T any] struct {
	// contains filtered or unexported fields
}

type HandlerFunc

type HandlerFunc[T any] func(ctx *Context[T])

func NewConversation

func NewConversation[T SessionMsg](handler SessionHandler[T]) HandlerFunc[T]

Start a conversation session with a handler.

func Recovery

func Recovery[T any]() HandlerFunc[T]

type HandlersChain

type HandlersChain[T any] []HandlerFunc[T]

type Sation

type Sation[T event.Messager] struct {
	// contains filtered or unexported fields
}

func (*Sation[T]) Await

func (s *Sation[T]) Await(ctx context.Context, fillers ...filter.Filter[T]) (*Context[T], error)

Context represents the context of a message in a session.

type SessionHandler

type SessionHandler[T event.Messager] = func(ctx *Context[T], sation *Sation[T])

type SessionMsg

type SessionMsg interface {
	event.Messager
	SessionKey() string
}

type SessionStore

type SessionStore[T event.Messager] struct {
	// contains filtered or unexported fields
}

func (*SessionStore[T]) Del

func (s *SessionStore[T]) Del(key string)

func (*SessionStore[T]) Set

func (s *SessionStore[T]) Set(key string, ctx *Context[T]) (*Sation[T], bool)

Directories

Path Synopsis
examples
command command
compose command
conversion command
filter command
handlers command
many command
meta command
reply command
request command

Jump to

Keyboard shortcuts

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