forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiLingualBot.cs
More file actions
114 lines (100 loc) · 5.42 KB
/
Copy pathMultiLingualBot.cs
File metadata and controls
114 lines (100 loc) · 5.42 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Schema;
using Microsoft.BotBuilderSamples.Translation;
namespace Microsoft.BotBuilderSamples
{
/// <summary>
/// Represents a bot that processes incoming activities.
/// For each user interaction, an instance of this class is created and the OnTurnAsync method is called.
/// This is a Transient lifetime service. Transient lifetime services are created
/// each time they're requested. For each Activity received, a new instance of this
/// class is created. Objects that are expensive to construct, or have a lifetime
/// beyond the single turn, should be carefully managed.
/// For example, the <see cref="MemoryStorage"/> object and associated
/// <see cref="IStatePropertyAccessor{T}"/> object are created with a singleton lifetime.
/// </summary>
/// <seealso cref="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1"/>
public class MultiLingualBot : IBot
{
private const string English = "en";
private const string Spanish = "es";
private readonly MultiLingualBotAccessors _accessors;
/// <summary>
/// Initializes a new instance of the <see cref="MultiLingualBot"/> class.
/// </summary>
/// <param name="accessors">Bot State Accessors.</param>
public MultiLingualBot(MultiLingualBotAccessors accessors)
{
_accessors = accessors ?? throw new ArgumentNullException(nameof(accessors));
}
private DialogSet Dialogs { get; set; }
/// <summary>
/// Run every turn of the conversation. Handles orchestration of messages.
/// </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>
/// <seealso cref="BotStateSet"/>
/// <seealso cref="ConversationState"/>
/// <seealso cref="IMiddleware"/>
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken)
{
if (turnContext == null)
{
throw new ArgumentNullException(nameof(turnContext));
}
if (turnContext.Activity.Type == ActivityTypes.Message)
{
string userLanguage = await _accessors.LanguagePreference.GetAsync(turnContext, () => TranslationSettings.DefaultLanguage) ?? TranslationSettings.DefaultLanguage;
bool translate = userLanguage != TranslationSettings.DefaultLanguage;
if (IsLanguageChangeRequested(turnContext.Activity.Text))
{
// If the user requested a language change through the suggested actions with values "es" or "en",
// simply change the user's language preference in the user state.
// The translation middleware will catch this setting and translate both ways to the user's
// selected language.
// If Spanish was selected by the user, the reply below will actually be shown in spanish to the user.
await _accessors.LanguagePreference.SetAsync(turnContext, turnContext.Activity.Text);
var reply = turnContext.Activity.CreateReply($"Your current language code is: {turnContext.Activity.Text}");
await turnContext.SendActivityAsync(reply, cancellationToken);
// Save the user profile updates into the user state.
await _accessors.UserState.SaveChangesAsync(turnContext, false, cancellationToken);
}
else
{
// Show the user the possible options for language. If the user chooses a different language
// than the default, then the translation middleware will pick it up from the user state and
// translate messages both ways, i.e. user to bot and bot to user.
var reply = turnContext.Activity.CreateReply("Choose your language:");
reply.SuggestedActions = new SuggestedActions()
{
Actions = new List<CardAction>()
{
new CardAction() { Title = "Español", Type = ActionTypes.PostBack, Value = Spanish },
new CardAction() { Title = "English", Type = ActionTypes.PostBack, Value = English },
},
};
await turnContext.SendActivityAsync(reply);
}
}
}
private static bool IsLanguageChangeRequested(string utterance)
{
if (string.IsNullOrEmpty(utterance))
{
return false;
}
utterance = utterance.ToLower().Trim();
return utterance == Spanish || utterance == English;
}
}
}