From 784030f714c905bf12c8af0261d0de408d8440c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Wed, 11 Oct 2023 23:52:49 +0900 Subject: [PATCH] finish message --- openapi_markdown.go | 88 +++++++++++++++++++++++++++++++++++++++++++++ openapi_member.go | 2 +- openapi_message.go | 44 ++++++++++++++++++----- 3 files changed, 125 insertions(+), 9 deletions(-) diff --git a/openapi_markdown.go b/openapi_markdown.go index 946a39f..fa46cc3 100644 --- a/openapi_markdown.go +++ b/openapi_markdown.go @@ -1,4 +1,92 @@ package nano +// MessageMarkdown https://bot.q.qq.com/wiki/develop/api/openapi/message/model.html#messagemarkdown type MessageMarkdown struct { + TemplateID int `json:"template_id,omitempty"` + CustomTemplateID string `json:"custom_template_id,omitempty"` + Params []MessageMarkdownParams `json:"params,omitempty"` + Content string `json:"content,omitempty"` // 原生 markdown 内容,与上面三个参数互斥,参数都传值将报错 +} + +// MessageMarkdownParams https://bot.q.qq.com/wiki/develop/api/openapi/message/model.html#messagemarkdownparams +type MessageMarkdownParams struct { + Key string `json:"key"` + Values []string `json:"values"` +} + +// MessageKeyboard https://bot.q.qq.com/wiki/develop/api/openapi/message/model.html#messagekeyboard +type MessageKeyboard struct { + ID string `json:"id,omitempty"` + Content *InlineKeyboard `json:"content,omitempty"` // 自定义 keyboard 内容,与 id 参数互斥,参数都传值将报错 +} + +// InlineKeyboard 消息按钮对象 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/message/message_keyboard.html +type InlineKeyboard struct { + Rows []InlineKeyboardRow `json:"rows"` + BotAppID int `json:"bot_appid"` +} + +// InlineKeyboardRow https://bot.q.qq.com/wiki/develop/api/openapi/message/message_keyboard.html#inlinekeyboardrow +type InlineKeyboardRow struct { + Buttons []InlineKeyboardButton `json:"buttons"` +} + +// InlineKeyboardButton https://bot.q.qq.com/wiki/develop/api/openapi/message/message_keyboard.html#button +type InlineKeyboardButton struct { + ID string `json:"id"` + RenderData InlineKeyboardButtonRenderData `json:"render_data"` + Action InlineKeyboardButtonAction `json:"action"` +} + +// InlineKeyboardButtonRenderDataStyle https://bot.q.qq.com/wiki/develop/api/openapi/message/message_keyboard.html#renderstyle +type InlineKeyboardButtonRenderDataStyle int + +const ( + InlineKeyboardButtonRenderDataStyleGray InlineKeyboardButtonRenderDataStyle = iota + InlineKeyboardButtonRenderDataStyleBlue +) + +// InlineKeyboardButtonRenderData https://bot.q.qq.com/wiki/develop/api/openapi/message/message_keyboard.html#renderdata +type InlineKeyboardButtonRenderData struct { + Label string `json:"label"` + VisitedLabel string `json:"visited_label"` + Style InlineKeyboardButtonRenderDataStyle `json:"style"` +} + +// InlineKeyboardButtonActionType https://bot.q.qq.com/wiki/develop/api/openapi/message/message_keyboard.html#actiontype +type InlineKeyboardButtonActionType int + +const ( + InlineKeyboardButtonActionTypeHTTP InlineKeyboardButtonActionType = iota + InlineKeyboardButtonActionTypeCallback + InlineKeyboardButtonActionTypeAtBot +) + +// InlineKeyboardButtonAction https://bot.q.qq.com/wiki/develop/api/openapi/message/message_keyboard.html#action +type InlineKeyboardButtonAction struct { + Type InlineKeyboardButtonActionType `json:"type"` + Permission InlineKeyboardButtonActionPermission `json:"permission"` + ClickLimit int `json:"click_limit"` + UnsupportTips string `json:"unsupport_tips"` + Data string `json:"data"` + AtBotShowChannelList bool `json:"at_bot_show_channel_list"` +} + +// InlineKeyboardButtonActionPermissionType https://bot.q.qq.com/wiki/develop/api/openapi/message/message_keyboard.html#permissiontype +type InlineKeyboardButtonActionPermissionType int + +const ( + InlineKeyboardButtonActionPermissionTypeShimeiUser InlineKeyboardButtonActionPermissionType = iota + InlineKeyboardButtonActionPermissionTypeAdmin + InlineKeyboardButtonActionPermissionTypeAll + InlineKeyboardButtonActionPermissionTypeShimeiRole +) + +// InlineKeyboardButtonActionPermission https://bot.q.qq.com/wiki/develop/api/openapi/message/message_keyboard.html#permission +type InlineKeyboardButtonActionPermission struct { + Type InlineKeyboardButtonActionPermissionType `json:"type"` + SpecifyRoleIDs []string `json:"specify_role_ids"` + SpecifyUserIDs []string `json:"specify_user_ids"` } diff --git a/openapi_member.go b/openapi_member.go index b7d30c4..92b7928 100644 --- a/openapi_member.go +++ b/openapi_member.go @@ -7,7 +7,7 @@ import "time" // https://bot.q.qq.com/wiki/develop/api/openapi/member/model.html type Member struct { GuildID string `json:"guild_id"` // MemberWithGuildID only - User User `json:"user"` + User *User `json:"user"` Nick string `json:"nick"` Roles []string `json:"roles"` JoinedAt time.Time `json:"joined_at"` diff --git a/openapi_message.go b/openapi_message.go index d0a97cc..3d1a5b3 100644 --- a/openapi_message.go +++ b/openapi_message.go @@ -3,6 +3,7 @@ package nano import ( "encoding/json" "reflect" + "strings" "time" "github.com/pkg/errors" @@ -23,13 +24,13 @@ type Message struct { Timestamp time.Time `json:"timestamp"` EditedTimestamp time.Time `json:"edited_timestamp"` MentionEveryone bool `json:"mention_everyone"` - Author User `json:"author"` + Author *User `json:"author"` Attachments []MessageAttachment `json:"attachments"` Embeds []MessageEmbed `json:"embeds"` - Member Member `json:"member"` - Ark MessageArk `json:"ark"` + Member *Member `json:"member"` + Ark *MessageArk `json:"ark"` SeqInChannel string `json:"seq_in_channel"` - MessageReference MessageReference `json:"message_reference"` + MessageReference *MessageReference `json:"message_reference"` SrcGuildID string `json:"src_guild_id"` Data *struct { MessageAudit *MessageAudited `json:"message_audit,omitempty"` @@ -38,10 +39,10 @@ type Message struct { // MessageEmbed https://bot.q.qq.com/wiki/develop/api/openapi/message/model.html#messageembed type MessageEmbed struct { - Title string `json:"title"` - Prompt string `json:"prompt"` - Thumbnail MessageEmbedThumbnail `json:"thumbnail"` - Fields []MessageEmbedField `json:"fields"` + Title string `json:"title"` + Prompt string `json:"prompt"` + Thumbnail *MessageEmbedThumbnail `json:"thumbnail"` + Fields []MessageEmbedField `json:"fields"` } // MessageEmbedThumbnail https://bot.q.qq.com/wiki/develop/api/openapi/message/model.html#messageembedthumbnail @@ -89,6 +90,12 @@ type MessageReference struct { IgnoreGetMessageError bool `json:"ignore_get_message_error"` } +// MessageDelete https://bot.q.qq.com/wiki/develop/api/openapi/message/model.html#messagedelete +type MessageDelete struct { + Message *Message `json:"message"` + OpUser *User `json:"op_user"` +} + // MessageAudited 消息审核对象 // // https://bot.q.qq.com/wiki/develop/api/openapi/message/model.html#%E6%B6%88%E6%81%AF%E5%AE%A1%E6%A0%B8%E5%AF%B9%E8%B1%A1-messageaudited @@ -116,6 +123,27 @@ type MessagePost struct { ReplyMessageID string `json:"msg_id,omitempty"` ReplyEventID string `json:"event_id,omitempty"` Markdown *MessageMarkdown `json:"markdown,omitempty"` + KeyBoard *MessageKeyboard `json:"keyboard,omitempty"` +} + +// MessageEscape 消息转义 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/message/message_format.html +func MessageEscape(text string) string { + text = strings.ReplaceAll(text, "&", "&") + text = strings.ReplaceAll(text, "<", "<") + text = strings.ReplaceAll(text, ">", ">") + return text +} + +// MessageUnescape 消息解转义 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/message/message_format.html +func MessageUnescape(text string) string { + text = strings.ReplaceAll(text, "&", "&") + text = strings.ReplaceAll(text, "<", "<") + text = strings.ReplaceAll(text, ">", ">") + return text } // PostMessageToChannel 向 channel_id 指定的子频道发送消息