1
0
mirror of https://github.com/fumiama/NanoBot.git synced 2026-06-05 02:30:23 +08:00

finish dms

This commit is contained in:
源文雨
2023-10-12 00:12:04 +09:00
parent c85d8701d5
commit 4c93bb6ff8
5 changed files with 81 additions and 28 deletions

View File

@@ -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, "<", "&lt;")
text = strings.ReplaceAll(text, ">", "&gt;")
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, "&amp;", "&")
text = strings.ReplaceAll(text, "&lt;", "<")
text = strings.ReplaceAll(text, "&gt;", ">")
return text
}

View File

@@ -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 {

View File

@@ -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
}

38
openapi_dms.go Normal file
View File

@@ -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)
}

View File

@@ -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, "&", "&amp;")
text = strings.ReplaceAll(text, "<", "&lt;")
text = strings.ReplaceAll(text, ">", "&gt;")
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, "&amp;", "&")
text = strings.ReplaceAll(text, "&lt;", "<")
text = strings.ReplaceAll(text, "&gt;", ">")
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