Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test4 #28

Merged
merged 42 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
fad0d50
Compiled main.go and pushed changes
Hoshinonyaruko Oct 23, 2023
fda2572
test
Hoshinonyaruko Oct 23, 2023
34cfac1
适配了频道私聊,用bolt数据库取代ini
Hoshinonyaruko Oct 23, 2023
7fc1f10
适配了nonebot2
Hoshinonyaruko Oct 24, 2023
28f9a73
Merge branch 'main' of https://github.com/Hoshinonyaruko/Gensokyo
Hoshinonyaruko Oct 24, 2023
9fadda4
适配私域
Hoshinonyaruko Oct 24, 2023
1336c6a
add license
Hoshinonyaruko Oct 24, 2023
b094949
add a lot
Hoshinonyaruko Oct 25, 2023
f4787f5
Resolved merge conflicts
Hoshinonyaruko Oct 25, 2023
86698c6
trss support
Hoshinonyaruko Oct 26, 2023
045f3e9
fix
Hoshinonyaruko Oct 26, 2023
618561c
add action
Hoshinonyaruko Oct 26, 2023
7e803aa
add action
Hoshinonyaruko Oct 26, 2023
ece4cef
add action
Hoshinonyaruko Oct 26, 2023
f864a50
fixbug
Hoshinonyaruko Oct 26, 2023
4564731
add wss
Hoshinonyaruko Oct 26, 2023
070e4df
merge
Hoshinonyaruko Oct 26, 2023
84e8fe9
bugfix
Hoshinonyaruko Oct 26, 2023
0e4fd50
fix action
Hoshinonyaruko Oct 26, 2023
fe2234a
fix action again
Hoshinonyaruko Oct 26, 2023
cf67a11
fix action againnn
Hoshinonyaruko Oct 26, 2023
0875837
fa
Hoshinonyaruko Oct 26, 2023
3a3c33f
fix
Hoshinonyaruko Oct 26, 2023
8789fa8
add a lot
Hoshinonyaruko Oct 27, 2023
7943466
add ws server token
Hoshinonyaruko Oct 28, 2023
4660983
merge
Hoshinonyaruko Oct 28, 2023
2a1026a
bugifx
Hoshinonyaruko Oct 28, 2023
4186970
fix
Hoshinonyaruko Oct 28, 2023
2f26828
fixat
Hoshinonyaruko Oct 28, 2023
007e1af
bugfix
Hoshinonyaruko Oct 28, 2023
c41fd77
bugfix
Hoshinonyaruko Oct 28, 2023
d8354b7
test
Hoshinonyaruko Oct 28, 2023
6e3c63c
test
Hoshinonyaruko Oct 28, 2023
f08d9d7
test2
Hoshinonyaruko Oct 28, 2023
4dd6e15
add url service
Hoshinonyaruko Oct 31, 2023
1f8c174
add url service
Hoshinonyaruko Oct 31, 2023
240f569
add url service
Hoshinonyaruko Oct 31, 2023
b191cb7
bugfix
Hoshinonyaruko Oct 31, 2023
42daab3
merge
Hoshinonyaruko Oct 31, 2023
8fec03f
fix
Hoshinonyaruko Oct 31, 2023
cca528b
fix
Hoshinonyaruko Oct 31, 2023
463a182
fix
Hoshinonyaruko Oct 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Processor/ProcessChannelDirectMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,15 @@ func (p *Processors) ProcessChannelDirectMessage(data *dto.WSDirectMessageData)
//将真实id写入数据库,可取出ChannelID
idmap.WriteConfigv2(data.Author.ID, "channel_id", data.ChannelID)
//将channelid写入数据库,可取出guild_id
ChannelID64, err := idmap.StoreIDv2(data.ChannelID)
if err != nil {
log.Printf("Error storing ID: %v", err)
return nil
}
//转成int再互转
idmap.WriteConfigv2(fmt.Sprint(ChannelID64), "guild_id", data.GuildID)
//直接储存 适用于私信场景私聊
idmap.WriteConfigv2(data.ChannelID, "guild_id", data.GuildID)

