-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.go
146 lines (123 loc) · 3.92 KB
/
webhook.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package gcp_dialogflow
import (
"bytes"
"regexp"
"fmt"
"github.com/gjbae1212/go-module/gcp"
"github.com/golang/protobuf/jsonpb"
dialogflow "google.golang.org/genproto/googleapis/cloud/dialogflow/v2"
)
var (
sessionRegex = regexp.MustCompile("projects/(.+)/agent/(.+)")
contextRegex = regexp.MustCompile("projects/(.+)/agent/(.+)/contexts/(.+)")
intentRegex = regexp.MustCompile("projects/(.+)/agent/intents/(.+)")
)
// https://dialogflow.com/docs/fulfillment/how-it-works
// https://developers.google.com/actions/build/json/
func GenerateSession(projectId, sessionId string) (string, error) {
if projectId == "" || sessionId == "" {
return "", gcp.EmptyError.New("webhook GenerateSession")
}
return fmt.Sprintf("projects/%s/agent/%s", projectId, sessionId), nil
}
func GenerateContext(projectId, sessionId, contextId string) (string, error) {
if projectId == "" || sessionId == "" || contextId == "" {
return "", gcp.EmptyError.New("webhook GenerateContext")
}
return fmt.Sprintf("projects/%s/agent/%s/contexts/%s", projectId, sessionId, contextId), nil
}
func GenerateIntent(projectId, intentId string) (string, error) {
if projectId == "" || intentId == "" {
return "", gcp.EmptyError.New("webhook GenerateIntent")
}
return fmt.Sprintf("projects/%s/agent/intents/%s", projectId, intentId), nil
}
func ParseSession(session string) (projectId, sessionId string, err error) {
if session == "" {
err = gcp.EmptyError.New("webhook ParseSession")
return
}
matches := sessionRegex.FindStringSubmatch(session)
if len(matches) == 0 || len(matches) != 3 {
err = gcp.InvalidError.New("webhook ParseSession invalid format")
return
}
projectId = matches[1]
sessionId = matches[len(matches)-1]
return
}
func ParseContext(context string) (projectId, sessionId string, contextId string, err error) {
if context == "" {
err = gcp.EmptyError.New("webhook ParseContext")
return
}
matches := contextRegex.FindStringSubmatch(context)
if len(matches) == 0 || len(matches) != 4 {
err = gcp.InvalidError.New("webhook ParseContext invalid format")
return
}
projectId = matches[1]
sessionId = matches[len(matches)-2]
contextId = matches[len(matches)-1]
return
}
func ParseIntent(intent string) (projectId, intentId string, err error) {
if intent == "" {
err = gcp.EmptyError.New("webhook ParseIntent")
return
}
matches := intentRegex.FindStringSubmatch(intent)
if len(matches) == 0 || len(matches) != 3 {
err = gcp.InvalidError.New("webhook ParseIntent invalid format")
return
}
projectId = matches[1]
intentId = matches[2]
return
}
func JsonToWebhookRequest(json []byte) (*dialogflow.WebhookRequest, error) {
if len(json) == 0 {
return nil, gcp.EmptyError.New("webhook JsonToWebhookRequest")
}
unmarshaler := &jsonpb.Unmarshaler{}
unmarshaler.AllowUnknownFields = true
wr := &dialogflow.WebhookRequest{}
if err := unmarshaler.Unmarshal(bytes.NewReader(json), wr); err != nil {
return nil, err
}
return wr, nil
}
func WebhookRequestToJson(req *dialogflow.WebhookRequest) ([]byte, error) {
if req == nil {
return nil, gcp.EmptyError.New("webhook WebhookRequestToJson")
}
marshaler := &jsonpb.Marshaler{}
json, err := marshaler.MarshalToString(req)
if err != nil {
return nil, err
}
return []byte(json), err
}
func JsonToWebhookResponse(json []byte) (*dialogflow.WebhookResponse, error) {
if len(json) == 0 {
return nil, gcp.EmptyError.New("webhook JsonToWebhookResponse")
}
unmarshaler := &jsonpb.Unmarshaler{}
unmarshaler.AllowUnknownFields = true
wr := &dialogflow.WebhookResponse{}
if err := unmarshaler.Unmarshal(bytes.NewReader(json), wr); err != nil {
return nil, err
}
return wr, nil
}
func WebhookResponseToJson(res *dialogflow.WebhookResponse) ([]byte, error) {
if res == nil {
return nil, gcp.EmptyError.New("webhook WebhookResponseToJson")
}
marshaler := &jsonpb.Marshaler{}
json, err := marshaler.MarshalToString(res)
if err != nil {
return nil, err
}
return []byte(json), err
}