Skip to content

Commit 7c6485b

Browse files
author
casonli
committed
sdk: 增加 interaction event 和 update interaction 接口 (merge request !39)
Squash merge branch 'feature_20220318_casonli_interaction_story_872352857' into 'master' - 增加 interaction event - 增加 update interaction 接口 TAPD: --story=872352857
1 parent 6c78c48 commit 7c6485b

File tree

13 files changed

+138
-13
lines changed

13 files changed

+138
-13
lines changed

dto/interaction.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package dto
22

3+
import "encoding/json"
4+
35
// Interaction 互动行为对象
46
type Interaction struct {
7+
ID string `json:"id,omitempty"` // 互动行为唯一标识
58
ApplicationID uint64 `json:"application_id,omitempty"` // 应用ID
69
Type InteractionType `json:"type,omitempty"` // 互动类型
710
Data *InteractionData `json:"data,omitempty"` // 互动数据
@@ -22,7 +25,7 @@ const (
2225
type InteractionData struct {
2326
Name string `json:"name,omitempty"` // 标题
2427
Type InteractionDataType `json:"type,omitempty"` // 数据类型,不同数据类型对应不同的 resolved 数据
25-
Resolved interface{} `json:"resolved,omitempty"` // 跟不同的互动类型和数据类型有关系的数据
28+
Resolved json.RawMessage `json:"resolved,omitempty"` // 跟不同的互动类型和数据类型有关系的数据
2629
}
2730

2831
// InteractionDataType 互动数据类型

dto/websocket_event.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ const (
3333
EventForumPostDelete EventType = "FORUM_POST_DELETE"
3434
EventForumReplyCreate EventType = "FORUM_REPLY_CREATE"
3535
EventForumReplyDelete EventType = "FORUM_REPLY_DELETE"
36-
37-
EventForumAuditResult EventType = "FORUM_PUBLISH_AUDIT_RESULT"
36+
EventForumAuditResult EventType = "FORUM_PUBLISH_AUDIT_RESULT"
37+
EventInteractionCreate EventType = "INTERACTION_CREATE"
3838
)
3939

4040
// intentEventMap 不同 intent 对应的事件定义
@@ -54,6 +54,7 @@ var intentEventMap = map[Intent][]EventType{
5454
EventForumThreadCreate, EventForumThreadUpdate, EventForumThreadDelete, EventForumPostCreate,
5555
EventForumPostDelete, EventForumReplyCreate, EventForumReplyDelete, EventForumAuditResult,
5656
},
57+
IntentInteraction: {EventInteractionCreate},
5758
}
5859

5960
var eventIntentMap = transposeIntentEventMap(intentEventMap)

dto/websocket_intent.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ const (
4343
IntentDirectMessageReactions
4444
IntentDirectMessageTyping
4545

46-
IntentAudit Intent = 1 << 27 // 审核事件
46+
IntentInteraction Intent = 1 << 26 // 互动事件
47+
IntentAudit Intent = 1 << 27 // 审核事件
4748
// IntentForum 论坛事件
4849
// - THREAD_CREATE // 当用户创建主题时
4950
// - THREAD_UPDATE // 当用户更新主题时

dto/websocket_payload.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,6 @@ type WSReplyData Reply
9595

9696
// WSForumAuditData 帖子审核事件
9797
type WSForumAuditData ForumAuditResult
98+
99+
// WSInteractionData 互动事件
100+
type WSInteractionData Interaction
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package apitest
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/tencent-connect/botgo/dto"
8+
)
9+
10+
func TestInteractions(t *testing.T) {
11+
t.Run(
12+
"put interaction", func(t *testing.T) {
13+
body, _ := json.Marshal(dto.InteractionData{
14+
Name: "interaction",
15+
Type: 2,
16+
Resolved: "test",
17+
})
18+
err := api.PutInteraction(ctx, testInteractionD, body)
19+
if err != nil {
20+
t.Error(err)
21+
}
22+
},
23+
)
24+
}

examples/apitest/main_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ var (
2626
testChannelID = "1513270" // replace your channel id
2727
testMessageID = `08e092eeb983afef9e0110f9bb5d1a1231343431313532313836373838333234303420801e
2828
28003091c4bb02380c400c48d8a7928d06` // replace your channel id
29-
testRolesID = `10054557` // replace your roles id
30-
testMemberID = `1201318637970874066` // replace your member id
31-
testMarkdownTemplateID = 1231231231231231 // replace your markdown template id
29+
testRolesID = `10054557` // replace your roles id
30+
testMemberID = `1201318637970874066` // replace your member id
31+
testMarkdownTemplateID = 1231231231231231 // replace your markdown template id
32+
testInteractionD = "e924431f-aaed-4e78-83\n75-9295b1f1e0ef" // replace your interaction id
3233
ctx context.Context
3334
)
3435

examples/receive-and-send/main.go

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ package main
22

33
import (
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+
128141
func 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+
}

interaction/simulate_search.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ type SearchConfig struct {
2626
// 开发者可以使用本方法请求自己的服务器进行平台内联搜索的模拟,避免在平台上触发搜索请求。提升联调效率。
2727
func SimulateSearch(config *SearchConfig, keyword string) (*dto.SearchRsp, error) {
2828
interactionData := &dto.InteractionData{
29-
Name: "search",
30-
Type: dto.InteractionDataTypeChatSearch,
31-
Resolved: dto.SearchInputResolved{Keyword: keyword},
29+
Name: "search",
30+
Type: dto.InteractionDataTypeChatSearch,
3231
}
32+
interactionData.Resolved, _ = json.Marshal(dto.SearchInputResolved{Keyword: keyword})
3333
interaction := &dto.Interaction{
3434
ApplicationID: config.AppID,
3535
Type: dto.InteractionTypeCommand,

openapi/iface.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type OpenAPI interface {
3434
APIPermissionsAPI
3535
PinsAPI
3636
MessageReactionAPI
37+
InteractionAPI
3738
}
3839

3940
// Base 基础能力接口
@@ -210,3 +211,9 @@ type MessageReactionAPI interface {
210211
// DeleteOwnMessageReaction 删除自己的消息表情表态
211212
DeleteOwnMessageReaction(ctx context.Context, channelID, messageID string, emoji dto.Emoji) error
212213
}
214+
215+
// InteractionAPI 互动接口
216+
type InteractionAPI interface {
217+
// PutInteraction 更新互动信息
218+
PutInteraction(ctx context.Context, interactionID string, body []byte) error
219+
}

openapi/v1/interaction.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package v1
2+
3+
import (
4+
"context"
5+
)
6+
7+
// PutInteraction 更新 interaction
8+
func (o *openAPI) PutInteraction(ctx context.Context,
9+
interactionID string, body []byte) error {
10+
_, err := o.request(ctx).
11+
SetPathParam("interaction_id", interactionID).
12+
SetBody(body).
13+
Put(o.getURL(interactionsURI))
14+
return err
15+
}

0 commit comments

Comments
 (0)