botoy

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

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

Go to latest
Published: Mar 20, 2021 License: MIT Imports: 14 Imported by: 0

README

go-botoy

一个极简的 opq 框架

go get github.com/xiyaowong/go-botoy

该项目有较多代码参考自https://github.com/mcoo/OPQBot/commit/b4bff5a002542d5097a66fbd22b07b5ffa1966d2这条 commit 时已有的代码。相关内容已获得许可。

玩游戏,已经快大半年没写代码了,突然看到群里这个项目,和我自己的想法有一定重合,但又有不同的地方,所以就想按自己想法参考着写一个很简单的小框架(学到了一点反射的用法), 因为会的 go 知识仍然停留在以前的刚刚入门水平,加上没时间,这个项目应该是不会完善和维护(一时兴起想敲键盘了)。

写一个 opq 的框架很简单,能收能发就够用了,专注消息处理的框架就另说了。这个小框架用法很简单,收发文字图片是够用的, 至于异步和队列的问题应该在 go 里面是很容易解决的。

感兴趣可以看看

请看示例godoc

License

MIT

Documentation

Overview

Package botoy 极简的OPQ开发框架

这是一个十分简单的框架,模块的方法很少,只封装了常用的几个功能, 比如发文字,发图。。。但在多数情况下够用了

Index

Constants

View Source
const (
	AtMsgType          = "AtMsg"
	PicMsgType         = "PicMsg"
	TextMsgType        = "TextMsg"
	ReplyMsgType       = "ReplyMsg"
	ReplyMsgAType      = "ReplayMsg"
	VoiceMsgType       = "VoiceMsg"
	VideoMsgType       = "VideoMsg"
	PhoneMsgType       = "PhoneMsg"
	RedBagMsgType      = "RedBagMsg"
	TempSessionMsgType = "TempSessionMsg"
	FriendFileMsgType  = "FriendFileMsg"
	GroupFileMsgType   = "GroupFileMsg"

	GroupRevokeEventType         = "ON_EVENT_GROUP_REVOKE"
	GroupAdminsysnotifyEventType = "ON_EVENT_GROUP_ADMINSYSNOTIFY"
	FriendRevokeEventType        = "ON_EVENT_FRIEND_REVOKE"
	GroupJoinEventType           = "ON_EVENT_GROUP_JOIN"
	GroupExitEventType           = "ON_EVENT_GROUP_EXIT"
	FriendDeleteEventType        = "ON_EVENT_FRIEND_DELETE"
	GroupShutEventType           = "ON_EVENT_GROUP_SHUT"
	GroupAdminEventType          = "ON_EVENT_GROUP_ADMIN"
	NotifyPushaddfrdEventType    = "ON_EVENT_NOTIFY_PUSHADDFRD"
	FriendAddedEventType         = "ON_EVENT_FRIEND_ADDED"
	GroupExitSuccEventType       = "ON_EVENT_GROUP_EXIT_SUCC"
)

Variables

View Source
var (
	ErrNotFunction      = errors.New("不是函数类型")
	ErrWrongArguments   = errors.New("参数有误,比如数目不对")
	ErrNotSupportedType = errors.New("不支持的类型")
	ErrInvalidBotID     = errors.New("无效的机器人QQ号")
)

Functions

This section is empty.

Types

type Botoy

type Botoy struct {
	// 机器人QQ号
	BotID int64
	// 连接地址 如: http://127.0.0.1:8888
	Address string
	// contains filtered or unexported fields
}

func NewBotoy

func NewBotoy(bot int64, address string) *Botoy

NewBotoy 新建Botoy

如果bot为0,会监听所有机器人的消息,如果为一个确定的qq号,则只会接收该QQ号机器人所收到的消息

如果bot为0,将无法使用 Do 方法

func (*Botoy) AddReceiver

func (bot *Botoy) AddReceiver(receiver ...interface{}) (err error)

AddReceiver 添加对应消息的处理函数

参数必须是函数,参数必须为模块内名字以Pack结尾的类型

可以添加多个函数,收到消息后各个函数异步执行

每个消息都可以添加多个函数进行处理

使用示例:

