forked from BotBuilderCommunity/botbuilder-community-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlexaContextExtensions.cs
More file actions
161 lines (131 loc) · 6.83 KB
/
Copy pathAlexaContextExtensions.cs
File metadata and controls
161 lines (131 loc) · 6.83 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Bot.Builder.Community.Adapters.Alexa.Directives;
using Microsoft.Bot.Builder;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Bot.Builder.Community.Adapters.Alexa
{
public static class AlexaContextExtensions
{
public static Dictionary<string, string> AlexaSessionAttributes(this ITurnContext context)
{
return context.TurnState.Get<Dictionary<string, string>>("AlexaSessionAttributes");
}
public static List<IAlexaDirective> AlexaResponseDirectives(this ITurnContext context)
{
return context.TurnState.Get<List<IAlexaDirective>>("AlexaResponseDirectives");
}
public static void AlexaSetRepromptSpeech(this ITurnContext context, string repromptSpeech)
{
context.TurnState.Add("AlexaReprompt", repromptSpeech);
}
public static void AlexaSetCard(this ITurnContext context, AlexaCard card)
{
context.TurnState.Add("AlexaCard", card);
}
public static async Task<HttpResponseMessage> AlexaSendProgressiveResponse(this ITurnContext context, string content)
{
var originalAlexaRequest = (AlexaRequestBody)context.Activity.ChannelData;
var directive = new AlexaDirectiveRequest()
{
Header = new AlexaDirectiveRequest.DirectiveHeader()
{
RequestId = originalAlexaRequest.Request.RequestId
},
Directive = new AlexaDirectiveRequest.DirectiveContent()
{
Type = "VoicePlayer.Speak",
Speech = content
}
};
var client = new HttpClient();
var jsonRequest = JsonConvert.SerializeObject(directive,
new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });
var directiveContent = new StringContent(jsonRequest, System.Text.Encoding.UTF8, "application/json");
var directiveEndpoint = $"{originalAlexaRequest.Context.System.ApiEndpoint}/v1/directives";
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", originalAlexaRequest.Context.System.ApiAccessToken);
return await client.PostAsync(directiveEndpoint, directiveContent);
}
public static AlexaRequestBody GetAlexaRequestBody(this ITurnContext context)
{
try
{
return (AlexaRequestBody)context.Activity.ChannelData;
}
catch (Exception ex)
{
return null;
}
}
public static bool AlexaDeviceHasDisplay(this ITurnContext context)
{
var alexaRequest = (AlexaRequestBody)context.Activity.ChannelData;
var hasDisplay =
alexaRequest?.Context?.System?.Device?.SupportedInterfaces?.Interfaces?.Keys.Contains("Display");
return hasDisplay.HasValue && hasDisplay.Value;
}
public static bool AlexaDeviceHasAudioPlayer(this ITurnContext context)
{
var alexaRequest = (AlexaRequestBody)context.Activity.ChannelData;
var hasDisplay =
alexaRequest?.Context?.System?.Device?.SupportedInterfaces?.Interfaces?.Keys.Contains("AudioPlayer");
return hasDisplay.HasValue && hasDisplay.Value;
}
public static async Task<AlexaAddress> AlexaGetUserAddress(this ITurnContext context)
{
var originalAlexaRequest = (AlexaRequestBody)context.Activity.ChannelData;
var deviceId = originalAlexaRequest.Context.System.Device.DeviceId;
var client = new HttpClient();
var directiveEndpoint = $"{originalAlexaRequest.Context.System.ApiEndpoint}/v1/devices/{deviceId}/settings/address";
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", originalAlexaRequest.Context.System.ApiAccessToken);
var response = await client.GetAsync(directiveEndpoint);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var responseContent = await response.Content.ReadAsStringAsync();
var address = JsonConvert.DeserializeObject<AlexaAddress>(responseContent);
return address;
}
else if (response.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
throw new UnauthorizedAccessException($"Alexa API returned status " +
$"code {response.StatusCode} with message {response.ReasonPhrase}. " +
$"This potentially means that the user has not granted your skill " +
$"permission to access their address.");
}
throw new Exception($"Alexa API returned status code " +
$"{response.StatusCode} with message {response.ReasonPhrase}");
}
public static async Task<string> AlexaGetCustomerProfile(this ITurnContext context, string item)
{
if ((item != AlexaCustomerItem.Name) & (item != AlexaCustomerItem.GivenName) & (item != AlexaCustomerItem.Email) & (item != AlexaCustomerItem.MobileNumber))
throw new ArgumentException($"Invalid AlexaGetCustomerProfile item: {item}");
var originalAlexaRequest = (AlexaRequestBody)context.Activity.ChannelData;
var client = new HttpClient();
var directiveEndpoint = $"{originalAlexaRequest.Context.System.ApiEndpoint}/v2/accounts/~current/settings/Profile.{item}";
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", originalAlexaRequest.Context.System.ApiAccessToken);
var response = await client.GetAsync(directiveEndpoint);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var responseContent = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<string>(responseContent);
return data;
}
else if (response.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
throw new UnauthorizedAccessException($"Alexa API returned status " +
$"code {response.StatusCode} with message {response.ReasonPhrase}. " +
$"This potentially means that the user has not granted your skill " +
$"permission to access their profile item {item}.");
}
throw new Exception($"Alexa API returned status code " +
$"{response.StatusCode} with message {response.ReasonPhrase}");
}
}
}