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

wtf #117

Merged
merged 4 commits into from
Nov 17, 2023
Merged

wtf #117

Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 6 additions & 2 deletions .github/workflows/cross_compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,17 @@ jobs:
GOARCH: ${{ matrix.goarch }}
CGO_ENABLED: 0
run: |
go build -o output/gensokyo-${{ matrix.os }}-${{ matrix.goarch }}
if [ "$GOOS" = "windows" ]; then
go build -o output/gensokyo-${{ matrix.os }}-${{ matrix.goarch }}.exe
else
go build -o output/gensokyo-${{ matrix.os }}-${{ matrix.goarch }}
fi

- name: Upload artifacts
uses: actions/upload-artifact@v2
with:
name: gensokyo-${{ matrix.os }}-${{ matrix.goarch }}
path: output/gensokyo-${{ matrix.os }}-${{ matrix.goarch }}
path: output/gensokyo-${{ matrix.os }}-${{ matrix.goarch }}${{ if eq(matrix.os, 'windows') }}.exe${{ endif }}

prepare_release:
needs: build
Expand Down
31 changes: 27 additions & 4 deletions handlers/message_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func parseMessageContent(paramsMessage callapi.ParamsContent) (string, map[strin
default:
mylog.Println("Unsupported message format: params.message field is not a string, map or slice")
}
//mylog.Printf(messageText)
mylog.Printf(messageText)
// 正则表达式部分
var localImagePattern *regexp.Regexp

Expand Down Expand Up @@ -160,7 +160,9 @@ func parseMessageContent(paramsMessage callapi.ParamsContent) (string, map[strin

//处理at
messageText = transformMessageText(messageText)

for key, items := range foundItems {
fmt.Printf("Key: %s, Items: %v\n", key, items)
}
return messageText, foundItems
}

Expand Down Expand Up @@ -399,8 +401,8 @@ func ConvertToSegmentedMessage(data interface{}) []map[string]interface{} {
messageSegments = append(messageSegments, imageSegment)

// 在msg.Content中替换旧的图片链接
newImagePattern := "[CQ:image,file=" + attachment.URL + "]"
msg.Content = msg.Content + newImagePattern
//newImagePattern := "[CQ:image,file=" + attachment.URL + "]"
//msg.Content = msg.Content + newImagePattern
}
// 将msg.Content里的BotID替换成AppID
msg.Content = strings.ReplaceAll(msg.Content, BotID, AppID)
Expand Down Expand Up @@ -477,6 +479,8 @@ func ConvertToSegmentedMessage(data interface{}) []map[string]interface{} {
}
messageSegments = append(messageSegments, textSegment)
}
//排列
messageSegments = sortMessageSegments(messageSegments)
return messageSegments
}

Expand All @@ -494,3 +498,22 @@ func ConvertToInt64(value interface{}) (int64, error) {
return 0, fmt.Errorf("无法将类型 %T 转换为 int64", value)
}
}

