@@ -2,17 +2,19 @@ package main
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
6
+ "errors"
5
7
"fmt"
6
8
"log"
7
9
"path"
8
10
"runtime"
9
11
"strings"
10
- "syscall"
11
12
"time"
12
13
13
14
"github.com/tencent-connect/botgo"
14
15
"github.com/tencent-connect/botgo/dto"
15
16
"github.com/tencent-connect/botgo/dto/message"
17
+ "github.com/tencent-connect/botgo/openapi"
16
18
"github.com/tencent-connect/botgo/token"
17
19
"github.com/tencent-connect/botgo/websocket"
18
20
)
@@ -41,7 +43,7 @@ func main() {
41
43
42
44
processor = Processor {api : api }
43
45
44
- websocket .RegisterResumeSignal (syscall .SIGUSR1 )
46
+ // websocket.RegisterResumeSignal(syscall.SIGUSR1)
45
47
// 根据不同的回调,生成 intents
46
48
intent := websocket .RegisterHandlers (
47
49
// at 机器人事件,目前是在这个事件处理中有逻辑,会回消息,其他的回调处理都只把数据打印出来,不做任何处理
@@ -60,8 +62,9 @@ func main() {
60
62
DirectMessageHandler (),
61
63
// 频道消息,只有私域才能够收到这个,如果你的机器人不是私域机器人,会导致连接报错,那么启动 example 就需要注释掉这个回调
62
64
CreateMessageHandler (),
65
+ // 互动事件
66
+ InteractionHandler (api ),
63
67
)
64
-
65
68
// 指定需要启动的分片数为 2 的话可以手动修改 wsInfo
66
69
// wsInfo.Shards = 2
67
70
if err = botgo .NewSessionManager ().Start (wsInfo , botToken , & intent ); err != nil {
@@ -125,10 +128,56 @@ func CreateMessageHandler() websocket.MessageEventHandler {
125
128
}
126
129
}
127
130
131
+ func InteractionHandler (api openapi.OpenAPI ) websocket.InteractionEventHandler {
132
+ return func (event * dto.WSPayload , data * dto.WSInteractionData ) error {
133
+ fmt .Println (data )
134
+ if err := putInteractionInlineSearch (api , data ); err != nil {
135
+ log .Println ("putInteractionInlineSearch error: " , err )
136
+ }
137
+ return nil
138
+ }
139
+ }
140
+
128
141
func getConfigPath (name string ) string {
129
142
_ , filename , _ , ok := runtime .Caller (1 )
130
143
if ok {
131
144
return fmt .Sprintf ("%s/%s" , path .Dir (filename ), name )
132
145
}
133
146
return ""
134
147
}
148
+
149
+ func putInteractionInlineSearch (api openapi.OpenAPI , interaction * dto.WSInteractionData ) error {
150
+ if interaction .Data .Type != dto .InteractionDataTypeChatSearch {
151
+ return errors .New ("interaction data type not chat search" )
152
+ }
153
+ search := & dto.SearchInputResolved {}
154
+ if err := json .Unmarshal (interaction .Data .Resolved , search ); err != nil {
155
+ return fmt .Errorf ("interaction data resolved unmarshal error:%v" , err )
156
+ }
157
+ if search .Keyword != "test" {
158
+ return errors .New ("resolved search key not allowed" )
159
+ }
160
+ searchRsp := & dto.SearchRsp {
161
+ Layouts : []dto.SearchLayout {
162
+ {
163
+ LayoutType : 0 ,
164
+ ActionType : 0 ,
165
+ Title : "内联搜索" ,
166
+ Records : []dto.SearchRecord {
167
+ {
168
+ Cover : "https://pub.idqqimg.com/pc/misc/files/20211208/311cfc87ce394c62b7c9f0508658cf25.png" ,
169
+ Title : "内联搜索标题" ,
170
+ Tips : "内联搜索 tips" ,
171
+ URL : "https://www.qq.com" ,
172
+ },
173
+ },
174
+ },
175
+ },
176
+ }
177
+ body , _ := json .Marshal (searchRsp )
178
+ if err := api .PutInteraction (context .Background (), interaction .ID , body ); err != nil {
179
+ log .Println ("api call putInteractionInlineSearch error: " , err )
180
+ return err
181
+ }
182
+ return nil
183
+ }
0 commit comments