/
DialogFlowManager.cs
77 lines (52 loc) · 2.45 KB
/
DialogFlowManager.cs
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
using Google.Cloud.Dialogflow.V2;
using Google.Protobuf.WellKnownTypes;
using Newtonsoft.Json;
namespace SZR
{
public static class DialogFlowManager
{
public static WebhookResponse GetDialogFlowResponse(string userId, string message)
{
WebhookResponse webHookResp = message.StartsWith("Sorry") ? InitializeResponse(false, userId) : InitializeResponse(true, userId);
var fulfillmentMessage = webHookResp.FulfillmentMessages[0];
fulfillmentMessage.SimpleResponses = new Intent.Types.Message.Types.SimpleResponses();
var simpleResp = new Intent.Types.Message.Types.SimpleResponse();
simpleResp.Ssml = $"<speak>{message}</speak>";
fulfillmentMessage.SimpleResponses.SimpleResponses_.Add(simpleResp);
return webHookResp;
}
private static WebhookResponse InitializeResponse(bool expectUserInput, string userId)
{
WebhookResponse webResp = new WebhookResponse();
var message = new Intent.Types.Message();
webResp.FulfillmentMessages.Add(message);
message.Platform = Intent.Types.Message.Types.Platform.ActionsOnGoogle;
Value payloadVal = new Value();
payloadVal.StructValue = new Struct();
Value expectedUserResp = new Value();
expectedUserResp.BoolValue = expectUserInput;
payloadVal.StructValue.Fields.Add("expectUserResponse", expectedUserResp);
Value userStorageValue = new Value();
UserStorage userStorage = new UserStorage();
userStorage.UserId = userId;
userStorageValue.StringValue = JsonConvert.SerializeObject(userStorage);
payloadVal.StructValue.Fields.Add("userStorage", userStorageValue);
Struct payloadStruct = new Struct();
payloadStruct.Fields.Add("google", payloadVal);
webResp.Payload = payloadStruct;
return webResp;
}
private static string GetUserId(WebhookRequest request)
{
string userId = null;
Struct intentRequestPayload = request.OriginalDetectIntentRequest?.Payload;
return userId;
}
[JsonObject]
public class UserStorage
{
[JsonProperty(PropertyName = "userId")]
public string UserId { get; set; }
}
}
}