// 排列MessageSegments
func sortMessageSegments(segments []map[string]interface{}) []map[string]interface{} {
var atSegments, textSegments, imageSegments []map[string]interface{}

for _, segment := range segments {
switch segment["type"] {
case "at":
atSegments = append(atSegments, segment)
case "text":
textSegments = append(textSegments, segment)
case "image":
imageSegments = append(imageSegments, segment)
}
}

// 按照指定的顺序合并这些切片
return append(append(atSegments, textSegments...), imageSegments...)
}
37 changes: 20 additions & 17 deletions handlers/send_group_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func handleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openap
}
message.Params.GroupID = originalGroupID
mylog.Println("群组发信息messageText:", messageText)
//mylog.Println("foundItems:", foundItems)
mylog.Println("foundItems:", foundItems)
// 如果messageID为空,通过函数获取
if messageID == "" {
messageID = GetMessageIDByUseridOrGroupid(config.GetAppIDStr(), message.Params.GroupID)
Expand Down Expand Up @@ -121,24 +121,27 @@ func handleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openap

// 遍历foundItems并发送每种信息
for key, urls := range foundItems {
var singleItem = make(map[string][]string)
singleItem[key] = urls
for _, url := range urls {
var singleItem = make(map[string][]string)
singleItem[key] = []string{url} // 创建一个只包含一个 URL 的 singleItem
mylog.Println("singleItem:", singleItem)

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

// 进行类型断言
richMediaMessage, ok := groupReply.(*dto.RichMediaMessage)
if !ok {
mylog.Printf("Error: Expected RichMediaMessage type for key %s.", key)
continue // 跳过这个项,继续下一个
}
mylog.Printf("richMediaMessage: %+v\n", richMediaMessage)
_, err := apiv2.PostGroupMessage(context.TODO(), message.Params.GroupID.(string), richMediaMessage)
if err != nil {
mylog.Printf("发送 %s 信息失败_send_group_msg: %v", key, err)
// 进行类型断言
richMediaMessage, ok := groupReply.(*dto.RichMediaMessage)
if !ok {
mylog.Printf("Error: Expected RichMediaMessage type for key %s.", key)
continue // 跳过这个项,继续下一个
}
mylog.Printf("richMediaMessage: %+v\n", richMediaMessage)
_, err := apiv2.PostGroupMessage(context.TODO(), message.Params.GroupID.(string), richMediaMessage)
if err != nil {
mylog.Printf("发送 %s 信息失败_send_group_msg: %v", key, err)
}
//发送成功回执
SendResponse(client, err, &message)
}
//发送成功回执
SendResponse(client, err, &message)
}
case "guild":
//用GroupID给ChannelID赋值,因为我们是把频道虚拟成了群
Expand Down Expand Up @@ -190,7 +193,7 @@ 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 {
// 从本地路径读取图片
Expand Down
57 changes: 29 additions & 28 deletions handlers/send_guild_channel_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,36 +89,37 @@ func handleSendGuildChannelMsg(client callapi.Client, api openapi.OpenAPI, apiv2

// 遍历foundItems并发送每种信息
for key, urls := range foundItems {
var singleItem = make(map[string][]string)
singleItem[key] = urls

reply, isBase64Image := GenerateReplyMessage(messageID, singleItem, "")

if isBase64Image {
// 将base64内容从reply的Content转换回字节
fileImageData, err := base64.StdEncoding.DecodeString(reply.Content)
if err != nil {
mylog.Printf("Base64 解码失败: %v", err)
return // 或其他的错误处理方式
for _, url := range urls {
var singleItem = make(map[string][]string)
singleItem[key] = []string{url} // 创建一个只有一个 URL 的 singleItem

reply, isBase64Image := GenerateReplyMessage(messageID, singleItem, "")

if isBase64Image {
// 将base64内容从reply的Content转换回字节
fileImageData, err := base64.StdEncoding.DecodeString(reply.Content)
if err != nil {
mylog.Printf("Base64 解码失败: %v", err)
continue // 跳过当前项,继续下一个
}

// 清除reply的Content
reply.Content = ""

// 使用Multipart方法发送
if _, err = api.PostMessageMultipart(context.TODO(), channelID, reply, fileImageData); err != nil {
mylog.Printf("使用multipart发送 %s 信息失败: %v message_id %v", key, err, messageID)
}
//发送成功回执
SendResponse(client, err, &message)
} else {
if _, err = api.PostMessage(context.TODO(), channelID, reply); err != nil {
mylog.Printf("发送 %s 信息失败: %v", key, err)
}
//发送成功回执
SendResponse(client, err, &message)
}

// 清除reply的Content
reply.Content = ""

// 使用Multipart方法发送
if _, err = api.PostMessageMultipart(context.TODO(), channelID, reply, fileImageData); err != nil {
mylog.Printf("使用multipart发送 %s 信息失败: %v message_id %v", key, err, messageID)
}
//发送成功回执
SendResponse(client, err, &message)
} else {
if _, err = api.PostMessage(context.TODO(), channelID, reply); err != nil {
mylog.Printf("发送 %s 信息失败: %v", key, err)
}
//发送成功回执
SendResponse(client, err, &message)
}

}
//频道私信 此时直接取出
case "guild_private":
Expand Down
64 changes: 34 additions & 30 deletions handlers/send_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,25 +123,27 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope

// 遍历foundItems并发送每种信息
for key, urls := range foundItems {
var singleItem = make(map[string][]string)
singleItem[key] = urls
for _, url := range urls {
var singleItem = make(map[string][]string)
singleItem[key] = []string{url} // 创建一个只包含一个 URL 的 singleItem

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

// 进行类型断言
richMediaMessage, ok := groupReply.(*dto.RichMediaMessage)
if !ok {
mylog.Printf("Error: Expected RichMediaMessage type for key %s.", key)
continue // 跳过这个项,继续下一个
}
// 进行类型断言
richMediaMessage, ok := groupReply.(*dto.RichMediaMessage)
if !ok {
mylog.Printf("Error: Expected RichMediaMessage type for key %s.", key)
continue // 跳过这个项,继续下一个
}

mylog.Printf("richMediaMessage: %+v\n", richMediaMessage)
_, err = apiv2.PostGroupMessage(context.TODO(), message.Params.GroupID.(string), richMediaMessage)
if err != nil {
mylog.Printf("发送 %s 信息失败_send_msg: %v", key, err)
mylog.Printf("richMediaMessage: %+v\n", richMediaMessage)
_, err := apiv2.PostGroupMessage(context.TODO(), message.Params.GroupID.(string), richMediaMessage)
if err != nil {
mylog.Printf("发送 %s 信息失败_send_msg: %v", key, err)
}
//发送成功回执
SendResponse(client, err, &message)
}
//发送成功回执
SendResponse(client, err, &message)
}
case "guild":
//用GroupID给ChannelID赋值,因为我们是把频道虚拟成了群
Expand Down Expand Up @@ -216,25 +218,27 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope
SendResponse(client, err, &message)
}

// 遍历 foundItems 并发送每种信息
// 遍历foundItems并发送每种信息
for key, urls := range foundItems {
var singleItem = make(map[string][]string)
singleItem[key] = urls
for _, url := range urls {
var singleItem = make(map[string][]string)
singleItem[key] = []string{url} // 创建一个只包含一个 URL 的 singleItem

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

// 进行类型断言
richMediaMessage, ok := groupReply.(*dto.RichMediaMessage)
if !ok {
mylog.Printf("Error: Expected RichMediaMessage type for key %s.", key)
continue
}
_, err = apiv2.PostC2CMessage(context.TODO(), UserID, richMediaMessage)
if err != nil {
mylog.Printf("发送 %s 私聊信息失败: %v", key, err)
// 进行类型断言
richMediaMessage, ok := groupReply.(*dto.RichMediaMessage)
if !ok {
mylog.Printf("Error: Expected RichMediaMessage type for key %s.", key)
continue // 跳过这个项,继续下一个
}
_, err = apiv2.PostC2CMessage(context.TODO(), UserID, richMediaMessage)
if err != nil {
mylog.Printf("发送 %s 私聊信息失败: %v", key, err)
}
//发送成功回执
SendResponse(client, err, &message)
}
//发送成功回执
SendResponse(client, err, &message)
}
default:
mylog.Printf("1Unknown message type: %s", msgType)
Expand Down
Loading