cqhttp_bot

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

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

Go to latest
Published: Oct 16, 2023 License: MIT Imports: 15 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
)
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 生成唯一编码

Types

type Action

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

func (*Action) GetFriendsList

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

GetFriendsList 获取好友列表

func (*Action) GetMsg

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

GetMsg 根据message_id获取消息

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
}

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
	MessageType EventMessageType
	SenderId    int64
	Sender      Sender
	GroupId     int64
	Message     *EventMessage
}

func NewEventMessageContext

func NewEventMessageContext() *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"`
}

type GroupRequestEventSubType

type GroupRequestEventSubType string

type Message

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

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

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) RawMessage

func (m *Messages) RawMessage() string

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 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 WithDebug

func WithDebug(n bool) Option

func WithHandleThreadNum

func WithHandleThreadNum(n int) Option

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

type Options

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

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 Sender

type Sender struct {
	Nickname string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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