//收到私聊信息调用的具体还原步骤
//1,idmap还原真实userid,
//2,通过idmap获取channelid,
Expand Down Expand Up @@ -168,6 +175,7 @@ func (p *Processors) ProcessChannelDirectMessage(data *dto.WSDirectMessageData)
echo.AddMsgType(AppIDString, userid64, "guild_private")
//储存当前群或频道号的类型
idmap.WriteConfigv2(data.ChannelID, "type", "guild_private")
//todo 完善频道类型信息转换

//调试
PrintStructWithFieldNames(onebotMsg)
Expand All @@ -184,7 +192,7 @@ func (p *Processors) ProcessChannelDirectMessage(data *dto.WSDirectMessageData)
log.Printf("Error storing ID: %v", err)
return nil
}
//转成int再互转
//转成int再互转 适用于群场景私聊
idmap.WriteConfigv2(fmt.Sprint(ChannelID64), "guild_id", data.GuildID)
//转换at
messageText := handlers.RevertTransformedText(data)
Expand Down Expand Up @@ -254,7 +262,8 @@ func (p *Processors) ProcessChannelDirectMessage(data *dto.WSDirectMessageData)
echo.AddMsgID(AppIDString, userid64, data.ID)
echo.AddMsgType(AppIDString, userid64, "guild_private")
//储存当前群或频道号的类型
idmap.WriteConfigv2(data.ChannelID, "type", "guild_private")
idmap.WriteConfigv2(fmt.Sprint(ChannelID64), "type", "guild_private")
echo.AddMsgType(AppIDString, ChannelID64, "guild_private")

