mirror of
https://github.com/fumiama/NanoBot.git
synced 2026-06-10 05:00:24 +08:00
finish dms
This commit is contained in:
20
helper.go
20
helper.go
@@ -27,3 +27,23 @@ func getThisFuncName() string {
|
|||||||
func getCallerFuncName() string {
|
func getCallerFuncName() string {
|
||||||
return getFuncNameWithSkip(2)
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ func (bot *Bot) DeleteOpenAPI(ep, contenttype string, body io.Reader) error {
|
|||||||
return nil
|
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
|
// PostOpenAPI 从 ep 得到 json 结构化数据返回值写到 ptr, ptr 除 Slice 外必须在开头继承 CodeMessageBase
|
||||||
func (bot *Bot) PostOpenAPI(ep, contenttype string, ptr any, body io.Reader) error {
|
func (bot *Bot) PostOpenAPI(ep, contenttype string, ptr any, body io.Reader) error {
|
||||||
|
|||||||
@@ -43,3 +43,15 @@ func (bot *Bot) postOpenAPIofMessage(ep, contenttype string, body io.Reader) (*M
|
|||||||
}
|
}
|
||||||
return &resp.Message, err
|
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
38
openapi_dms.go
Normal 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)
|
||||||
|
}
|
||||||
@@ -3,7 +3,6 @@ package nano
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@@ -126,32 +125,9 @@ type MessagePost struct {
|
|||||||
KeyBoard *MessageKeyboard `json:"keyboard,omitempty"`
|
KeyBoard *MessageKeyboard `json:"keyboard,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// MessageEscape 消息转义
|
func (bot *Bot) postMessageTo(ep string, content *MessagePost) (*Message, error) {
|
||||||
//
|
|
||||||
// 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) {
|
|
||||||
if content.ImageFile == "" {
|
if content.ImageFile == "" {
|
||||||
return bot.postOpenAPIofMessage("/channels/"+id+"/messages", "", WriteBodyFromJSON(content))
|
return bot.postOpenAPIofMessage(ep, "", WriteBodyFromJSON(content))
|
||||||
}
|
}
|
||||||
x := reflect.ValueOf(content).Elem()
|
x := reflect.ValueOf(content).Elem()
|
||||||
t := x.Type()
|
t := x.Type()
|
||||||
@@ -183,7 +159,14 @@ func (bot *Bot) PostMessageToChannel(id string, content *MessagePost) (*Message,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, getThisFuncName())
|
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
|
// DeleteMessageInChannel 回子频道 channel_id 下的消息 message_id
|
||||||
|
|||||||
Reference in New Issue
Block a user