onebot

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2021 License: MIT Imports: 16 Imported by: 0

README

onebot-go

onebot协议机器人库(开发中)

目前已实现
API
  • send_private_msg
  • send_group_msg
消息段
  • Text
  • Face
  • Image
  • Record
  • Video
  • At
  • Reply
事件
  • message
快速开始

服务端实现推荐使用 go-cqhttp,使用HTTP/HTTP POST方式连接

package main

import (
	"errors"
	"fmt"

	"github.com/kzw200015/onebot-go"
	"github.com/sirupsen/logrus"
)

func main() {
	bot := onebot.New(onebot.BotConfig{
		SelfId: 12345678, //QQ号
		ApiConfig: onebot.ApiConfig{ //与HTTP API配置要一致
			Token:   "token", //API token
			Address: "http://localhost:5700", //API地址
		},
		ServerConfig: onebot.ServerConfig{ //与HTTP POST配置要一致
			Secret:  "secret", //验证用secret
			Address: ":8080", //监听地址
			Path:    "/event", //监听Path
		},
	})

	bot.OnPrivateMessage("", func(bot *onebot.Bot, event onebot.Event) {
		bot.SendPrivateMsg(event.UserId, onebot.MessageBuilder().Text("hello").Text("world").Build()...)
	})

	bot.Start()
}

Documentation

Index

Constants

View Source
const (
	PostTypeMessage = "message"
	PostTypeNotice  = "notice"
	PostTypeRequest = "request"
	PostTypeMeta    = "meta_event"

	MessageTypePrivate = "private"
	MessageTypeGroup   = "group"
)

Variables

This section is empty.

Functions

func DefaultLogger

func DefaultLogger(level logrus.Level) *logrus.Logger

func MessageBuilder added in v0.2.0

func MessageBuilder() *messageBuilder

Types

type APIResp

type APIResp struct {
	Status  string      `json:"status"`
	Retcode int         `json:"retcode"`
	Data    interface{} `json:"data"`
}

APIResp API响应 https://github.com/botuniverse/onebot-11/blob/master/communication/http.md#%E5%93%8D%E5%BA%94

type Anonymous

type Anonymous struct {
	Id   int    `json:"id"`
	Name string `json:"name"`
	Flag string `json:"flag"`
}

type ApiConfig

type ApiConfig struct {
	Token   string
	Address string
}

type Bot

type Bot struct {
	BotConfig *BotConfig
	Logger    *logrus.Logger
	// contains filtered or unexported fields
}

func New

func New(botConfig BotConfig) *Bot

func (*Bot) On

func (bot *Bot) On(postType string, handler EventHandler)

func (*Bot) OnGroupMessage

func (bot *Bot) OnGroupMessage(prefix string, fn EventHandlerFunc)

func (*Bot) OnMessage

func (bot *Bot) OnMessage(prefix string, fn EventHandlerFunc)

func (*Bot) OnMeta

func (bot *Bot) OnMeta(fn EventHandlerFunc)

func (*Bot) OnNotice

func (bot *Bot) OnNotice(fn EventHandlerFunc)

func (*Bot) OnPrivateMessage

func (bot *Bot) OnPrivateMessage(prefix string, fn EventHandlerFunc)

func (*Bot) OnRequest

func (bot *Bot) OnRequest(fn EventHandlerFunc)

func (*Bot) Request

func (bot *Bot) Request(action string, requestBody interface{}) (APIResp, error)

func (*Bot) Start

func (bot *Bot) Start()

type BotConfig

type BotConfig struct {
	SelfId       int64
	ApiConfig    ApiConfig
	ServerConfig ServerConfig
}

type Context added in v0.2.1

type Context struct {
	Event Event
	Bot   *Bot
}

func (*Context) SendMessage added in v0.2.1

func (ctx *Context) SendMessage(message ...MessageSegment) (APIResp, error)

type Event

type Event struct {
	Time     int64  `json:"time"`
	SelfId   int64  `json:"self_id"`
	PostType string `json:"post_type"`

	SubType string `json:"sub_type"`

	// message
	MessageType string           `json:"message_type"`
	MessageId   int32            `json:"message_id,"`
	UserId      int64            `json:"user_id"`
	Message     []MessageSegment `json:"message"`
	RawMessage  string           `json:"raw_message"`
	Font        int32            `json:"font"`
	Sender      Sender           `json:"sender"`

	// group
	GroupId   int64     `json:"group_id"`
	Anonymous Anonymous `json:"anonymous"`
}

Event 事件 https://github.com/botuniverse/onebot-11/tree/master/event

type EventHandler

type EventHandler struct {
	MessagePrefix string
	MessageType   string
	Handle        EventHandlerFunc
}

type EventHandlerFunc

type EventHandlerFunc func(*Context)

type HandlerMap

type HandlerMap struct {
	*sync.Map
}

func (*HandlerMap) GetHandlers

func (hm *HandlerMap) GetHandlers(postType string) []EventHandler

type MessageSegment

type MessageSegment struct {
	Type string            `json:"type"`
	Data map[string]string `json:"data"`
}

MessageSegment 消息段 https://github.com/botuniverse/onebot-11/blob/master/message/array.md

type Sender

type Sender struct {
	UserId   int    `json:"user_id"`
	Nickname string `json:"nickname"`
	Sex      string `json:"sex"`
	Age      int    `json:"age"`
	Card     string `json:"card"`

	// group extras
	Area  string `json:"area"`
	Level string `json:"level"`
	Role  string `json:"role"`
	Title string `json:"title"`
}

type ServerConfig

type ServerConfig struct {
	Secret  string
	Address string
	Path    string
}

Jump to

Keyboard shortcuts

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