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

feat: implement imoto

This commit is contained in:
源文雨
2023-11-17 13:46:21 +09:00
parent c9ba2f7350
commit 44f83cae19
3 changed files with 46 additions and 4 deletions

View File

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

1
go.mod
View File

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

2
go.sum
View File

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