bot.AddReceiver(
    func(pack botoy.FriendMsgPack) {
        fmt.Printf("收到一条好友消息, 发送人: %d, 内容: %s\n", pack.FromUin, pack.Content)
    },
    func(pack botoy.GroupMsgPack) {
        fmt.Printf("收到一条群消息, 发送人: %d, 群id: %d, 内容: %s\n", pack.FromUserID, pack.FromGroupID, pack.Content)
    },
    func(pack botoy.ConnectedPack) {
        fmt.Println("socketio已连接!")
    },
)
bot.AddReceiver(
    func(pack botoy.FriendRevokePack) {
        fmt.Printf("好友【%d】撤回了一条消息!\n", pack.Eventdata.Userid)
    },
)
bot.AddReceiver(
    func(pack botoy.GroupMsgPack) {
        fmt.Printf("收到一条群消息, 消息类型为: %s\n", pack.MsgType)
    },
)

func (*Botoy) Do

func (bot *Botoy) Do(payload payload) (resp *requests.Response, err error)

Do 进行发送等操作

接收参数为模块内以Payload结尾的结构体,每个结构体内的字段无需全部指定

对于哪些是必须指定的字段,需要你自己十分了解机器人服务端api的使用

func (*Botoy) Start

func (bot *Botoy) Start() (err error)

Start 启动

func (*Botoy) Stop

func (bot *Botoy) Stop()

Stop 停止

type ConnectedPack

type ConnectedPack struct{}

ConnectedPack socketio连接上

type DisConnectedPack

type DisConnectedPack struct{}

DisConnectedPack socketio 断开连接

type FriendMsgPack

type FriendMsgPack struct {
	CurrentQQ  int64       `json:"CurrentQQ,omitempty"`
	Content    string      `json:"Content"`
	FromUin    int64       `json:"FromUin"`
	MsgSeq     int         `json:"MsgSeq"`
	MsgType    string      `json:"MsgType"`
	ToUin      int64       `json:"ToUin"`
	TempUin    int64       `json:"TempUin"`
	RedBaginfo interface{} `json:"RedBaginfo"`
}

FriendMsgPack 好友消息

type FriendPicPayload

type FriendPicPayload struct {
	ToUser    int64
	Content   string
	PicURL    string
	PicBase64 string
	FileMd5   string
	Flash     bool
}

FriendPicPayload 好友图片消息

type FriendRevokePack

type FriendRevokePack struct {
	CurrentQQ int64 `json:"CurrentQQ,omitempty"`
	Eventdata struct {
		Msgseq int `json:"MsgSeq"`
		Userid int `json:"UserID"`
	} `json:"EventData"`
	Eventmsg struct {
		Fromuin    int         `json:"FromUin"`
		Touin      int         `json:"ToUin"`
		Msgtype    string      `json:"MsgType"`
		Msgseq     int         `json:"MsgSeq"`
		Content    string      `json:"Content"`
		Redbaginfo interface{} `json:"RedBaginfo"`
	} `json:"EventMsg"`
	Eventname string `json:"EventName"`
}

FriendRevokePack 好友撤回事件

type FriendTextPayload

type FriendTextPayload struct {
	ToUserID int64
	Text     string
}

FriendTextPayload 好友文字消息

type FriendVoicePayload

type FriendVoicePayload struct {
	ToUser      int64
	VoiceURL    string
	VoiceBase64 string
}

FriendVoicePayload 好友语音消息

type GroupExitPack

type GroupExitPack struct {
	CurrentQQ int64 `json:"CurrentQQ,omitempty"`
	EventData struct {
		UserID int64 `json:"UserID"`
	} `json:"EventData"`
	EventMsg struct {
		FromUin    int64       `json:"FromUin"`
		ToUin      int64       `json:"ToUin"`
		MsgType    string      `json:"MsgType"`
		MsgSeq     int         `json:"MsgSeq"`
		Content    string      `json:"Content"`
		RedBaginfo interface{} `json:"RedBaginfo"`
	} `json:"EventMsg"`
}

GroupExitPack 退群事件

type GroupExitSuccessPack

type GroupExitSuccessPack struct {
	CurrentQQ int64 `json:"CurrentQQ,omitempty"`
	EventData struct {
		GroupID int64 `json:"GroupID"`
	} `json:"EventData"`
	EventMsg struct {
		FromUin    int64       `json:"FromUin"`
		ToUin      int64       `json:"ToUin"`
		MsgType    string      `json:"MsgType"`
		MsgSeq     int         `json:"MsgSeq"`
		Content    string      `json:"Content"`
		RedBaginfo interface{} `json:"RedBaginfo"`
	} `json:"EventMsg"`
}

GroupExitSuccessPack 退群事件

type GroupJSONPayload

type GroupJSONPayload struct {
	GroupID int64
	JSONStr string
}

GroupJSONPayload 群json消息

type GroupJoinPack

