@@ -2,17 +2,19 @@ package main
22
33import (
44 "context"
5+ "encoding/json"
6+ "errors"
57 "fmt"
68 "log"
79 "path"
810 "runtime"
911 "strings"
10- "syscall"
1112 "time"
1213
1314 "github.com/tencent-connect/botgo"
1415 "github.com/tencent-connect/botgo/dto"
1516 "github.com/tencent-connect/botgo/dto/message"
17+ "github.com/tencent-connect/botgo/openapi"
1618 "github.com/tencent-connect/botgo/token"
1719 "github.com/tencent-connect/botgo/websocket"
1820)
@@ -41,7 +43,7 @@ func main() {
4143
4244 processor = Processor {api : api }
4345
44- websocket .RegisterResumeSignal (syscall .SIGUSR1 )
46+ // websocket.RegisterResumeSignal(syscall.SIGUSR1)
4547 // 根据不同的回调,生成 intents
4648 intent := websocket .RegisterHandlers (
4749 // at 机器人事件,目前是在这个事件处理中有逻辑,会回消息,其他的回调处理都只把数据打印出来,不做任何处理
@@ -60,8 +62,9 @@ func main() {
6062 DirectMessageHandler (),
6163 // 频道消息,只有私域才能够收到这个,如果你的机器人不是私域机器人,会导致连接报错,那么启动 example 就需要注释掉这个回调
6264 CreateMessageHandler (),
65+ // 互动事件
66+ InteractionHandler (api ),
6367 )
64-
6568 // 指定需要启动的分片数为 2 的话可以手动修改 wsInfo
6669 // wsInfo.Shards = 2
6770 if err = botgo .NewSessionManager ().Start (wsInfo , botToken , & intent ); err != nil {
@@ -125,10 +128,56 @@ func CreateMessageHandler() websocket.MessageEventHandler {
125128 }
126129}
127130
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+
128141func getConfigPath (name string ) string {
129142 _ , filename , _ , ok := runtime .Caller (1 )
130143 if ok {
131144 return fmt .Sprintf ("%s/%s" , path .Dir (filename ), name )
132145 }
133146 return ""
134147}
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