onebot

package module
v0.0.0-...-c0ede07 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2024 License: AGPL-3.0 Imports: 15 Imported by: 7

README

OneBot的Go SDK

image

这是针对onebot-11编写的Go SDK。

OneBot是一个通用聊天机器人应用接口标准。

开始

请多参阅onebot-11的文档。

[!IMPORTANT] 本项目是基于onebot的正向ws接口,因此你需要开启对应机器人项目的ws监听。

本项目处理消息的格式是消息段数组,因此你需要将onobot中的event.message_format配置为array

引入项目:

go get -u github.com/CuteReimu/onebot

关于如何使用,可以参考examples文件夹下的例子

进度

目前已支持的功能有:

  • 消息链
    • 所有消息类型
    • 所有消息解析
  • 事件
    • 消息事件,包括私聊消息、群消息等
    • 通知事件,包括群成员变动、好友变动等
    • 请求事件,包括加群请求、加好友请求等
    • 元事件,包括 OneBot 生命周期、心跳等
  • 请求
    • 发送、撤回消息
    • 获取消息
    • 发送好友赞
    • 群管理
    • 设置群名片
    • 退出群
    • 处理好友、加群请求
    • 获取账号信息
    • 获取群信息
    • 获取群成员信息
    • 获取群荣誉信息
    • 获取QQ相关信息
    • 图片语音相关
    • 获取OneBot相关信息
  • 其它
    • 连接与认证
    • 请求限流
    • 快速操作

Documentation

Index

Constants

