-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathActionsSdkAdapter.cs
116 lines (116 loc) · 5.61 KB
/
ActionsSdkAdapter.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
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
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Bot.Builder.Community.Adapters.ActionsSDK.Core;
using Bot.Builder.Community.Adapters.ActionsSDK.Core.Model;
using Bot.Builder.Community.Adapters.Shared;
using Microsoft.AspNetCore.Http;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Schema;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Bot.Builder.Community.Adapters.ActionsSDK
{
public class ActionsSdkAdapter : BotAdapter, IBotFrameworkHttpAdapter
{
private static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore,
};
private readonly ILogger _logger;
private readonly ActionsSdkRequestMapperOptions _actionsSdkRequestMapperOptions;
private readonly ActionsSdkAdapterOptions _options;
public ActionsSdkAdapter(ActionsSdkAdapterOptions options = null, ILogger logger = null)
{
_options = options ?? new ActionsSdkAdapterOptions();
_logger = logger ?? NullLogger.Instance;
_actionsSdkRequestMapperOptions = new ActionsSdkRequestMapperOptions()
{
ActionInvocationName = _options.ActionInvocationName,
ShouldEndSessionByDefault = _options.ShouldEndSessionByDefault
};
}
public async Task ProcessAsync(HttpRequest httpRequest, HttpResponse httpResponse, IBot bot, CancellationToken cancellationToken = default)
{
if (httpRequest == null)
{
throw new ArgumentNullException(nameof(httpRequest));
}
if (httpResponse == null)
{
throw new ArgumentNullException(nameof(httpResponse));
}
if (bot == null)
{
throw new ArgumentNullException(nameof(bot));
}
string body;
using (var sr = new StreamReader(httpRequest.Body))
{
body = await sr.ReadToEndAsync();
}
httpRequest.Headers.TryGetValue("Authorization", out var bearerHeadertoken);
string accessToken = bearerHeadertoken.FirstOrDefault()?.Replace("Bearer ", "");
var actionsSdkRequest = JsonConvert.DeserializeObject<ActionsSdkRequest>(body);
actionsSdkRequest.User.AccessToken = accessToken;
var actionsSdkRequestMapper = new ActionsSdkRequestMapper(_actionsSdkRequestMapperOptions, _logger);
var activity = actionsSdkRequestMapper.RequestToActivity(actionsSdkRequest);
var context = await CreateContextAndRunPipelineAsync(bot, cancellationToken, activity);
var actionsSdkResponse = actionsSdkRequestMapper.ActivityToResponse(await ProcessOutgoingActivitiesAsync(context.SentActivities, context), actionsSdkRequest);
var responseJson = JsonConvert.SerializeObject(actionsSdkResponse, JsonSerializerSettings);
httpResponse.ContentType = "application/json;charset=utf-8";
httpResponse.StatusCode = (int)HttpStatusCode.OK;
var responseData = Encoding.UTF8.GetBytes(responseJson);
await httpResponse.Body.WriteAsync(responseData, 0, responseData.Length, cancellationToken).ConfigureAwait(false);
}
public async Task ContinueConversationAsync(ConversationReference reference, BotCallbackHandler logic, CancellationToken cancellationToken)
{
if (reference == null)
{
throw new ArgumentNullException(nameof(reference));
}
if (logic == null)
{
throw new ArgumentNullException(nameof(logic));
}
var request = reference.GetContinuationActivity().ApplyConversationReference(reference, true);
using (var context = new TurnContext(this, request))
{
await RunPipelineAsync(context, logic, cancellationToken).ConfigureAwait(false);
}
}
public override Task<ResourceResponse> UpdateActivityAsync(ITurnContext turnContext, Activity activity, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public override Task DeleteActivityAsync(ITurnContext turnContext, ConversationReference reference, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public virtual Task<Activity> ProcessOutgoingActivitiesAsync(List<Activity> activities, ITurnContext turnContext)
{
return Task.FromResult(ActivityMappingHelper.MergeActivities(activities));
}
public override Task<ResourceResponse[]> SendActivitiesAsync(ITurnContext turnContext, Activity[] activities, CancellationToken cancellationToken)
{
return Task.FromResult(new ResourceResponse[0]);
}
private async Task<TurnContextEx> CreateContextAndRunPipelineAsync(IBot bot, CancellationToken cancellationToken, Activity activity)
{
var context = new TurnContextEx(this, activity);
await RunPipelineAsync(context, bot.OnTurnAsync, cancellationToken).ConfigureAwait(false);
return context;
}
}
}