-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialogflow.go
145 lines (126 loc) · 3.59 KB
/
dialogflow.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
package gcp_dialogflow
import (
"context"
"fmt"
dialogflow "cloud.google.com/go/dialogflow/apiv2"
"github.com/gjbae1212/go-module/gcp"
"github.com/golang/protobuf/ptypes/struct"
dialogflowpb "google.golang.org/genproto/googleapis/cloud/dialogflow/v2"
)
var (
defaultLanguage = "ko"
defaultTimezone = "Asia/Tokyo"
)
type DialogFlow struct {
projectId string
client *dialogflow.SessionsClient
}
type Request struct {
Message string // required
Username string // required
Language string // optional <https://dialogflow.com/docs/languages>
Timezone string // optional <https://www.iana.org/time-zones>
IntentContext string // optional context
IntentContextDeadline int32 // optional context lifespan count
ResetContext bool // optional whether delete all contexts in the current session
}
type Response struct {
Intent string
Confidence float32
Entities map[string]*structpb.Value
Response string
}
func (req *Request) Validate() bool {
if req.Message == "" {
return false
}
if req.Username == "" {
return false
}
if req.Language == "" {
req.Language = defaultLanguage
}
if req.Timezone == "" {
req.Timezone = defaultTimezone
}
return true
}
func (req *Request) Session(projectId string) string {
return fmt.Sprintf("projects/%s/agent/sessions/%s", projectId, req.Username)
}
func (req *Request) Contexts(projectId string) []*dialogflowpb.Context {
var contexts []*dialogflowpb.Context
if req.IntentContext != "" {
name := fmt.Sprintf("projects/%s/agent/sessions/%s/contexts/%s", projectId, req.Username, req.IntentContext)
contexts = append(contexts, &dialogflowpb.Context{Name: name, LifespanCount: req.IntentContextDeadline})
}
return contexts
}
func (res *Response) Set(result *dialogflowpb.DetectIntentResponse) error {
if result == nil {
return gcp.EmptyError.New("Response Set")
}
query := result.GetQueryResult()
if query.Intent != nil {
res.Intent = query.Intent.DisplayName
res.Confidence = float32(query.IntentDetectionConfidence)
}
res.Entities = make(map[string]*structpb.Value)
for name, value := range query.Parameters.GetFields() {
res.Entities[name] = value
}
res.Response = result.QueryResult.GetFulfillmentText()
return nil
}
func (res *Response) GetEntity(name string) interface{} {
v, ok := res.Entities[name]
if !ok {
return nil
}
switch v.GetKind().(type) {
case *structpb.Value_StringValue:
return v.GetStringValue()
case *structpb.Value_NumberValue:
return v.GetNumberValue()
case *structpb.Value_BoolValue:
return v.GetBoolValue()
case *structpb.Value_StructValue:
return v.GetStructValue()
case *structpb.Value_ListValue:
return v.GetListValue()
case *structpb.Value_NullValue:
return v.GetNullValue()
default:
return nil
}
}
func (df *DialogFlow) DetectIntent(ctx context.Context, req *Request) (*Response, error) {
if !req.Validate() {
return nil, gcp.InvalidError.New("DialogFlow DetectIntent")
}
request := &dialogflowpb.DetectIntentRequest{
Session: req.Session(df.projectId),
QueryInput: &dialogflowpb.QueryInput{
Input: &dialogflowpb.QueryInput_Text{
Text: &dialogflowpb.TextInput{
Text: req.Message,
LanguageCode: req.Language,
},
},
},
QueryParams: &dialogflowpb.QueryParameters{
TimeZone: req.Timezone,
ResetContexts: req.ResetContext,
Contexts: req.Contexts(df.projectId),
},
}
response, err := df.client.DetectIntent(ctx, request)
if err != nil {
return nil, err
}
value := &Response{}
if err := value.Set(response); err != nil {
return nil, err
}
return value, nil
}