Skip to content

Commit

Permalink
Test4 (#23)
Browse files Browse the repository at this point in the history
* Compiled main.go and pushed changes

* test

* 适配了频道私聊,用bolt数据库取代ini

* 适配了nonebot2

* add license

* add a lot

* trss support

* add action

* add action

* add action

* fixbug

* add wss

* bugfix

* fix action

* fix action again

* fa

* fix

* add a lot

* add ws server token

* bugifx

* fixat
  • Loading branch information
Hoshinonyaruko authored Oct 28, 2023
1 parent 0404531 commit 87ab62f
Show file tree
Hide file tree
Showing 16 changed files with 222 additions and 100 deletions.
77 changes: 44 additions & 33 deletions callapi/callapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,66 @@ import (
"encoding/json"
"fmt"
"log"
"strconv"

"github.com/tencent-connect/botgo/openapi"
)

type EchoData struct {
Seq int `json:"seq"`
// onebot发来的action调用信息
type ActionMessage struct {
Action string `json:"action"`
Params ParamsContent `json:"params"`
Echo interface{} `json:"echo,omitempty"`
}

type EchoContent string
func (a *ActionMessage) UnmarshalJSON(data []byte) error {
type Alias ActionMessage

func (e *EchoContent) UnmarshalJSON(data []byte) error {
// 尝试解析为字符串
var strVal string
if err := json.Unmarshal(data, &strVal); err == nil {
*e = EchoContent(strVal)
return nil
var rawEcho json.RawMessage
temp := &struct {
*Alias
Echo *json.RawMessage `json:"echo,omitempty"`
}{
Alias: (*Alias)(a),
Echo: &rawEcho,
}

// 尝试解析为整数
var intVal int
if err := json.Unmarshal(data, &intVal); err == nil {
*e = EchoContent(strconv.Itoa(intVal))
return nil
if err := json.Unmarshal(data, &temp); err != nil {
return err
}

// 尝试解析为EchoData结构体
var echoData EchoData
if err := json.Unmarshal(data, &echoData); err == nil {
*e = EchoContent(strconv.Itoa(echoData.Seq))
return nil
if rawEcho != nil {
var lastErr error

var intValue int
if lastErr = json.Unmarshal(rawEcho, &intValue); lastErr == nil {
a.Echo = intValue
return nil
}

var strValue string
if lastErr = json.Unmarshal(rawEcho, &strValue); lastErr == nil {
a.Echo = strValue
return nil
}

var arrValue []interface{}
if lastErr = json.Unmarshal(rawEcho, &arrValue); lastErr == nil {
a.Echo = arrValue
return nil
}

var objValue map[string]interface{}
if lastErr = json.Unmarshal(rawEcho, &objValue); lastErr == nil {
a.Echo = objValue
return nil
}

return fmt.Errorf("unable to unmarshal echo: %v", lastErr)
}

// 如果都不符合预期,设置为空字符串
*e = ""
return nil
}

// func (e EchoContent) String() string {
// return string(e)
// }

// onebot发来的action调用信息
type ActionMessage struct {
Action string `json:"action"`
Params ParamsContent `json:"params"`
Echo EchoContent `json:"echo,omitempty"`
}

// params类型
type ParamsContent struct {
BotQQ string `json:"botqq"`
Expand Down
9 changes: 6 additions & 3 deletions handlers/get_group_member_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,12 @@ func buildResponse(members []MemberList, echoValue interface{}) map[string]inter
case string:
log.Printf("Setting echo as string: %s", v)
response["echo"] = v
case callapi.EchoContent:
log.Printf("Setting echo from EchoContent: %s", string(v))
response["echo"] = string(v)
case []interface{}:
log.Printf("Setting echo as array: %v", v)
response["echo"] = v
case map[string]interface{}:
log.Printf("Setting echo as object: %v", v)
response["echo"] = v
default:
log.Printf("Unknown type for echo: %T", v)
}
Expand Down
2 changes: 1 addition & 1 deletion handlers/get_guild_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func getGuildList(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Open
response.Message = ""
response.RetCode = 0
response.Status = "ok"
response.Echo = string(message.Echo) // Directly assign the string value
response.Echo = message.Echo

// Convert the members slice to a map
outputMap := structToMap(response)
Expand Down
2 changes: 1 addition & 1 deletion handlers/get_guild_service_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func getGuildServiceProfile(client callapi.Client, api openapi.OpenAPI, apiv2 op
response.Message = ""
response.RetCode = 0
response.Status = "ok"
response.Echo = string(message.Echo) // Directly assign the string value
response.Echo = message.Echo

// Convert the members slice to a map
outputMap := structToMap(response)
Expand Down
2 changes: 1 addition & 1 deletion handlers/get_login_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func getLoginInfo(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Open
response.Message = ""
response.RetCode = 0
response.Status = "ok"
response.Echo = string(message.Echo)
response.Echo = message.Echo

// Convert the members slice to a map
outputMap := structToMap(response)
Expand Down
2 changes: 1 addition & 1 deletion handlers/get_online_clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func getOnlineClients(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.
response.Message = ""
response.RetCode = 0
response.Status = "ok"
response.Echo = string(message.Echo)
response.Echo = message.Echo

// Convert the members slice to a map
outputMap := structToMap(response)
Expand Down
2 changes: 1 addition & 1 deletion handlers/get_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func getStatus(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.OpenAPI
response.Message = ""
response.RetCode = 0
response.Status = "ok"
response.Echo = string(message.Echo) // Directly assign the string value
response.Echo = message.Echo

outputMap := structToMap(response)

Expand Down
2 changes: 1 addition & 1 deletion handlers/get_version_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func getVersionInfo(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Op
response.Message = ""
response.RetCode = 0
response.Status = "ok"
response.Echo = string(message.Echo) // Directly assign the string value
response.Echo = message.Echo

// Convert the members slice to a map
outputMap := structToMap(response)
Expand Down
18 changes: 12 additions & 6 deletions handlers/message_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/hoshinonyaruko/gensokyo/callapi"
"github.com/hoshinonyaruko/gensokyo/idmap"
"github.com/tencent-connect/botgo/dto"
)

Expand All @@ -19,18 +20,18 @@ type ServerResponse struct {
Data struct {
MessageID int `json:"message_id"`
} `json:"data"`
Message string `json:"message"`
RetCode int `json:"retcode"`
Status string `json:"status"`
Echo string `json:"echo"`
Message string `json:"message"`
RetCode int `json:"retcode"`
Status string `json:"status"`
Echo interface{} `json:"echo"`
}

// 发送成功回执 todo 返回可互转的messageid
func SendResponse(client callapi.Client, err error, message *callapi.ActionMessage) error {
// 设置响应值
response := ServerResponse{}
response.Data.MessageID = 0 // todo 实现messageid转换
response.Echo = string(message.Echo)
response.Echo = message.Echo
if err != nil {
response.Message = err.Error() // 可选:在响应中添加错误消息
//response.RetCode = -1 // 可以是任何非零值,表示出错
Expand Down Expand Up @@ -154,7 +155,12 @@ func transformMessageText(messageText string) string {
return re.ReplaceAllStringFunc(messageText, func(m string) string {
submatches := re.FindStringSubmatch(m)
if len(submatches) > 1 {
return "<@!" + submatches[1] + ">"
realUserID, err := idmap.RetrieveRowByIDv2(submatches[1])
if err != nil {
log.Printf("Error retrieving user ID: %v", err)
return m // 如果出错,返回原始匹配
}
return "<@!" + realUserID + ">"
}
return m
})
Expand Down
16 changes: 10 additions & 6 deletions handlers/send_group_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ func init() {

func handleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.OpenAPI, message callapi.ActionMessage) {
// 使用 message.Echo 作为key来获取消息类型
msgType := echo.GetMsgTypeByKey(string(message.Echo))
var msgType string
if echoStr, ok := message.Echo.(string); ok {
// 当 message.Echo 是字符串类型时执行此块
msgType = echo.GetMsgTypeByKey(echoStr)
}

//如果获取不到 就用user_id获取信息类型
if msgType == "" {
Expand All @@ -41,12 +45,12 @@ func handleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openap
// 解析消息内容
messageText, foundItems := parseMessageContent(message.Params)

// 获取 echo 的值
echostr := string(message.Echo)

// 使用 echo 获取消息ID
messageID := echo.GetMsgIDByKey(echostr)
log.Println("群组发信息对应的message_id:", messageID)
var messageID string
if echoStr, ok := message.Echo.(string); ok {
messageID = echo.GetMsgIDByKey(echoStr)
log.Println("echo取群组发信息对应的message_id:", messageID)
}
log.Println("群组发信息messageText:", messageText)
log.Println("foundItems:", foundItems)
// 如果messageID为空,通过函数获取
Expand Down
18 changes: 11 additions & 7 deletions handlers/send_guild_channel_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ func init() {

func handleSendGuildChannelMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.OpenAPI, message callapi.ActionMessage) {
// 使用 message.Echo 作为key来获取消息类型
msgType := echo.GetMsgTypeByKey(string(message.Echo))
var msgType string
if echoStr, ok := message.Echo.(string); ok {
// 当 message.Echo 是字符串类型时执行此块
msgType = echo.GetMsgTypeByKey(echoStr)
}

//如果获取不到 就用user_id获取信息类型
if msgType == "" {
Expand All @@ -46,12 +50,12 @@ func handleSendGuildChannelMsg(client callapi.Client, api openapi.OpenAPI, apiv2
messageText, foundItems := parseMessageContent(params)

channelID := params.ChannelID
// 获取 echo 的值
echostr := string(message.Echo)

//messageType := echo.GetMsgTypeByKey(echostr)
messageID := echo.GetMsgIDByKey(echostr)
log.Println("频道发信息对应的message_id:", messageID)
// 使用 echo 获取消息ID
var messageID string
if echoStr, ok := message.Echo.(string); ok {
messageID = echo.GetMsgIDByKey(echoStr)
log.Println("echo取频道发信息对应的message_id:", messageID)
}
log.Println("频道发信息messageText:", messageText)
log.Println("foundItems:", foundItems)
var err error
Expand Down
26 changes: 15 additions & 11 deletions handlers/send_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ func init() {

func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.OpenAPI, message callapi.ActionMessage) {
// 使用 message.Echo 作为key来获取消息类型
msgType := echo.GetMsgTypeByKey(string(message.Echo))
var msgType string
if echoStr, ok := message.Echo.(string); ok {
// 当 message.Echo 是字符串类型时执行此块
msgType = echo.GetMsgTypeByKey(echoStr)
}

//如果获取不到 就用group_id获取信息类型
if msgType == "" {
Expand All @@ -46,12 +50,12 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope
// 解析消息内容
messageText, foundItems := parseMessageContent(message.Params)

// 获取 echo 的值
echostr := string(message.Echo)

// 使用 echo 获取消息ID
messageID := echo.GetMsgIDByKey(echostr)
log.Println("群组发信息对应的message_id:", messageID)
var messageID string
if echoStr, ok := message.Echo.(string); ok {
messageID = echo.GetMsgIDByKey(echoStr)
log.Println("echo取群组发信息对应的message_id:", messageID)
}
log.Println("群组发信息messageText:", messageText)
log.Println("foundItems:", foundItems)
// 如果messageID为空,通过函数获取
Expand Down Expand Up @@ -159,12 +163,12 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope
// 解析消息内容
messageText, foundItems := parseMessageContent(message.Params)

// 获取 echo 的值
echostr := string(message.Echo)

// 使用 echo 获取消息ID
messageID := echo.GetMsgIDByKey(echostr)
log.Println("私聊发信息对应的message_id:", messageID)
var messageID string
if echoStr, ok := message.Echo.(string); ok {
messageID = echo.GetMsgIDByKey(echoStr)
log.Println("echo取私聊发信息对应的message_id:", messageID)
}
log.Println("私聊发信息messageText:", messageText)
log.Println("foundItems:", foundItems)

Expand Down
26 changes: 16 additions & 10 deletions handlers/send_private_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ func init() {

func handleSendPrivateMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.OpenAPI, message callapi.ActionMessage) {
// 使用 message.Echo 作为key来获取消息类型
msgType := echo.GetMsgTypeByKey(string(message.Echo))
var msgType string
if echoStr, ok := message.Echo.(string); ok {
// 当 message.Echo 是字符串类型时执行此块
msgType = echo.GetMsgTypeByKey(echoStr)
}

switch msgType {
case "group_private":
Expand All @@ -37,12 +41,12 @@ func handleSendPrivateMsg(client callapi.Client, api openapi.OpenAPI, apiv2 open
// 解析消息内容
messageText, foundItems := parseMessageContent(message.Params)

// 获取 echo 的值
echostr := string(message.Echo)

// 使用 echo 获取消息ID
messageID := echo.GetMsgIDByKey(echostr)
log.Println("私聊发信息对应的message_id:", messageID)
var messageID string
if echoStr, ok := message.Echo.(string); ok {
messageID = echo.GetMsgIDByKey(echoStr)
log.Println("echo取私聊发信息对应的message_id:", messageID)
}
log.Println("私聊发信息messageText:", messageText)
log.Println("foundItems:", foundItems)

Expand Down Expand Up @@ -142,10 +146,12 @@ func handleSendGuildChannelPrivateMsg(client callapi.Client, api openapi.OpenAPI
}
}

// 获取 echo 的值
echostr := string(message.Echo)
messageID := echo.GetMsgIDByKey(echostr)
log.Println("私聊信息对应的message_id:", messageID)
// 使用 echo 获取消息ID
var messageID string
if echoStr, ok := message.Echo.(string); ok {
messageID = echo.GetMsgIDByKey(echoStr)
log.Println("echo取私聊发信息对应的message_id:", messageID)
}
log.Println("私聊信息messageText:", messageText)
log.Println("foundItems:", foundItems)
// 如果messageID为空,通过函数获取
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func main() {
}(wsAddr)
}

// Collect results
// 获取连接成功后的wsClient
for i := 0; i < len(conf.Settings.WsAddress); i++ {
select {
case wsClient := <-wsClientChan:
Expand Down
Loading

0 comments on commit 87ab62f

Please sign in to comment.