cqhttp_bot

package module
v0.0.0-...-037c65e Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2026 License: MIT Imports: 16 Imported by: 0

README

go-cqhttp api简单封装

由于GO-CQHTTP#2471原因,决定逐渐放缓cqhttp_bot开发

GoDoc

Installation

go get -u github.com/Rehtt/qbot/cqhttp_bot

Use

初始化

bot := cqhttp_bot.New("ws://127.0.0.1:8080")
//bot.Start() //  已非阻塞的方式运行
bot.Run()   //  已阻塞的方式运行

事件

// 群消息事件
bot.Event.OnGroupMessage(func (senderQid, groupId int64, message *EventMessage) {
    fmt.Println(senderQid, groupId, message.Messages, message.RawMessage)
})

// 私人消息事件
bot.OnPrivateMessage(func (userId int64, message *EventMessage) {
    fmt.Println(userId, message.Messages)
})

请求

// 获取好友列表
bot.GetFriendsList()

// 发送消息
bot.SendMsg(<目标qq>,<消息>,<EventMessageType>)

Documentation

Index

Constants

View Source
const (
	Private = EventMessageType("private")
	Group   = EventMessageType("group")
)
View Source
const (
	TEXT = MessageType(iota) + 1
	IMAGE
	REPLY
	AT
	VIDEO
)
View Source
const (
	Add    = GroupRequestEventSubType("add")    // 加入
	Invite = GroupRequestEventSubType("invite") // 邀请
)

Variables

This section is empty.

Functions

func DeepCopy

func DeepCopy(dst, src any)

func GenCode

func GenCode(data []byte) string

GenCode 生成唯一编码

func Unescape

func Unescape(str string) string

Types

type Action

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

func (*Action) GetFriendsCategoryList

func (b *Action) GetFriendsCategoryList() ([]FriendCategory, error)

GetFriendsCategoryList 获取好友分组

func (*Action) GetFriendsList

func (b *Action) GetFriendsList(noCache bool) ([]Friend, error)

GetFriendsList 获取好友列表

func (*Action) GetMsg

func (b *Action) GetMsg(messageId string) (message *Message, err error)

GetMsg 根据message_id获取消息

func (*Action) GetSelfInfo

func (b *Action) GetSelfInfo() (*SelfInfo, error)

func (*Action) Send

func (b *Action) Send(action string, data any) (jsoniter.Any, error)

func (*Action) SendMsg

func (b *Action) SendMsg(qid any, msg Messages, ty EventMessageType, autoEscape ...bool) (int32, error)

SendMsg 发送消息

@param qid 目标,私聊为对方qq号,群聊为群号 @param msg 消息 @param ty 消息类型,私聊为Private,群聊为Group

@return id 消息id @return error

func (*Action) SendPrivateMsg

func (b *Action) SendPrivateMsg(userId int64, msg string) (int32, error)

func (*Action) SetFriendAddRequest

func (b *Action) SetFriendAddRequest(flag string, approve bool, remark ...string) error

func (*Action) SetGroupAddRequest

func (b *Action) SetGroupAddRequest(flag string, subType GroupRequestEventSubType, approve bool, reason ...string) error

type Bot

type Bot struct {
	*Options
	Event
	Action
	// contains filtered or unexported fields
}

func New

func New(addr string, options ...Option) (b *Bot, err error)

New 实例化一个Bot对象

func (*Bot) Run

func (b *Bot) Run()

Run 已阻塞的方式运行

func (*Bot) Start

func (b *Bot) Start()

Start 已非阻塞的方式运行

type Event

type Event struct {
	MessageEvent
	RequestEvent
	NoticeEvent
	// contains filtered or unexported fields
}

func (*Event) AllEventCallback

func (e *Event) AllEventCallback(f EventCallback)

所有事件回调

func (*Event) SpecifyEventCallback

func (e *Event) SpecifyEventCallback(event string, f EventCallback)

指定事件回调

type EventCallback

type EventCallback func(event string, data jsoniter.Any)

type EventMessage

type EventMessage struct {
	RawMessage string
	Messages   []Message
}

func NewEventMessage

func NewEventMessage() *EventMessage

func (*EventMessage) Close

func (e *EventMessage) Close()

type EventMessageContext

type EventMessageContext struct {
	MessageId   int32
	MessageSeq  int64 // 只有群消息有
	MessageType EventMessageType
	Time        time.Time
	SenderId    int64
	Sender      Sender
	GroupId     int64
	Message     *EventMessage
}

func NewEventMessageContext

func NewEventMessageContext() *EventMessageContext

func ParseEventMessage

func ParseEventMessage(data jsoniter.Any) *EventMessageContext

func (*EventMessageContext) Close

func (e *EventMessageContext) Close()

func (*EventMessageContext) QuickReply

func (e *EventMessageContext) QuickReply(bot *Bot, msg Messages, at ...bool) (int32, error)

QuickReply 对消息快速回复 @param at @发送人,仅当消息类型为Group时有效

func (*EventMessageContext) QuickReplyText

func (e *EventMessageContext) QuickReplyText(bot *Bot, msg string, at ...bool) (int32, error)

QuickReplyText 对消息快速回复 @param at @发送人,仅当消息类型为Group时有效

type EventMessageType

type EventMessageType string

type Friend