View Source
const (
	// WsChannelApi 连接此接口后,向 OneBot 发送如下结构的 JSON 对象,即可调用相应的 API
	WsChannelApi = "api"

	// WsChannelEvent 连接此接口后,OneBot 会在收到事件后推送至客户端
	WsChannelEvent = "event"

	// WsChannelAll 在一条连接上同时提供 api 和 event 的服务
	WsChannelAll = ""
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Anonymous

type Anonymous struct {
	Ignore string `json:"ignore,omitempty"` // 发消息时使用,1和0表示无法匿名时是否继续发送
}

Anonymous 匿名发消息

func (*Anonymous) GetMessageType

func (m *Anonymous) GetMessageType() string

func (*Anonymous) String

func (m *Anonymous) String() string

type AnonymousMember

type AnonymousMember struct {
	Id   int64  `json:"id"`   // 匿名用户 ID
	Name string `json:"name"` // 匿名用户名称
	Flag string `json:"flag"` // 匿名用户 flag,在调用禁言 API 时需要传入
}

func (*AnonymousMember) String

func (a *AnonymousMember) String() string

type At

type At struct {
	QQ string `json:"qq"` // 群员QQ号,或者"all"表示全体成员
}

At @某人

func (*At) GetMessageType

func (m *At) GetMessageType() string

func (*At) String

func (m *At) String() string

type Bot

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

func Connect

func Connect(host string, port int, channel WsChannel, accessToken string, qq int64, concurrentEvent bool) (*Bot, error)

Connect 连接onebot

concurrentEvent 参数如果是true,表示采用并发方式处理事件和消息,由调用者自行解决并发问题。 如果是false表示用单线程处理事件和消息,调用者无需关心并发问题。

func (*Bot) Close

func (b *Bot) Close() error

func (*Bot) DeleteMessage

func (b *Bot) DeleteMessage(messageId int64) error

DeleteMessage 撤回消息,messageId-需要撤回的消息的ID

func (*Bot) GetGroupInfo

func (b *Bot) GetGroupInfo(group int64, noCache bool) (*GroupInfo, error)

GetGroupInfo 获取群信息,group-群号,noCache-是否不使用缓存

func (*Bot) GetGroupList

func (b *Bot) GetGroupList() ([]*GroupInfo, error)

GetGroupList 获取群列表

func (*Bot) GetGroupMemberInfo

func (b *Bot) GetGroupMemberInfo(group, qq int64, noCache bool) (*GroupMemberInfo, error)

GetGroupMemberInfo 获取群成员信息,group-群号,qq-QQ号,noCache-是否不使用缓存

func (*Bot) GetGroupMemberList

func (b *Bot) GetGroupMemberList(group int64) ([]*GroupMemberInfo, error)

GetGroupMemberList 获取群成员列表,group-群号

注意:获取群成员列表时,不保证能获取到每个成员的所有信息,有些信息(例如area、title等)可能无法获得。 想要获取所有信息,请调用 Bot.GetGroupMemberInfo 方法获取单个成员信息。

func (*Bot) ListenFriendRequest

func (b *Bot) ListenFriendRequest(l func(request *FriendRequest) bool)

ListenFriendRequest 监听加好友请求

func (*Bot) ListenGroupMessage

func (b *Bot) ListenGroupMessage(l func(message *GroupMessage) bool)

ListenGroupMessage 监听群消息

func (*Bot) ListenGroupRequest

func (b *Bot) ListenGroupRequest(l func(request *GroupRequest) bool)

ListenGroupRequest 监听加群请求 / 邀请

func (*Bot) ListenPrivateMessage

func (b *Bot) ListenPrivateMessage(l func(message *PrivateMessage) bool)

ListenPrivateMessage 监听私聊消息

func (*Bot) Run

func (b *Bot) Run(f func())

Run 如果不是并发方式启动,则此方法会将函数放入事件队列。如果是并发方式启动,则此方法等同于go f()。

func (*Bot) SendGroupMessage

func (b *Bot) SendGroupMessage(group int64, message MessageChain) (int64, error)

SendGroupMessage 发送群消息,group-群号,message-发送的内容,返回消息id

func (*Bot) SendMessage

func (b *Bot) SendMessage(messageType MessageType, targetId int64, message MessageChain) (int64, error)

SendMessage 发送消息,返回消息id

func (*Bot) SendPrivateMessage

func (b *Bot) SendPrivateMessage(userId int64, message MessageChain) (int64, error)

SendPrivateMessage 发送私聊消息,消息ID

func (*Bot) SetGroupLeave

func (b *Bot) SetGroupLeave(group int64, isDismiss bool) error

SetGroupLeave 退出群,group-群号,isDismiss-是否解散

func (*Bot) SetLimiter

func (b *Bot) SetLimiter(limiterType string, l *rate.Limiter)

SetLimiter 设置限流器,limiterType为"wait"表示等待,为"drop"表示丢弃

type Contact

type Contact struct {
	Type ContactType `json:"type"` // 类型
	Id   string      `json:"id"`   // QQ号或群号
}

Contact 推荐好友、推荐群

func (*Contact) GetMessageType

func (m *Contact) GetMessageType() string

func (*Contact) String

func (m *Contact) String() string

type ContactType

type ContactType string
const (
	ContactTypeQQ    ContactType = "qq"    // 推荐好友
	ContactTypeGroup ContactType = "group" // 推荐群
)

type Dice

type Dice struct {
}

Dice 掷骰子魔法表情

func (*Dice) GetMessageType

func (m *Dice) GetMessageType() string

func (*Dice) String

func (m *Dice) String() string

type Face

type Face struct {
	Id string `json:"id"` // QQ 表情 ID
}

Face QQ表情

func (*Face) GetMessageType

func (m *Face) GetMessageType() string

func (*Face) String

func (m *Face) String() string

type Forward

type Forward struct {
	Id string `json:"id"` // 合并转发 ID,需调用 Bot.GetForwardMsg 方法获取具体内容
}

Forward 合并转发

func (*Forward) GetMessageType

func (m *Forward) GetMessageType() string

func (*Forward) String

func (m *Forward) String() string

type Friend

type Friend struct {
	UserId   int64  `json:"user_id"`  // QQ号
	Nickname string `json:"nickname"` // 昵称
	Remark   string `json:"remark"`   // 备注
}

Friend 好友

func (*Friend) RemarkOrNickname

func (f *Friend) RemarkOrNickname() string

func (*Friend) String

func (f *Friend) String() string

type FriendRequest

type FriendRequest struct {
	Time        int64  `json:"time"`         // 事件发生的时间戳
	SelfId      int64  `json:"self_id"`      // 收到事件的机器人 QQ 号
	PostType    string `json:"post_type"`    // "request"
	RequestType string `json:"request_type"` // "friend"
	UserId      int64  `json:"user_id"`      // 发送请求的 QQ 号
	Comment     string `json:"comment"`      // 验证信息
	Flag        string `json:"flag"`         // 请求 flag,在调用处理请求的 API 时需要传入
}

FriendRequest 加好友请求

func (*FriendRequest) Reply

func (r *FriendRequest) Reply(b *Bot, approve bool, remark string) error

Reply 响应加好友请求,approve是是否同意,remark是添加后的好友备注(仅在同意时有效)

type Group

type Group struct {
	Id         int64  `json:"id"`         // 群号
	Name       string `json:"name"`       // 群名称
	Permission Role   `json:"permission"` // Bot在群中的权限
}

Group 群

func (*Group) String

func (g *Group) String() string

type GroupInfo

type GroupInfo struct {
	GroupId        int64  `json:"group_id"`         // 群号
	GroupName      string `json:"group_name"`       // 群名称
	MemberCount    int32  `json:"member_count"`     // 成员数
	MaxMemberCount int32  `json:"max_member_count"` // 最大成员数(群容量)
}

type GroupMemberInfo

type GroupMemberInfo struct {
	GroupId         int64  `json:"group_id"`          // 群号
	UserId          int64  `json:"user_id"`           // QQ 号
	Nickname        string `json:"nickname"`          // 昵称
	Card            string `json:"card"`              // 群名片/备注
	Sex             Sex    `json:"sex"`               // 性别
	Age             int32  `json:"age"`               // 年龄
	Area            string `json:"area"`              // 地区
	JoinTime        int32  `json:"join_time"`         // 加群时间戳
	LastSentTime    int32  `json:"last_sent_time"`    // 最后发言时间戳
	Level           string `json:"level"`             // 成员等级
	Role            Role   `json:"role"`              // 角色
	Unfriendly      bool   `json:"unfriendly"`        // 是否不良记录成员
	Title           string `json:"title"`             // 专属头衔
	TitleExpireTime int32  `json:"title_expire_time"` // 专属头衔过期时间戳
	CardChangeable  bool   `json:"card_changeable"`   // 是否允许修改群名片
}

func (*GroupMemberInfo) CardOrNickname

func (m *GroupMemberInfo) CardOrNickname() string

type GroupMessage

type GroupMessage struct {
	Time        int64               `json:"time"`         // 事件发生的时间戳
	SelfId      int64               `json:"self_id"`      // 收到事件的机器人 QQ 号
	PostType    string              `json:"post_type"`    // "message"
	MessageType string              `json:"message_type"` // "group"
	SubType     GroupMessageSubType `json:"sub_type"`     // 消息子类型
	MessageId   int32               `json:"message_id"`   // 消息 ID
	GroupId     int64               `json:"group_id"`     // 群号
	UserId      int64               `json:"user_id"`      // 发送者 QQ 号
	Anonymous   *AnonymousMember    `json:"anonymous"`    // 匿名信息,如果不是匿名消息则为 nil
	Message     MessageChain        `json:"message"`      // 消息内容
	RawMessage  string              `json:"raw_message"`  // 原始消息内容
	Font        int32               `json:"font"`         // 字体
	Sender      Member              `json:"sender"`       // 发送人信息
}

GroupMessage 群消息

func (*GroupMessage) Ban

func (m *GroupMessage) Ban(b *Bot, duration int32) error

func (*GroupMessage) Delete

func (m *GroupMessage) Delete(b *Bot) error

Delete 撤回(需要权限),发送者是匿名用户时无效

func (*GroupMessage) Kick

func (m *GroupMessage) Kick(b *Bot) error

Kick 把发送者踢出群组(需要权限),不拒绝此人后续加群请求,发送者是匿名用户时无效

func (*GroupMessage) Reply

func (m *GroupMessage) Reply(b *Bot, reply MessageChain, atSender bool) error

Reply 回复

type GroupMessageSubType

type GroupMessageSubType string
const (
	GroupMessageNormal    GroupMessageSubType = "normal"    // 正常消息
	GroupMessageAnonymous GroupMessageSubType = "anonymous" // 匿名消息
	GroupMessageNotice    GroupMessageSubType = "notice"    // 系统提示
)

type GroupRequest

type GroupRequest struct {
	Time        int64               `json:"time"`         // 事件发生的时间戳
	SelfId      int64               `json:"self_id"`      // 收到事件的机器人 QQ 号
	PostType    string              `json:"post_type"`    // "request"
	RequestType string              `json:"request_type"` // "group"
	SubType     GroupRequestSubType `json:"sub_type"`     // 请求子类型
	GroupId     int64               `json:"group_id"`     // 群号
	UserId      int64               `json:"user_id"`      // 发送请求的 QQ 号
	Comment     string              `json:"comment"`      // 验证信息
	Flag        string              `json:"flag"`         // 请求 flag,在调用处理请求的 API 时需要传入
}

GroupRequest 加群请求/邀请

func (*GroupRequest) Reply

func (r *GroupRequest) Reply(b *Bot, approve bool, reason string) error

Reply 响应加群请求/邀请,approve是是否同意,reason是拒绝理由(仅在拒绝时有效)

type GroupRequestSubType

type GroupRequestSubType string
const (
	GroupRequestAdd    GroupRequestSubType = "add"    // 加群请求
	GroupRequestInvite GroupRequestSubType = "invite" // 邀请入群
)

type Image

type Image struct {
	// 图片文件名
	//
	// 当发送图片时,除了可以直接使用收到的文件名直接发送以外,还支持:
	//
	// 绝对路径,格式使用file URI;
	// 网络URL,格式为http://xxx
	// Base64编码,格式为base64://<base64编码>。
	File string `json:"file"`

	// 图片类型,"flash"表示闪照,无此参数表示普通图片
	Type string `json:"type,omitempty"`

	// 图片的URL,只有在收图片时才有这个字段
	Url string `json:"url,omitempty"`

	// 以下字段只在通过网络URL发送时有效
	Cache   string `json:"cache,omitempty"`   // 1和0表示是否使用已缓存的文件,默认是
	Proxy   string `json:"proxy,omitempty"`   // 1和0表示是否通过代理下载文件(需通过环境变量或配置文件配置代理),默认是
	Timeout string `json:"timeout,omitempty"` // 单位秒,表示下载网络文件的超时时间,默认不超时
}

Image 图片

func (*Image) GetMessageType

func (m *Image) GetMessageType() string

func (*Image) String

func (m *Image) String() string

type Json

type Json struct {
	Data string `json:"data"` // Json文本
}

func (*Json) GetMessageType

func (m *Json) GetMessageType() string

func (*Json) String

func (m *Json) String() string

type Location

type Location struct {
	Lat     string `json:"lat"`               // 纬度
	Lon     string `json:"lon"`               // 经度
	Title   string `json:"title,omitempty"`   // 标题,发送时可不填
	Content string `json:"content,omitempty"` // 内容描述,发送时可不填
}

Location 位置

func (*Location) GetMessageType

func (m *Location) GetMessageType() string

func (*Location) String

func (m *Location) String() string

type Member

type Member struct {
	UserId   int64  `json:"user_id"`  // QQ号
	Nickname string `json:"nickname"` // 昵称
	Card     string `json:"card"`     // 群名片/备注
	Sex      Sex    `json:"sex"`      // 性别
	Age      int32  `json:"age"`      // 年龄
	Area     string `json:"area"`     // 地区
	Level    string `json:"level"`    // 成员等级
	Role     Role   `json:"role"`     // 角色
}

Member 群成员

func (*Member) CardOrNickname

func (m *Member) CardOrNickname() string

func (*Member) String

func (m *Member) String() string

type MessageChain

type MessageChain []SingleMessage

func (*MessageChain) MarshalJSON

func (c *MessageChain) MarshalJSON() ([]byte, error)

func (*MessageChain) UnmarshalJSON

func (c *MessageChain) UnmarshalJSON(data []byte) error

type MessageType

type MessageType string
const (
	MessageTypePrivate MessageType = "private" // 私聊消息
	MessageTypeGroup   MessageType = "group"   // 群消息
)

type Music

type Music struct {
	// "qq": QQ音乐, "163": 网易云音乐, "xm": 虾米音乐, "custom": 音乐自定义分享
	Type string `json:"type"`

	// 歌曲ID,用于非自定义分享
	Id string `json:"id,omitempty"`

	Url     string `json:"url,omitempty"`     // 点击后跳转目标 URL
	Audio   string `json:"audio,omitempty"`   // 音乐 URL
	Title   string `json:"title,omitempty"`   // 标题
	Content string `json:"content,omitempty"` // 内容描述,发送时可不填
	Image   string `json:"image,omitempty"`   // 图片URL,发送时可不填
}

Music 音乐分享

func (*Music) GetMessageType

func (m *Music) GetMessageType() string

func (*Music) String

func (m *Music) String() string

type Node

type Node struct {
	Id string `json:"id,omitempty"` // 转发的消息 ID

	UserId   string       `json:"user_id,omitempty"`  // 发送者 QQ 号
	Nickname string       `json:"nickname,omitempty"` // 发送者昵称
	Content  MessageChain `json:"content,omitempty"`  // 消息内容
}

Node 合并转发节点

func (*Node) GetMessageType

func (m *Node) GetMessageType() string

func (*Node) String

func (m *Node) String() string

type Poke

type Poke struct {
	Type string `json:"type"`
	Id   string `json:"id"`
	Name string `json:"name,omitempty"` // 表情名,发送时无需此字段
}

Poke 戳一戳,字段含义参考文档

https://github.com/botuniverse/onebot-11/blob/master/message/segment.md#%E6%88%B3%E4%B8%80%E6%88%B3

func (*Poke) GetMessageType

func (m *Poke) GetMessageType() string

func (*Poke) String

func (m *Poke) String() string

type PrivateMessage

type PrivateMessage struct {
	Time        int64                 `json:"time"`         // 事件发生的时间戳
	SelfId      int64                 `json:"self_id"`      // 收到事件的机器人 QQ 号
	PostType    string                `json:"post_type"`    // "message"
	MessageType string                `json:"message_type"` // "private"
	SubType     PrivateMessageSubType `json:"sub_type"`     // 消息子类型
	MessageId   int32                 `json:"message_id"`   // 消息 ID
	UserId      int64                 `json:"user_id"`      // 发送者 QQ 号
	Message     MessageChain          `json:"message"`      // 消息内容
	RawMessage  string                `json:"raw_message"`  // 原始消息内容
	Font        int32                 `json:"font"`         // 字体
	Sender      Profile               `json:"sender"`       // 发送人信息
}

PrivateMessage 私聊消息

func (*PrivateMessage) Reply

func (m *PrivateMessage) Reply(b *Bot, reply MessageChain) error

Reply 回复

type PrivateMessageSubType

type PrivateMessageSubType string
const (
	PrivateMessageFriend PrivateMessageSubType = "friend" // 好友私聊
	PrivateMessageGroup  PrivateMessageSubType = "group"  // 群私聊
	PrivateMessageOther  PrivateMessageSubType = "other"  // 其它
)

type Profile

type Profile struct {
	UserId   int64  `json:"user_id"`  // QQ号
	Nickname string `json:"nickname"` // 昵称
	Sex      Sex    `json:"sex"`      // 性别
	Age      int32  `json:"age"`      // 年龄
}

Profile 用户资料

type RPS

type RPS struct {
}

RPS 猜拳魔法表情

func (*RPS) GetMessageType

func (m *RPS) GetMessageType() string

func (*RPS) String

func (m *RPS) String() string

type Record

type Record struct {
	// 语音文件名
	//
	// 当发送语音时,除了可以直接使用收到的文件名直接发送以外,还支持:
	//
	// 绝对路径,格式使用file URI;
	// 网络URL,格式为http://xxx
	// Base64编码,格式为base64://<base64编码>。
	File string `json:"file"`

	// 发送时可选,是否变声
	Magic bool `json:"magic,omitempty"`

	// 语音的URL,只有在收语音时才有这个字段
	Url string `json:"url,omitempty"`

	// 以下字段只在通过网络URL发送时有效
	Cache   string `json:"cache,omitempty"`   // 1和0表示是否使用已缓存的文件,默认是
	Proxy   string `json:"proxy,omitempty"`   // 1和0表示是否通过代理下载文件(需通过环境变量或配置文件配置代理),默认是
	Timeout string `json:"timeout,omitempty"` // 单位秒,表示下载网络文件的超时时间,默认不超时
}

Record 语音

func (*Record) GetMessageType

func (m *Record) GetMessageType() string

func (*Record) String

func (m *Record) String() string

type Reply

type Reply struct {
	Id string `json:"id"` // 回复时引用的消息 ID
}

Reply 回复

func (*Reply) GetMessageType

func (m *Reply) GetMessageType() string

func (*Reply) String

func (m *Reply) String() string

type Role

type Role string
const (
	RoleOwner  Role = "owner"  // 群主
	RoleAdmin  Role = "admin"  // 管理员
	RoleMember Role = "member" // 群成员
)

type Sex

type Sex string
const (
	SexUnknown Sex = "unknown" // 未知
	SexMale    Sex = "male"    // 男
	SexFemale  Sex = "female"  // 女
)

type Shake

type Shake struct {
}

Shake 窗口抖动

func (*Shake) GetMessageType

func (m *Shake) GetMessageType() string

func (*Shake) String

func (m *Shake) String() string

type Share

type Share struct {
	Url     string `json:"url"`               // URL
	Title   string `json:"title"`             // 标题
	Content string `json:"content,omitempty"` // 内容描述,发送时可不填
	Image   string `json:"image,omitempty"`   // 图片URL,发送时可不填
}

Share 链接分享

func (*Share) GetMessageType

func (m *Share) GetMessageType() string

func (*Share) String

func (m *Share) String() string

type SingleMessage

type SingleMessage interface {
	GetMessageType() string
}

type Text

type Text struct {
	Text string `json:"text"` // 文字消息
}

Text 纯文本

func (*Text) GetMessageType

func (m *Text) GetMessageType() string

func (*Text) String

func (m *Text) String() string

type Video

type Video struct {
	// 短视频文件名
	//
	// 当发送短视频时,除了可以直接使用收到的文件名直接发送以外,还支持:
	//
	// 绝对路径,格式使用file URI;
	// 网络URL,格式为http://xxx
	// Base64编码,格式为base64://<base64编码>。
	File string `json:"file"`

	// 短视频的URL,只有在收短视频时才有这个字段
	Url string `json:"url,omitempty"`

	// 以下字段只在通过网络URL发送时有效
	Cache   string `json:"cache,omitempty"`   // 1和0表示是否使用已缓存的文件,默认是
	Proxy   string `json:"proxy,omitempty"`   // 1和0表示是否通过代理下载文件(需通过环境变量或配置文件配置代理),默认是
	Timeout string `json:"timeout,omitempty"` // 单位秒,表示下载网络文件的超时时间,默认不超时
}

Video 短视频

func (*Video) GetMessageType

func (m *Video) GetMessageType() string

func (*Video) String

func (m *Video) String() string

type WsChannel

type WsChannel string

WsChannel 连接通道

type Xml

type Xml struct {
	Data string `json:"data"` // XML文本
}

func (*Xml) GetMessageType

func (m *Xml) GetMessageType() string

func (*Xml) String

func (m *Xml) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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