1
0
mirror of https://github.com/fumiama/NanoBot.git synced 2026-06-10 13:10:26 +08:00

feat: new message media api

This commit is contained in:
源文雨
2023-11-22 17:45:06 +09:00
parent 3c2b37d5d7
commit b79077c9d7
4 changed files with 45 additions and 27 deletions

View File

@@ -134,6 +134,14 @@ func (ctx *Ctx) Send(messages Messages) (m []*Message, err error) {
} else if OnlyQQPrivate(ctx) { } else if OnlyQQPrivate(ctx) {
reply, err = ctx.PostFileToQQUser(ctx.Message.Author.ID, fp) reply, err = ctx.PostFileToQQUser(ctx.Message.Author.ID, fp)
} }
if err != nil {
return
}
reply, err = ctx.Post(isnextreply, &MessagePost{
Type: MessageTypeMedia,
Content: " ",
Media: &MessageMedia{FileInfo: reply.FileInfo},
})
m = append(m, reply) m = append(m, reply)
if err != nil { if err != nil {
return return
@@ -203,6 +211,10 @@ func (ctx *Ctx) SendPlainMessage(replytosender bool, printable ...any) (*Message
// SendImage 发送带图片消息到对方 // SendImage 发送带图片消息到对方
func (ctx *Ctx) SendImage(file string, replytosender bool, caption ...any) (reply *Message, err error) { func (ctx *Ctx) SendImage(file string, replytosender bool, caption ...any) (reply *Message, err error) {
post := &MessagePost{
Content: HideURL(fmt.Sprint(caption...)),
}
if OnlyQQ(ctx) { if OnlyQQ(ctx) {
if strings.HasPrefix(file, "file:///") { if strings.HasPrefix(file, "file:///") {
data, err := os.ReadFile(file[8:]) data, err := os.ReadFile(file[8:])
@@ -229,25 +241,27 @@ func (ctx *Ctx) SendImage(file string, replytosender bool, caption ...any) (repl
Type: FileTypeImage, Type: FileTypeImage,
URL: file, URL: file,
} }
if len(caption) > 0 { /*if len(caption) > 0 {
_, _ = ctx.SendPlainMessage(replytosender, caption...) _, _ = ctx.SendPlainMessage(replytosender, caption...)
}*/
if post.Content == "" {
post.Content = " "
} }
if OnlyQQGroup(ctx) { if OnlyQQGroup(ctx) {
reply, err = ctx.PostFileToQQGroup(ctx.Message.ChannelID, fp) reply, err = ctx.PostFileToQQGroup(ctx.Message.ChannelID, fp)
} else if OnlyQQPrivate(ctx) { } else if OnlyQQPrivate(ctx) {
reply, err = ctx.PostFileToQQUser(ctx.Message.Author.ID, fp) reply, err = ctx.PostFileToQQUser(ctx.Message.Author.ID, fp)
} }
return if err != nil {
} return
}
post := &MessagePost{ post.Media = &MessageMedia{FileInfo: reply.FileInfo}
Content: HideURL(fmt.Sprint(caption...)),
}
if strings.HasPrefix(file, "http") {
post.Image = file
} else { } else {
post.ImageFile = file if strings.HasPrefix(file, "http") {
post.Image = file
} else {
post.ImageFile = file
}
} }
return ctx.Post(replytosender, post) return ctx.Post(replytosender, post)
@@ -260,17 +274,7 @@ func (ctx *Ctx) SendImageBytes(data []byte, replytosender bool, caption ...any)
if err != nil { if err != nil {
return nil, err return nil, err
} }
fp := &FilePost{ return ctx.SendImage(file, replytosender, caption...)
Type: FileTypeImage,
URL: file,
}
if len(caption) > 0 {
_, _ = ctx.SendPlainMessage(replytosender, caption...)
}
if OnlyQQGroup(ctx) {
return ctx.PostFileToQQGroup(ctx.Message.ChannelID, fp)
}
return ctx.PostFileToQQUser(ctx.Message.Author.ID, fp)
} }
post := &MessagePost{ post := &MessagePost{

View File

@@ -23,9 +23,12 @@ type Message struct {
ChannelID string `json:"channel_id"` ChannelID string `json:"channel_id"`
GuildID string `json:"guild_id"` GuildID string `json:"guild_id"`
GroupOpenID string `json:"group_openid"` GroupOpenID string `json:"group_openid"`
FileUUID string `json:"file_uuid"`
FileInfo string `json:"file_info"`
Content string `json:"content"` Content string `json:"content"`
Timestamp time.Time `json:"timestamp"` Timestamp time.Time `json:"timestamp"`
EditedTimestamp time.Time `json:"edited_timestamp"` EditedTimestamp time.Time `json:"edited_timestamp"`
FileInfoTTL int `json:"ttl"`
MentionEveryone bool `json:"mention_everyone"` MentionEveryone bool `json:"mention_everyone"`
Author *User `json:"author"` Author *User `json:"author"`
Attachments []MessageAttachment `json:"attachments"` Attachments []MessageAttachment `json:"attachments"`
@@ -239,6 +242,7 @@ type MessagePost struct {
ReplyEventID string `json:"event_id,omitempty"` ReplyEventID string `json:"event_id,omitempty"`
Markdown *MessageMarkdown `json:"markdown,omitempty"` Markdown *MessageMarkdown `json:"markdown,omitempty"`
KeyBoard *MessageKeyboard `json:"keyboard,omitempty"` KeyBoard *MessageKeyboard `json:"keyboard,omitempty"`
Media *MessageMedia `json:"media,omitempty"`
} }
func (mp *MessagePost) String() string { func (mp *MessagePost) String() string {
@@ -303,6 +307,10 @@ func (mp *MessagePost) String() string {
sb.WriteString(", KB模版: ") sb.WriteString(", KB模版: ")
sb.WriteString(mp.KeyBoard.ID) sb.WriteString(mp.KeyBoard.ID)
} }
if mp.Media != nil {
sb.WriteString(", 富媒体: ")
sb.WriteString(mp.Media.FileInfo)
}
return sb.String() return sb.String()
} }

View File

@@ -36,9 +36,9 @@ func (ft FileType) String() string {
// //
// https://bot.q.qq.com/wiki/develop/api-231017/server-inter/message/send-receive/rich-text-media.html // https://bot.q.qq.com/wiki/develop/api-231017/server-inter/message/send-receive/rich-text-media.html
type FilePost struct { type FilePost struct {
Type FileType `json:"file_type"` Type FileType `json:"file_type"`
URL string `json:"url"` URL string `json:"url"`
MotherFuckingAlwaysTrue bool `json:"srv_send_msg"` IsPositive bool `json:"srv_send_msg"` // IsPositive
// file_data 否 【暂未支持】 // file_data 否 【暂未支持】
} }
@@ -61,7 +61,6 @@ func (fp *FilePost) String() string {
// https://bot.q.qq.com/wiki/develop/api-231017/server-inter/message/send-receive/rich-text-media.html#%E5%8F%91%E9%80%81%E5%88%B0%E5%8D%95%E8%81%8A // https://bot.q.qq.com/wiki/develop/api-231017/server-inter/message/send-receive/rich-text-media.html#%E5%8F%91%E9%80%81%E5%88%B0%E5%8D%95%E8%81%8A
func (bot *Bot) PostFileToQQUser(id string, content *FilePost) (*Message, error) { func (bot *Bot) PostFileToQQUser(id string, content *FilePost) (*Message, error) {
logrus.Infoln(getLogHeader(), "<= [Q]单:", id+",", content) logrus.Infoln(getLogHeader(), "<= [Q]单:", id+",", content)
content.MotherFuckingAlwaysTrue = true
return bot.postOpenAPIofMessage("/v2/users/"+id+"/files", "", WriteBodyFromJSON(content)) return bot.postOpenAPIofMessage("/v2/users/"+id+"/files", "", WriteBodyFromJSON(content))
} }
@@ -70,6 +69,10 @@ func (bot *Bot) PostFileToQQUser(id string, content *FilePost) (*Message, error)
// https://bot.q.qq.com/wiki/develop/api-231017/server-inter/message/send-receive/rich-text-media.html#%E5%8F%91%E9%80%81%E5%88%B0%E7%BE%A4%E8%81%8A // https://bot.q.qq.com/wiki/develop/api-231017/server-inter/message/send-receive/rich-text-media.html#%E5%8F%91%E9%80%81%E5%88%B0%E7%BE%A4%E8%81%8A
func (bot *Bot) PostFileToQQGroup(id string, content *FilePost) (*Message, error) { func (bot *Bot) PostFileToQQGroup(id string, content *FilePost) (*Message, error) {
logrus.Infoln(getLogHeader(), "<= [Q]群:", id+",", content) logrus.Infoln(getLogHeader(), "<= [Q]群:", id+",", content)
content.MotherFuckingAlwaysTrue = true
return bot.postOpenAPIofMessage("/v2/groups/"+id+"/files", "", WriteBodyFromJSON(content)) return bot.postOpenAPIofMessage("/v2/groups/"+id+"/files", "", WriteBodyFromJSON(content))
} }
// MessageMedia used in MessagePost
type MessageMedia struct {
FileInfo string `json:"file_info"`
}

View File

@@ -14,6 +14,9 @@ const (
MessageTypeMarkdown MessageTypeMarkdown
MessageTypeArk MessageTypeArk
MessageTypeEmbed MessageTypeEmbed
MessageTypeUnk1
MessageTypeUnk2
MessageTypeMedia
) )
func (mt2 MessageType) String() string { func (mt2 MessageType) String() string {