//调试
PrintStructWithFieldNames(groupMsg)
Expand Down
3 changes: 2 additions & 1 deletion Processor/ProcessGroupMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ func (p *Processors) ProcessGroupMessage(data *dto.WSGroupATMessageData) error {
echo.AddMsgID(AppIDString, GroupID64, data.ID)
echo.AddMsgType(AppIDString, GroupID64, "group")
//储存当前群或频道号的类型
idmap.WriteConfigv2(data.GroupID, "type", "group")
idmap.WriteConfigv2(fmt.Sprint(GroupID64), "type", "group")
echo.AddMsgType(AppIDString, GroupID64, "group")

// 调试
PrintStructWithFieldNames(groupMsg)
Expand Down
4 changes: 3 additions & 1 deletion Processor/ProcessGuildATMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (p *Processors) ProcessGuildATMessage(data *dto.WSATMessageData) error {
echo.AddMsgType(AppIDString, userid64, "guild")
//储存当前群或频道号的类型
idmap.WriteConfigv2(data.ChannelID, "type", "guild")
//todo 完善频道转换

//调试
PrintStructWithFieldNames(onebotMsg)
Expand Down Expand Up @@ -181,7 +182,8 @@ func (p *Processors) ProcessGuildATMessage(data *dto.WSATMessageData) error {
echo.AddMsgID(AppIDString, ChannelID64, data.ID)
echo.AddMsgType(AppIDString, ChannelID64, "guild")
//储存当前群或频道号的类型
idmap.WriteConfigv2(data.ChannelID, "type", "guild")
idmap.WriteConfigv2(fmt.Sprint(ChannelID64), "type", "guild")
echo.AddMsgType(AppIDString, ChannelID64, "guild")

//调试
PrintStructWithFieldNames(groupMsg)
Expand Down
4 changes: 3 additions & 1 deletion Processor/ProcessGuildNormalMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (p *Processors) ProcessGuildNormalMessage(data *dto.WSMessageData) error {
echo.AddMsgType(AppIDString, userid64, "guild")
//储存当前群或频道号的类型
idmap.WriteConfigv2(data.ChannelID, "type", "guild")
//todo 完善频道ob信息

//调试
PrintStructWithFieldNames(onebotMsg)
Expand Down Expand Up @@ -180,7 +181,8 @@ func (p *Processors) ProcessGuildNormalMessage(data *dto.WSMessageData) error {
echo.AddMsgID(AppIDString, ChannelID64, data.ID)
echo.AddMsgType(AppIDString, ChannelID64, "guild")
//储存当前群或频道号的类型
idmap.WriteConfigv2(data.ChannelID, "type", "guild")
idmap.WriteConfigv2(fmt.Sprint(ChannelID64), "type", "guild")
echo.AddMsgType(AppIDString, ChannelID64, "guild")

//调试
PrintStructWithFieldNames(groupMsg)
Expand Down
Binary file modified gensokyo.db
Binary file not shown.
23 changes: 20 additions & 3 deletions handlers/send_group_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ func handleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openap
case "guild":
//用GroupID给ChannelID赋值,因为我们是把频道虚拟成了群
message.Params.ChannelID = message.Params.GroupID.(string)
// 使用RetrieveRowByIDv2还原真实的ChannelID
RChannelID, err := idmap.RetrieveRowByIDv2(message.Params.ChannelID)
if err != nil {
log.Printf("error retrieving real UserID: %v", err)
}
message.Params.ChannelID = RChannelID
//这一句是group_private的逻辑,发频道信息用的是channelid
//message.Params.GroupID = value
handleSendGuildChannelMsg(client, api, apiv2, message)
Expand Down Expand Up @@ -139,14 +145,20 @@ func handleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openap
}
}

// 不支持base64
func generateGroupMessage(id string, foundItems map[string][]string, messageText string) interface{} {
if imageURLs, ok := foundItems["local_image"]; ok && len(imageURLs) > 0 {
// 从本地路径读取图片
imageData, err := os.ReadFile(imageURLs[0])
if err != nil {
// 读入文件,如果是本地图,应用端和gensokyo需要在一台电脑
// 读入文件失败
log.Printf("Error reading the image from path %s: %v", imageURLs[0], err)
return nil
// 返回文本信息,提示图片文件不存在
return &dto.MessageToCreate{
Content: "错误: 图片文件不存在",
MsgID: id,
MsgType: 0, // 默认文本类型
}
}

// base64编码
Expand All @@ -156,7 +168,12 @@ func generateGroupMessage(id string, foundItems map[string][]string, messageText
imageURL, err := images.UploadBase64ImageToServer(base64Encoded)
if err != nil {
log.Printf("Error uploading base64 encoded image: %v", err)
return nil
// 如果上传失败,也返回文本信息,提示上传失败
return &dto.MessageToCreate{
Content: "错误: 上传图片失败",
MsgID: id,
MsgType: 0, // 默认文本类型
}
}

// 创建RichMediaMessage并返回,当作URL图片处理
Expand Down
21 changes: 17 additions & 4 deletions handlers/send_guild_channel_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/hoshinonyaruko/gensokyo/callapi"
"github.com/hoshinonyaruko/gensokyo/config"
"github.com/hoshinonyaruko/gensokyo/idmap"

"github.com/hoshinonyaruko/gensokyo/echo"

Expand Down Expand Up @@ -50,6 +51,7 @@ func handleSendGuildChannelMsg(client callapi.Client, api openapi.OpenAPI, apiv2
messageText, foundItems := parseMessageContent(params)

channelID := params.ChannelID
log.Printf("发送文本信息失败: %v,%v", channelID, channelID)
// 使用 echo 获取消息ID
var messageID string
if echoStr, ok := message.Echo.(string); ok {
Expand All @@ -63,8 +65,8 @@ func handleSendGuildChannelMsg(client callapi.Client, api openapi.OpenAPI, apiv2
}
log.Println("频道发信息messageText:", messageText)
log.Println("foundItems:", foundItems)
var err error
// 优先发送文本信息
var err error
if messageText != "" {
textMsg, _ := generateReplyMessage(messageID, nil, messageText)
if _, err = api.PostMessage(context.TODO(), channelID, textMsg); err != nil {
Expand Down Expand Up @@ -112,13 +114,18 @@ func handleSendGuildChannelMsg(client callapi.Client, api openapi.OpenAPI, apiv2
params := message.Params
channelID := params.ChannelID
guildID := params.GuildID
handleSendGuildChannelPrivateMsg(client, api, apiv2, message, &guildID, &channelID)
// 使用RetrieveRowByIDv2还原真实的ChannelID
RChannelID, err := idmap.RetrieveRowByIDv2(channelID)
if err != nil {
log.Printf("error retrieving real UserID: %v", err)
}
handleSendGuildChannelPrivateMsg(client, api, apiv2, message, &guildID, &RChannelID)
default:
log.Printf("2Unknown message type: %s", msgType)
}
}

// 组合发频道信息需要的MessageToCreate
// 组合发频道信息需要的MessageToCreate 支持base64
func generateReplyMessage(id string, foundItems map[string][]string, messageText string) (*dto.MessageToCreate, bool) {
var reply dto.MessageToCreate
var isBase64 bool
Expand All @@ -129,7 +136,13 @@ func generateReplyMessage(id string, foundItems map[string][]string, messageText
if err != nil {
// 读入文件,如果是本地图,应用端和gensokyo需要在一台电脑
log.Printf("Error reading the image from path %s: %v", imageURLs[0], err)
return nil, false
// 发文本信息,提示图片文件不存在
reply = dto.MessageToCreate{
Content: "错误: 图片文件不存在",
MsgID: id,
MsgType: 0, // 默认文本类型
}
return &reply, false
}

//base64编码
Expand Down
101 changes: 11 additions & 90 deletions handlers/send_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ package handlers

import (
"context"
"encoding/base64"
"fmt"
"log"
"os"
"strconv"
"time"

"github.com/hoshinonyaruko/gensokyo/callapi"
"github.com/hoshinonyaruko/gensokyo/config"
"github.com/hoshinonyaruko/gensokyo/echo"
"github.com/hoshinonyaruko/gensokyo/idmap"
"github.com/hoshinonyaruko/gensokyo/images"
"github.com/tencent-connect/botgo/dto"
"github.com/tencent-connect/botgo/openapi"
)
Expand Down Expand Up @@ -74,7 +71,7 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope

// 优先发送文本信息
if messageText != "" {
groupReply := generateMessage(messageID, nil, messageText)
groupReply := generateGroupMessage(messageID, nil, messageText)

// 进行类型断言
groupMessage, ok := groupReply.(*dto.MessageToCreate)
Expand All @@ -97,7 +94,7 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope
var singleItem = make(map[string][]string)
singleItem[key] = urls

groupReply := generateMessage(messageID, singleItem, "")
groupReply := generateGroupMessage(messageID, singleItem, "")

// 进行类型断言
richMediaMessage, ok := groupReply.(*dto.RichMediaMessage)
Expand All @@ -117,6 +114,12 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope
case "guild":
//用GroupID给ChannelID赋值,因为我们是把频道虚拟成了群
message.Params.ChannelID = message.Params.GroupID.(string)
// 使用RetrieveRowByIDv2还原真实的ChannelID
RChannelID, err := idmap.RetrieveRowByIDv2(message.Params.ChannelID)
if err != nil {
log.Printf("error retrieving real UserID: %v", err)
}
message.Params.ChannelID = RChannelID
handleSendGuildChannelMsg(client, api, apiv2, message)
case "guild_private":
//send_msg比具体的send_xxx少一层,其包含的字段类型在虚拟化场景已经失去作用
Expand All @@ -130,14 +133,8 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope
// 先尝试将GroupID断言为字符串
if channelID, ok := message.Params.GroupID.(string); ok && channelID != "" {
channelIDPtr = &channelID
// 使用RetrieveRowByIDv2还原真实的UserID
RChannelID, err := idmap.RetrieveRowByIDv2(*channelIDPtr)
if err != nil {
log.Printf("error retrieving real ChannelID: %v", err)
return
}
// 读取bolt数据库 通过ChannelID取回之前储存的guild_id
if value, err := idmap.ReadConfigv2(RChannelID, "guild_id"); err == nil && value != "" {
if value, err := idmap.ReadConfigv2(*channelIDPtr, "guild_id"); err == nil && value != "" {
GuildidPtr = &value
} else {
log.Printf("Error reading config: %v", err)
Expand Down Expand Up @@ -178,7 +175,7 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope

// 优先发送文本信息
if messageText != "" {
groupReply := generateMessage(messageID, nil, messageText)
groupReply := generateGroupMessage(messageID, nil, messageText)

// 进行类型断言
groupMessage, ok := groupReply.(*dto.MessageToCreate)
Expand All @@ -201,7 +198,7 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope
var singleItem = make(map[string][]string)
singleItem[key] = urls

groupReply := generateMessage(messageID, singleItem, "")
groupReply := generateGroupMessage(messageID, singleItem, "")

// 进行类型断言
richMediaMessage, ok := groupReply.(*dto.RichMediaMessage)
Expand All @@ -221,82 +218,6 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope
}
}

func generateMessage(id string, foundItems map[string][]string, messageText string) interface{} {
if imageURLs, ok := foundItems["local_image"]; ok && len(imageURLs) > 0 {
// 从本地路径读取图片
imageData, err := os.ReadFile(imageURLs[0])
if err != nil {
// 读入文件,如果是本地图,应用端和gensokyo需要在一台电脑
log.Printf("Error reading the image from path %s: %v", imageURLs[0], err)
return nil
}

// base64编码
base64Encoded := base64.StdEncoding.EncodeToString(imageData)

// 上传base64编码的图片并获取其URL
imageURL, err := images.UploadBase64ImageToServer(base64Encoded)
if err != nil {
log.Printf("Error uploading base64 encoded image: %v", err)
return nil
}

// 创建RichMediaMessage并返回,当作URL图片处理
return &dto.RichMediaMessage{
EventID: id,
FileType: 1, // 1代表图片
URL: imageURL,
Content: "", // 这个字段文档没有了
SrvSendMsg: true,
}
} else if imageURLs, ok := foundItems["url_image"]; ok && len(imageURLs) > 0 {
// 发链接图片
return &dto.RichMediaMessage{
EventID: id,
FileType: 1, // 1代表图片
URL: "http://" + imageURLs[0],
Content: "", // 这个字段文档没有了
SrvSendMsg: true,
}
} else if base64_image, ok := foundItems["base64_image"]; ok && len(base64_image) > 0 {
// todo 适配base64图片
//因为QQ群没有 form方式上传,所以在gensokyo内置了图床,需公网,或以lotus方式连接位于公网的gensokyo
//要正确的开放对应的端口和设置正确的ip地址在config,这对于一般用户是有一些难度的
if base64Image, ok := foundItems["base64_image"]; ok && len(base64Image) > 0 {
// 解码base64图片数据
fileImageData, err := base64.StdEncoding.DecodeString(base64Image[0])
if err != nil {
log.Printf("failed to decode base64 image: %v", err)
return nil
}
// 将解码的图片数据转换回base64格式并上传
imageURL, err := images.UploadBase64ImageToServer(base64.StdEncoding.EncodeToString(fileImageData))
if err != nil {
log.Printf("failed to upload base64 image: %v", err)
return nil
}
// 创建RichMediaMessage并返回
return &dto.RichMediaMessage{
EventID: id,
FileType: 1, // 1代表图片
URL: imageURL,
Content: "", // 这个字段文档没有了
SrvSendMsg: true,
}
}
} else if voiceURLs, ok := foundItems["base64_record"]; ok && len(voiceURLs) > 0 {
// 目前不支持发语音 todo 适配base64 slik
} else {
// 返回文本信息
return &dto.MessageToCreate{
Content: messageText,
MsgID: id,
MsgType: 0, // 默认文本类型
}
}
return nil
}

// 通过user_id获取messageID
func GetMessageIDByUseridOrGroupid(appID string, userID interface{}) string {
// 从appID和userID生成key
Expand Down
Loading
Loading