type Friend struct {
	UserId        int64  `json:"user_id"`
	Nickname      string `json:"nickname"`
	Remark        string `json:"remark"`
	CategoryId    int64  `json:"category_id"`
	Email         string `json:"email"`
	Age           int    `json:"age"`
	Sex           string `json:"sex"`
	Level         int    `json:"level"`
	BirthdayYear  int    `json:"birthday_year"`
	BirthdayMonth int    `json:"birthday_month"`
	BirthdayDay   int    `json:"birthday_day"`
}

type FriendCategory

type FriendCategory struct {
	CategoryId      int64     `json:"categoryId"`
	CategorySortId  int64     `json:"categorySortId"`
	CategoryName    string    `json:"categoryName"`
	CategoryMbCount int64     `json:"categoryMbCount"`
	OnlineCount     int64     `json:"onlineCount"`
	Friends         []*Friend `json:"buddyList"`
}

type GroupRequestEventSubType

type GroupRequestEventSubType string

type Message

type Message struct {
	Type  MessageType
	Image *messageImage
	At    *messageAt
	Text  string
	Video *messageVideo
}

func AtMessage

func AtMessage(qid any, name ...string) Message

func ImageMessage

func ImageMessage(file, url string, src []byte, flash bool) Message

ImageMessage file,url,src 三选一。优先级:src > url > file

func ReplyMessage

func ReplyMessage(messageId string) Message

func TextMessage

func TextMessage(text string) Message

func VideoMessage

func VideoMessage(file, url string) Message

type MessageEvent

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

func (*MessageEvent) OnGroupMessage

func (b *MessageEvent) OnGroupMessage(f OnGroupMessageFunc)

OnGroupMessage 接收群消息

func (*MessageEvent) OnMessage

func (b *MessageEvent) OnMessage(f OnMessageFunc)

OnMessage 接收所有消息

func (*MessageEvent) OnPrivateMessage

func (b *MessageEvent) OnPrivateMessage(f OnPrivateMessageFunc)

OnPrivateMessage 接收私人消息

type MessageType

type MessageType uint8

type Messages

type Messages []Message

func MessageArray

func MessageArray(msg ...Message) Messages

func ParseMessage

func ParseMessage(raw string) (m Messages)

func (*Messages) Add

func (m *Messages) Add(msg Message) Messages

func (*Messages) AtMessage

func (m *Messages) AtMessage(qid any, name ...string) Messages

func (*Messages) ImageMessage

func (m *Messages) ImageMessage(file, url string, src []byte, flash bool) Messages

func (*Messages) OutputArrayMessage

func (m *Messages) OutputArrayMessage()

func (*Messages) OutputCQMessage

func (m *Messages) OutputCQMessage() string

OutputCQMessage 输出 CQ 码

func (*Messages) ReplyMessage

func (m *Messages) ReplyMessage(messageId string) Messages

func (*Messages) TextMessage

func (m *Messages) TextMessage(text string) Messages

type Msg

type Msg struct {
	MessageType EventMessageType `json:"message_type"`
	UserId      any              `json:"user_id"`
	GroupId     any              `json:"group_id"`
	Message     string           `json:"message"`
	AutoEscape  *bool            `json:"auto_escape,omitempty"` // 消息内容是否作为纯文本发送 ( 即不解析 CQ 码 ) , 只在 message 字段是字符串时有效
}

type NoticeEvent

type NoticeEvent struct{}

type OnFriendRequestFunc

type OnFriendRequestFunc func(userId int64, comment string, flag string)

type OnGroupMessageFunc

type OnGroupMessageFunc func(messageId int32, senderQid, groupId int64, message *EventMessage)

type OnGroupRequestFunc

type OnGroupRequestFunc func(userId, groupId int64, requestType GroupRequestEventSubType, comment, flag string)

type OnMessageFunc

type OnMessageFunc func(ctx *EventMessageContext)

type OnPrivateMessageFunc

type OnPrivateMessageFunc func(messageId int32, userId int64, message *EventMessage)

type Option

type Option func(options *Options)

func AddRequestHeader

func AddRequestHeader(key, value string) Option

func WithAuthorizationBasic

func WithAuthorizationBasic(username, password string) Option

func WithAuthorizationBearer

func WithAuthorizationBearer(token string) Option

func WithHandleThreadNum

func WithHandleThreadNum(n int) Option

WithHandleThreadNum 默认为200,如果数值太小会导致处理出现延时

func WithLogger

func WithLogger(l *slog.Logger) Option

func WithRequestHead

func WithRequestHead(h http.Header) Option

WithRequestHead 设置请求头,会覆盖默认请求头

type Options

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

func (*Options) Log

func (o *Options) Log() *slog.Logger

type RequestEvent

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

func (*RequestEvent) OnFriendRequest

func (r *RequestEvent) OnFriendRequest(f OnFriendRequestFunc)

func (*RequestEvent) OnGroupRequest

func (r *RequestEvent) OnGroupRequest(f OnGroupRequestFunc)

type SelfInfo

type SelfInfo struct {
	UserId   int64  `json:"user_id"`
	Nickname string `json:"nickname"`

	Company      string `json:"company"`
	Email        string `json:"email"`
	College      string `json:"college"`
	PersonalNote string `json:"personal_note"`
}

type Sender

type Sender struct {
	Nickname string
	UserId   int64
	Card     string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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