-
Notifications
You must be signed in to change notification settings - Fork 42
/
put_interaction.go
90 lines (76 loc) · 2.19 KB
/
put_interaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package handlers
import (
"context"
"encoding/json"
"fmt"
"github.com/hoshinonyaruko/gensokyo/callapi"
"github.com/hoshinonyaruko/gensokyo/mylog"
"github.com/tencent-connect/botgo/openapi"
)
type InteractionResponse struct {
Data string `json:"data"`
Message string `json:"message"`
RetCode int `json:"retcode"`
Status string `json:"status"`
Echo interface{} `json:"echo"`
}
func init() {
callapi.RegisterHandler("put_interaction", HandlePutInteraction)
}
func HandlePutInteraction(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.OpenAPI, message callapi.ActionMessage) (string, error) {
// 解析 ActionMessage 中的 Echo 字段获取 interactionID
interactionID, ok := message.Echo.(string)
if !ok {
return "", fmt.Errorf("echo is not a string")
}
// 根据 PostType 解析出 code 的值
var code int
switch message.PostType {
case "0":
code = 0 // 成功
case "1":
code = 1 // 操作失败
case "2":
code = 2 // 操作频繁
case "3":
code = 3 // 重复操作
case "4":
code = 4 // 没有权限
case "5":
code = 5 // 仅管理员操作
default:
// 如果 PostType 不在预期范围内,可以设置一个默认值或返回错误
return "", fmt.Errorf("invalid post type: %s", message.PostType)
}
// 构造请求体,包括 code
requestBody := fmt.Sprintf(`{"code": %d}`, code)
// 调用 PutInteraction API
ctx := context.Background()
err := api.PutInteraction(ctx, interactionID, requestBody)
if err != nil {
return "", err
}
var response InteractionResponse
response.Data = ""
response.Message = ""
response.RetCode = 0
response.Status = "ok"
response.Echo = message.Echo
// Convert the members slice to a map
outputMap := structToMap(response)
mylog.Printf("put_interaction: %+v\n", outputMap)
err = client.SendMessage(outputMap)
if err != nil {
mylog.Printf("Error sending message via client: %v", err)
} else {
mylog.Printf("响应put_interaction: %+v", outputMap)
}
//把结果从struct转换为json
result, err := json.Marshal(response)
if err != nil {
mylog.Printf("Error marshaling data: %v", err)
//todo 符合onebotv11 ws返回的错误码
return "", nil
}
return string(result), nil
}