type GroupJoinPack struct {
	CurrentQQ int64 `json:"CurrentQQ,omitempty"`
	EventData struct {
		InviteUin int64  `json:"InviteUin"`
		UserID    int64  `json:"UserID"`
		UserName  string `json:"UserName"`
	} `json:"EventData"`
	EventMsg struct {
		FromUin    int64       `json:"FromUin"`
		ToUin      int64       `json:"ToUin"`
		MsgType    string      `json:"MsgType"`
		MsgSeq     int         `json:"MsgSeq"`
		Content    string      `json:"Content"`
		RedBaginfo interface{} `json:"RedBaginfo"`
	} `json:"EventMsg"`
}

GroupJoinPack 加群事件

type GroupMsgPack

type GroupMsgPack struct {
	CurrentQQ     int64       `json:"CurrentQQ,omitempty"`
	Content       string      `json:"Content"`
	FromGroupID   int64       `json:"FromGroupId"`
	FromGroupName string      `json:"FromGroupName"`
	FromNickName  string      `json:"FromNickName"`
	FromUserID    int64       `json:"FromUserId"`
	MsgRandom     int         `json:"MsgRandom"`
	MsgSeq        int         `json:"MsgSeq"`
	MsgTime       int         `json:"MsgTime"`
	MsgType       string      `json:"MsgType"`
	RedBaginfo    interface{} `json:"RedBaginfo"`
}

GroupMsgPack 群消息

type GroupPicPayload

type GroupPicPayload struct {
	GroupID   int64
	Content   string
	PicURL    string
	PicBase64 string
	PicMD5s   []string
	PicMD5    string
	Flash     bool
}

GroupPicPayload 群图片消息

type GroupReplyPayload

type GroupReplyPayload struct {
	GroupID       int64
	Content       string
	RawMsgSeq     int
	RawMsgTime    int
	RawMsgUserID  int64
	RawMsgContent string
}

GroupReplyPayload 群回复消息

type GroupRevokePack

type GroupRevokePack struct {
	CurrentQQ int64 `json:"CurrentQQ,omitempty"`
	EventData struct {
		AdminUserID int   `json:"AdminUserID"`
		GroupID     int64 `json:"GroupID"`
		MsgRandom   int64 `json:"MsgRandom"`
		MsgSeq      int   `json:"MsgSeq"`
		UserID      int64 `json:"UserID"`
	} `json:"EventData"`
	EventMsg struct {
		FromUin    int64       `json:"FromUin"`
		ToUin      int64       `json:"ToUin"`
		MsgType    string      `json:"MsgType"`
		MsgSeq     int         `json:"MsgSeq"`
		Content    string      `json:"Content"`
		RedBaginfo interface{} `json:"RedBaginfo"`
	} `json:"EventMsg"`
}

GroupRevokePack 群撤回消息

type GroupShutPack

type GroupShutPack struct {
	CurrentQQ int64 `json:"CurrentQQ,omitempty"`
	EventData struct {
		GroupID  int64 `json:"GroupID"`
		ShutTime int   `json:"ShutTime"`
		UserID   int64 `json:"UserID"`
	} `json:"EventData"`
	EventMsg struct {
		FromUin    int64       `json:"FromUin"`
		ToUin      int64       `json:"ToUin"`
		MsgType    string      `json:"MsgType"`
		MsgSeq     int         `json:"MsgSeq"`
		Content    string      `json:"Content"`
		RedBaginfo interface{} `json:"RedBaginfo"`
	} `json:"EventMsg"`
}

GroupShutPack 群禁言

type GroupTextPayload

type GroupTextPayload struct {
	GroupID int64
	Text    string
}

GroupTextPayload 群文字消息

type GroupVoicePayload

type GroupVoicePayload struct {
	GroupID     int64
	VoiceURL    string
	VoiceBase64 string
}

GroupVoicePayload 群语音消息

type GroupXMLPayload

type GroupXMLPayload struct {
	GroupID int64
	XMLStr  string
}

GroupXMLPayload 群XML消息

type PhoneTextPayload

type PhoneTextPayload struct {
	Text string
}

PhoneTextPayload 给手机发文字消息

type PrivatePicPayload

type PrivatePicPayload struct {
	ToUserID  int64
	GroupID   int64
	PicURL    string
	PicBase64 string
	FileMd5   string
	Content   string
	Flash     bool
}

PrivatePicPayload 私聊图片消息

type PrivateTextPayload

type PrivateTextPayload struct {
	ToUserID int64
	GroupID  int64
	Text     string
}

PrivateTextPayload 私聊文字消息

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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