diff --git a/helper.go b/helper.go index 31a765f..7af0020 100644 --- a/helper.go +++ b/helper.go @@ -27,3 +27,23 @@ func getThisFuncName() string { func getCallerFuncName() string { return getFuncNameWithSkip(2) } + +// 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 +} diff --git a/openapi.go b/openapi.go index 6c3e6d2..eb7f0c4 100644 --- a/openapi.go +++ b/openapi.go @@ -103,7 +103,7 @@ func (bot *Bot) DeleteOpenAPI(ep, contenttype string, body io.Reader) error { return nil } -//go:generate go run codegen/postopenapiof/main.go Channel GuildRoleCreate Message +//go:generate go run codegen/postopenapiof/main.go Channel GuildRoleCreate Message DMS // PostOpenAPI 从 ep 得到 json 结构化数据返回值写到 ptr, ptr 除 Slice 外必须在开头继承 CodeMessageBase func (bot *Bot) PostOpenAPI(ep, contenttype string, ptr any, body io.Reader) error { diff --git a/openapi_codegen_postopenapiof.go b/openapi_codegen_postopenapiof.go index 4ee7517..7d305e5 100644 --- a/openapi_codegen_postopenapiof.go +++ b/openapi_codegen_postopenapiof.go @@ -43,3 +43,15 @@ func (bot *Bot) postOpenAPIofMessage(ep, contenttype string, body io.Reader) (*M } return &resp.Message, err } + +func (bot *Bot) postOpenAPIofDMS(ep, contenttype string, body io.Reader) (*DMS, error) { + resp := &struct { + CodeMessageBase + DMS + }{} + err := bot.PostOpenAPI(ep, contenttype, resp, body) + if err != nil { + err = errors.Wrap(err, getCallerFuncName()) + } + return &resp.DMS, err +} diff --git a/openapi_dms.go b/openapi_dms.go new file mode 100644 index 0000000..1d0b650 --- /dev/null +++ b/openapi_dms.go @@ -0,0 +1,38 @@ +package nano + +// DMS 私信会话对象 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/dms/model.html +type DMS struct { + GuildID string `json:"guild_id"` + ChannelID string `json:"channel_id"` + CreateTime string `json:"create_time"` // 创建私信会话时间戳 +} + +// CreatePrivateChat 机器人和在同一个频道内的成员创建私信会话 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/dms/post_dms.html +func (bot *Bot) CreatePrivateChat(guildid, userid string) (*DMS, error) { + return bot.postOpenAPIofDMS("/users/@me/dms", "", WriteBodyFromJSON(&struct { + R string `json:"recipient_id"` + S string `json:"source_guild_id"` + }{userid, guildid})) +} + +// PostMessageToUser 发送私信消息,前提是已经创建了私信会话 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/dms/post_dms_messages.html +// +// - 私信的 guild_id 在创建私信会话时以及私信消息事件中获取 +func (bot *Bot) PostMessageToUser(id string, content *MessagePost) (*Message, error) { + return bot.postMessageTo("/dms/"+id+"/messages", content) +} + +// DeleteMessageOfUser 撤回私信频道 guild_id 中 message_id 指定的私信消息, 只能用于撤回机器人自己发送的私信 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/dms/delete_dms.html +func (bot *Bot) DeleteMessageOfUser(guildid, messageid string, hidetip bool) error { + return bot.DeleteOpenAPI(WriteHTTPQueryIfNotNil("/dms/"+guildid+"/messages/"+messageid, + "hidetip", hidetip, + ), "", nil) +} diff --git a/openapi_message.go b/openapi_message.go index cceae1c..d089b36 100644 --- a/openapi_message.go +++ b/openapi_message.go @@ -3,7 +3,6 @@ package nano import ( "encoding/json" "reflect" - "strings" "time" "github.com/pkg/errors" @@ -126,32 +125,9 @@ type MessagePost struct { 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 指定的子频道发送消息 -// -// https://bot.q.qq.com/wiki/develop/api/openapi/message/post_messages.html -func (bot *Bot) PostMessageToChannel(id string, content *MessagePost) (*Message, error) { +func (bot *Bot) postMessageTo(ep string, content *MessagePost) (*Message, error) { if content.ImageFile == "" { - return bot.postOpenAPIofMessage("/channels/"+id+"/messages", "", WriteBodyFromJSON(content)) + return bot.postOpenAPIofMessage(ep, "", WriteBodyFromJSON(content)) } x := reflect.ValueOf(content).Elem() t := x.Type() @@ -183,7 +159,14 @@ func (bot *Bot) PostMessageToChannel(id string, content *MessagePost) (*Message, if err != nil { return nil, errors.Wrap(err, getThisFuncName()) } - return bot.postOpenAPIofMessage("/channels/"+id+"/messages", contenttype, body) + return bot.postOpenAPIofMessage(ep, contenttype, body) +} + +// PostMessageToChannel 向 channel_id 指定的子频道发送消息 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/message/post_messages.html +func (bot *Bot) PostMessageToChannel(id string, content *MessagePost) (*Message, error) { + return bot.postMessageTo("/channels/"+id+"/messages", content) } // DeleteMessageInChannel 回子频道 channel_id 下的消息 message_id