forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBotControllerBase.cs
More file actions
92 lines (82 loc) · 3.82 KB
/
Copy pathBotControllerBase.cs
File metadata and controls
92 lines (82 loc) · 3.82 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Bot.Schema;
using Microsoft.Rest.Serialization;
using Newtonsoft.Json;
namespace Microsoft.BotBuilderSamples
{
/// <summary>
/// A base class for Controllers that act as bots. To use this add a Controller to the Visual Studio project
/// as usual and then just edit it to swap out the base Controller class for this class.
/// </summary>
public abstract class BotControllerBase : Controller
{
public static readonly JsonSerializer BotMessageSerializer = JsonSerializer.Create(new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.Indented,
DateFormatHandling = DateFormatHandling.IsoDateFormat,
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
ContractResolver = new ReadOnlyJsonContractResolver(),
Converters = new List<JsonConverter> { new Iso8601TimeSpanConverter() },
});
[HttpPost]
public async Task PostAsync()
{
var activity = default(Activity);
using (var bodyReader = new JsonTextReader(new StreamReader(Request.Body, Encoding.UTF8)))
{
activity = BotMessageSerializer.Deserialize<Activity>(bodyReader);
}
var botFrameworkAdapter = CreateAdapter();
var invokeResponse = await botFrameworkAdapter.ProcessActivityAsync(
Request.Headers["Authorization"],
activity,
OnTurnAsync,
default(CancellationToken));
if (invokeResponse == null)
{
Response.StatusCode = (int)HttpStatusCode.OK;
}
else
{
Response.ContentType = "application/json";
Response.StatusCode = invokeResponse.Status;
using (var writer = new StreamWriter(Response.Body))
{
using (var jsonWriter = new JsonTextWriter(writer))
{
BotMessageSerializer.Serialize(jsonWriter, invokeResponse.Body);
}
}
}
}
/// <summary>
/// Override CreateAdapter to create your own custom adapter or set properties on a BotFrameworkAdapter instance.
/// </summary>
/// <returns>An adapter that will be used for the current request.</returns>
protected virtual IAdapterIntegration CreateAdapter() => new BotFrameworkAdapter(new SimpleCredentialProvider());
/// <summary>
/// Override OnTurnAsync to implement the bot specific logic. This is exactly the same as implementing
/// the IBot OnTurnAsync except here we can have multiple independent bots in the same project in addition
/// to including arbitray other MVC Controllers.
/// </summary>
/// <param name="turnContext">A <see cref="ITurnContext"/> containing all the data needed
/// for processing this conversation turn. </param>
/// <param name="cancellationToken">(Optional) A <see cref="CancellationToken"/> that can be used by other objects
/// or threads to receive notice of cancellation.</param>
/// <returns>A <see cref="Task"/> that represents the work queued to execute.</returns>
protected abstract Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken);
}
}