From 44f83cae19270ddf21b65089b926122e8959d0b8 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: Fri, 17 Nov 2023 13:46:21 +0900 Subject: [PATCH] feat: implement imoto --- context.go | 47 +++++++++++++++++++++++++++++++++++++++++++---- go.mod | 1 + go.sum | 2 ++ 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/context.go b/context.go index e5f211c..d6147c9 100644 --- a/context.go +++ b/context.go @@ -1,12 +1,17 @@ package nano import ( + "encoding/base64" "errors" "fmt" + "os" "reflect" "strconv" "strings" "sync" + + base14 "github.com/fumiama/go-base16384" + "github.com/fumiama/imoto" ) //go:generate go run codegen/context/main.go @@ -79,6 +84,8 @@ func (ctx *Ctx) CheckSession() Rule { } } +var imotoken = "f7f06a63b8c111df0d4faa256cd5ba35cb98678ee8274923576d0416b52fe768" + // Send 发送一批消息 func (ctx *Ctx) Send(messages Messages) (m []*Message, err error) { isnextreply := false @@ -99,9 +106,6 @@ func (ctx *Ctx) Send(messages Messages) (m []*Message, err error) { return } case MessageSegmentTypeImageBytes: - if ctx.IsQQ { - continue - } reply, err = ctx.SendImageBytes(StringToBytes(msg.Data), isnextreply, textlist...) if isnextreply { isnextreply = false @@ -200,6 +204,27 @@ func (ctx *Ctx) SendPlainMessage(replytosender bool, printable ...any) (*Message // SendImage 发送带图片消息到对方 func (ctx *Ctx) SendImage(file string, replytosender bool, caption ...any) (reply *Message, err error) { if OnlyQQ(ctx) { + if strings.HasPrefix(file, "file:///") { + data, err := os.ReadFile(file[8:]) + if err != nil { + return nil, err + } + return ctx.SendImageBytes(data, replytosender, caption...) + } + if strings.HasPrefix(file, "base64://") { + data, err := base64.StdEncoding.DecodeString(file[9:]) + if err != nil { + return nil, err + } + return ctx.SendImageBytes(data, replytosender, caption...) + } + if strings.HasPrefix(file, "base16384://") { + data := base14.DecodeFromString(file[12:]) + if len(data) == 0 { + return nil, errors.New("invalid base16384 image") + } + return ctx.SendImageBytes(data, replytosender, caption...) + } fp := &FilePost{ Type: FileTypeImage, URL: file, @@ -231,7 +256,21 @@ func (ctx *Ctx) SendImage(file string, replytosender bool, caption ...any) (repl // SendImageBytes 发送带图片消息到对方 func (ctx *Ctx) SendImageBytes(data []byte, replytosender bool, caption ...any) (*Message, error) { if OnlyQQ(ctx) { - return nil, errors.New("QQ暂不支持直接发送图片数据") + file, _, _, err := imoto.Bed(imotoken, data) + if err != nil { + return nil, err + } + fp := &FilePost{ + 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{ diff --git a/go.mod b/go.mod index b5e7a98..f16491a 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/RomiChan/syncx v0.0.0-20221202055724-5f842c53020e github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5 github.com/fumiama/go-base16384 v1.7.0 + github.com/fumiama/imoto v0.1.1 github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.8.1 diff --git a/go.sum b/go.sum index b0be882..41d59a1 100644 --- a/go.sum +++ b/go.sum @@ -25,6 +25,8 @@ github.com/fumiama/go-simple-protobuf v0.1.0 h1:rLzJgNqB6LHNDVMl81yyNt6ZKziWtVfu github.com/fumiama/go-simple-protobuf v0.1.0/go.mod h1:5yYNapXq1tQMOZg9bOIVhQlZk9pQqpuFIO4DZLbsdy4= github.com/fumiama/gofastTEA v0.0.10 h1:JJJ+brWD4kie+mmK2TkspDXKzqq0IjXm89aGYfoGhhQ= github.com/fumiama/gofastTEA v0.0.10/go.mod h1:RIdbYZyB4MbH6ZBlPymRaXn3cD6SedlCu5W/HHfMPBk= +github.com/fumiama/imoto v0.1.1 h1:UtWslFtRSNKGNB7O9ThhY66Rz4foJ5gfdEgvIH0W01A= +github.com/fumiama/imoto v0.1.1/go.mod h1:1MLP+qfEbhZOn+ETXd6k0xkjMyNGuqnaiblcQuWu9NQ= github.com/fumiama/sqlite3 v1.20.0-with-win386 h1:ZR1AXGBEtkfq9GAXehOVcwn+aaCG8itrkgEsz4ggx5k= github.com/fumiama/sqlite3 v1.20.0-with-win386/go.mod h1:Os58MHwYCcYZCy2PGChBrQtBAw5/LS1ZZOkfc+C/I7s= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=