Added samples for QnA and Luis Recognizer
This commit is contained in:
Родитель
6087843c99
Коммит
75ed54ba1d
|
@ -1,7 +1,7 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 15
|
||||
VisualStudioVersion = 15.0.27130.2027
|
||||
VisualStudioVersion = 15.0.27130.2036
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Bot.Builder", "libraries\Microsoft.Bot.Builder\Microsoft.Bot.Builder.csproj", "{74908F42-3842-49B4-9344-60BB791FDE32}"
|
||||
EndProject
|
||||
|
@ -45,6 +45,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Bot.Samples.Simpl
|
|||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Bot.Samples.EchoBot-AspNet461", "samples\Microsoft.Bot.Samples.EchoBot-AspNet461\Microsoft.Bot.Samples.EchoBot-AspNet461.csproj", "{AAC0F76C-4114-4D64-A99E-8BE4C43982F9}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Bot.Samples.Ai.Luis.Translator", "samples\Microsoft.Bot.Samples.Ai.Luis.Translator\Microsoft.Bot.Samples.Ai.Luis.Translator.csproj", "{CB3E5EC8-F666-4A13-AD9C-DB79E014AA43}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Bot.Samples.Ai.QnA.Translator", "samples\Microsoft.Bot.Samples.Ai.QnA.Translator\Microsoft.Bot.Samples.Ai.QnA.Translator.csproj", "{10A00767-431E-4C18-BE26-1FA2AE3D18E2}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
@ -123,6 +127,14 @@ Global
|
|||
{AAC0F76C-4114-4D64-A99E-8BE4C43982F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{AAC0F76C-4114-4D64-A99E-8BE4C43982F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{AAC0F76C-4114-4D64-A99E-8BE4C43982F9}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{CB3E5EC8-F666-4A13-AD9C-DB79E014AA43}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{CB3E5EC8-F666-4A13-AD9C-DB79E014AA43}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{CB3E5EC8-F666-4A13-AD9C-DB79E014AA43}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{CB3E5EC8-F666-4A13-AD9C-DB79E014AA43}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{10A00767-431E-4C18-BE26-1FA2AE3D18E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{10A00767-431E-4C18-BE26-1FA2AE3D18E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{10A00767-431E-4C18-BE26-1FA2AE3D18E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{10A00767-431E-4C18-BE26-1FA2AE3D18E2}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -146,6 +158,8 @@ Global
|
|||
{7DFD2037-A631-48B2-9704-3F9AC9363BF5} = {3ADFB27A-95FA-4330-B211-1D66A29A17AB}
|
||||
{BF6D1EA0-BA3B-4DB3-8247-5CEB18584E59} = {3ADFB27A-95FA-4330-B211-1D66A29A17AB}
|
||||
{AAC0F76C-4114-4D64-A99E-8BE4C43982F9} = {3ADFB27A-95FA-4330-B211-1D66A29A17AB}
|
||||
{CB3E5EC8-F666-4A13-AD9C-DB79E014AA43} = {3ADFB27A-95FA-4330-B211-1D66A29A17AB}
|
||||
{10A00767-431E-4C18-BE26-1FA2AE3D18E2} = {3ADFB27A-95FA-4330-B211-1D66A29A17AB}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {7173C9F3-A7F9-496E-9078-9156E35D6E16}
|
||||
|
|
|
@ -0,0 +1,194 @@
|
|||
|
||||
// Copyright(c) Microsoft Corporation.All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Bot.Builder;
|
||||
using Microsoft.Bot.Builder.Ai;
|
||||
using Microsoft.Bot.Builder.BotFramework;
|
||||
using Microsoft.Bot.Builder.Middleware;
|
||||
using Microsoft.Bot.Builder.Storage;
|
||||
using Microsoft.Bot.Schema;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Microsoft.Bot.Samples.Ai.Luis.Translator
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
public class MessagesController : Controller
|
||||
{
|
||||
BotFrameworkAdapter _adapter;
|
||||
|
||||
/// <summary>
|
||||
/// In this sample Bot, a new instance of the Bot is created by the controller
|
||||
/// on every incoming HTTP reques. The bot is constructed using the credentials
|
||||
/// found in the config file. Note that no credentials are needed if testing
|
||||
/// the bot locally using the emulator.
|
||||
/// </summary>
|
||||
///
|
||||
|
||||
private static readonly string[] _supportedLanguages = new string[] { "fr", "en" };
|
||||
private static readonly string[] _supportedLocales = new string[] { "fr-fr", "en-us" };
|
||||
private static string currentLanguage = null;
|
||||
private static string currentLocale = null;
|
||||
|
||||
public MessagesController(IConfiguration configuration)
|
||||
{
|
||||
var bot = new Builder.Bot(new BotFrameworkAdapter(configuration))
|
||||
.Use(new BotStateManager(new FileStorage(System.IO.Path.GetTempPath()))) //store user state in a temp directory
|
||||
.Use(new TranslationMiddleware(new string[] { "en" }, "xxxxxx", "templates", GetActiveLanguage, SetActiveLanguage))
|
||||
.Use(new LocaleConverterMiddleware(GetActiveLocale, SetActiveLocale, "en-us", new LocaleConverter()))
|
||||
// add QnA middleware
|
||||
.Use(new LuisRecognizerMiddleware("xxxxxx", "xxxxxx"));
|
||||
|
||||
|
||||
|
||||
// LUIS with correct baseUri format example
|
||||
//.Use(new LuisRecognizerMiddleware("xxxxxx", "xxxxxx", "https://xxxxxx.api.cognitive.microsoft.com/luis/v2.0/apps"))
|
||||
|
||||
bot.OnReceive(BotReceiveHandler);
|
||||
|
||||
_adapter = (BotFrameworkAdapter)bot.Adapter;
|
||||
}
|
||||
|
||||
private Task BotReceiveHandler(IBotContext context)
|
||||
{
|
||||
if (context.Request.Type == ActivityTypes.Message)
|
||||
{
|
||||
if (context.Responses.Count > 0)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
context.Reply($"the top intent was: {context.TopIntent.Name}");
|
||||
|
||||
foreach (var entity in context.TopIntent.Entities)
|
||||
{
|
||||
context.Reply($"entity: {entity.ValueAs<string>()}");
|
||||
}
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Post([FromBody]Activity activity)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _adapter.Receive(this.Request.Headers["Authorization"].FirstOrDefault(), activity);
|
||||
return this.Ok();
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
return this.Unauthorized();
|
||||
}
|
||||
}
|
||||
|
||||
//Change language and locale
|
||||
[HttpGet]
|
||||
public IActionResult Get(string lang, string locale)
|
||||
{
|
||||
currentLanguage = lang;
|
||||
currentLocale = locale;
|
||||
return new ObjectResult("Success!");
|
||||
}
|
||||
|
||||
private void SetLanguage(IBotContext context, string language) => context.State.User[@"Microsoft.API.translateTo"] = language;
|
||||
private void SetLocale(IBotContext context, string locale) => context.State.User[@"LocaleConverterMiddleware.fromLocale"] = locale;
|
||||
|
||||
protected bool IsSupportedLanguage(string language) => _supportedLanguages.Contains(language);
|
||||
protected async Task<bool> SetActiveLanguage(IBotContext context)
|
||||
{
|
||||
bool changeLang = false;//logic implemented by developper to make a signal for language changing
|
||||
//use a specific message from user to change language
|
||||
var messageActivity = context.Request.AsMessageActivity();
|
||||
if (messageActivity.Text.ToLower().StartsWith("set my language to"))
|
||||
{
|
||||
changeLang = true;
|
||||
}
|
||||
if (changeLang)
|
||||
{
|
||||
var newLang = messageActivity.Text.ToLower().Replace("set my language to", "").Trim();
|
||||
if (!string.IsNullOrWhiteSpace(newLang)
|
||||
&& IsSupportedLanguage(newLang))
|
||||
{
|
||||
SetLanguage(context, newLang);
|
||||
context.Reply($@"Changing your language to {newLang}");
|
||||
}
|
||||
else
|
||||
{
|
||||
context.Reply($@"{newLang} is not a supported language.");
|
||||
}
|
||||
//intercepts message
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
protected string GetActiveLanguage(IBotContext context)
|
||||
{
|
||||
if (currentLanguage != null)
|
||||
{
|
||||
//user has specified a different language so update the bot state
|
||||
if (currentLanguage != (string)context.State.User[@"Microsoft.API.translateTo"])
|
||||
{
|
||||
SetLanguage(context, currentLanguage);
|
||||
}
|
||||
}
|
||||
if (context.Request.Type == ActivityTypes.Message
|
||||
&& context.State.User.ContainsKey(@"Microsoft.API.translateTo"))
|
||||
{
|
||||
return (string)context.State.User[@"Microsoft.API.translateTo"];
|
||||
}
|
||||
|
||||
return "en";
|
||||
}
|
||||
protected async Task<bool> SetActiveLocale(IBotContext context)
|
||||
{
|
||||
bool changeLocale = false;//logic implemented by developper to make a signal for language changing
|
||||
//use a specific message from user to change language
|
||||
var messageActivity = context.Request.AsMessageActivity();
|
||||
if (messageActivity.Text.ToLower().StartsWith("set my locale to"))
|
||||
{
|
||||
changeLocale = true;
|
||||
}
|
||||
if (changeLocale)
|
||||
{
|
||||
var newLocale = messageActivity.Text.ToLower().Replace("set my locale to", "").Trim(); //extracted by the user using user state
|
||||
if (!string.IsNullOrWhiteSpace(newLocale)
|
||||
&& IsSupportedLanguage(newLocale))
|
||||
{
|
||||
SetLocale(context, newLocale);
|
||||
context.Reply($@"Changing your language to {newLocale}");
|
||||
}
|
||||
else
|
||||
{
|
||||
context.Reply($@"{newLocale} is not a supported locale.");
|
||||
}
|
||||
//intercepts message
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
protected string GetActiveLocale(IBotContext context)
|
||||
{
|
||||
if (currentLocale != null)
|
||||
{
|
||||
//the user has specified a different locale so update the bot state
|
||||
if (currentLocale != (string)context.State.User[@"LocaleConverterMiddleware.fromLocale"])
|
||||
{
|
||||
SetLocale(context, currentLocale);
|
||||
}
|
||||
}
|
||||
if (context.Request.Type == ActivityTypes.Message
|
||||
&& context.State.User.ContainsKey(@"LocaleConverterMiddleware.fromLocale"))
|
||||
{
|
||||
return (string)context.State.User[@"LocaleConverterMiddleware.fromLocale"];
|
||||
}
|
||||
|
||||
return "en-us";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.2" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\libraries\Microsoft.Bot.Builder.Ai\Microsoft.Bot.Builder.Ai.csproj" />
|
||||
<ProjectReference Include="..\..\libraries\Microsoft.Bot.Builder.BotFramework\Microsoft.Bot.Builder.BotFramework.csproj" />
|
||||
<ProjectReference Include="..\..\libraries\Microsoft.Bot.Builder\Microsoft.Bot.Builder.csproj" />
|
||||
<ProjectReference Include="..\..\libraries\Microsoft.Bot.Schema\Microsoft.Bot.Schema.csproj" />
|
||||
<ProjectReference Include="..\libraries\Microsoft.Bot.Builder.Ai\Microsoft.Bot.Builder.Ai.csproj" />
|
||||
<ProjectReference Include="..\libraries\Microsoft.Bot.Builder.BotFramework\Microsoft.Bot.Builder.BotFramework.csproj" />
|
||||
<ProjectReference Include="..\libraries\Microsoft.Bot.Builder\Microsoft.Bot.Builder.csproj" />
|
||||
<ProjectReference Include="..\libraries\Microsoft.Bot.Schema\Microsoft.Bot.Schema.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Update="templates\fr.template">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -0,0 +1,25 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Microsoft.Bot.Samples.Ai.Luis.Translator
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
BuildWebHost(args).Run();
|
||||
}
|
||||
|
||||
public static IWebHost BuildWebHost(string[] args) =>
|
||||
WebHost.CreateDefaultBuilder(args)
|
||||
.UseStartup<Startup>()
|
||||
.Build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Microsoft.Bot.Samples.Ai.Luis.Translator
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IHostingEnvironment env)
|
||||
{
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(env.ContentRootPath)
|
||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
|
||||
.AddEnvironmentVariables();
|
||||
Configuration = builder.Build();
|
||||
}
|
||||
|
||||
public IConfigurationRoot Configuration { get; }
|
||||
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton(_ => Configuration);
|
||||
services.AddMvc();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
app.UseDefaultFiles();
|
||||
app.UseStaticFiles();
|
||||
app.UseMvc();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"Logging": {
|
||||
"IncludeScopes": false,
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"System": "Information",
|
||||
"Microsoft": "Information"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"Logging": {
|
||||
"IncludeScopes": false,
|
||||
"LogLevel": {
|
||||
"Default": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
// Configure bundling and minification for the project.
|
||||
// More info at https://go.microsoft.com/fwlink/?LinkId=808241
|
||||
[
|
||||
{
|
||||
"outputFileName": "wwwroot/css/site.min.css",
|
||||
// An array of relative input file paths. Globbing patterns supported
|
||||
"inputFiles": [
|
||||
"wwwroot/css/site.css"
|
||||
]
|
||||
},
|
||||
{
|
||||
"outputFileName": "wwwroot/js/site.min.js",
|
||||
"inputFiles": [
|
||||
"wwwroot/js/site.js"
|
||||
],
|
||||
// Optionally specify minification options
|
||||
"minify": {
|
||||
"enabled": true,
|
||||
"renameLocals": true
|
||||
},
|
||||
// Optionally generate .map file
|
||||
"sourceMap": false
|
||||
}
|
||||
]
|
|
@ -0,0 +1,3 @@
|
|||
James
|
||||
Chris
|
||||
Je m'appelle (.*)
|
|
@ -0,0 +1,26 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Sample Bot LUIS Recognizer</title>
|
||||
</head>
|
||||
<body>
|
||||
Sample Bot LUIS Recognizer
|
||||
<form action="/api/messages" method="get">
|
||||
<img src="https://png.icons8.com/ios/1600/language.png" height="30" />
|
||||
<select name="lang">
|
||||
<option value="en">English</option>
|
||||
<option value="fr">Francais</option>
|
||||
</select>
|
||||
<br />
|
||||
<img src="https://image.freepik.com/icones-gratuites/symbole-de-referencement-locale_318-50438.jpg" height="30" />
|
||||
<select name="locale">
|
||||
<option value="en-us">USA</option>
|
||||
<option value="fr-fr">France</option>
|
||||
</select>
|
||||
<br />
|
||||
<br />
|
||||
<button type="submit"><img src="https://png.icons8.com/material/1600/checked.png" height="30" /></button>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,186 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Bot.Builder;
|
||||
using Microsoft.Bot.Builder.Ai;
|
||||
using Microsoft.Bot.Builder.BotFramework;
|
||||
using Microsoft.Bot.Schema;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Bot.Builder.Storage;
|
||||
|
||||
namespace Microsoft.Bot.Samples.Ai.QnA.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
public class MessagesController : Controller
|
||||
{
|
||||
|
||||
private static readonly HttpClient _httpClient = new HttpClient();
|
||||
BotFrameworkAdapter _adapter;
|
||||
|
||||
//supported langauges and locales
|
||||
private static readonly string[] _supportedLanguages = new string[] { "en", "fr" };
|
||||
private static readonly string[] _supportedLocales = new string[] { "fr-fr", "en-us" };
|
||||
private static string currentLanguage = null;
|
||||
private static string currentLocale = null;
|
||||
|
||||
|
||||
public MessagesController(IConfiguration configuration)
|
||||
{
|
||||
var qnaMiddlewareOptions = new QnAMakerMiddlewareOptions
|
||||
{
|
||||
// add subscription key and knowledge base id
|
||||
SubscriptionKey = "xxxxxx",
|
||||
KnowledgeBaseId = "xxxxxx"
|
||||
};
|
||||
|
||||
|
||||
var bot = new Builder.Bot(new BotFrameworkAdapter(configuration))
|
||||
.Use(new BotStateManager(new FileStorage(System.IO.Path.GetTempPath()))) //store user state in a temp directory
|
||||
.Use(new TranslationMiddleware(new string[] { "en" }, "xxxxxx", "templates", GetActiveLanguage, SetActiveLanguage))
|
||||
.Use(new LocaleConverterMiddleware(GetActiveLocale, SetActiveLocale, "en-us", new LocaleConverter()))
|
||||
//LocaleConverter and Translation middleware use default values for source language and from locale
|
||||
// add QnA middleware
|
||||
.Use(new QnAMakerMiddleware(qnaMiddlewareOptions, _httpClient));
|
||||
|
||||
bot.OnReceive(BotReceiveHandler);
|
||||
|
||||
_adapter = (BotFrameworkAdapter)bot.Adapter;
|
||||
}
|
||||
|
||||
private Task BotReceiveHandler(IBotContext context)
|
||||
{
|
||||
if (context.Request.Type == ActivityTypes.Message && context.Responses.Count == 0)
|
||||
{
|
||||
// add app logic when QnA Maker doesn't find an answer
|
||||
context.Reply("No good match found in the KB.");
|
||||
}
|
||||
//context.Reply(context.Request.AsMessageActivity().Text);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Post([FromBody]Activity activity)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _adapter.Receive(this.Request.Headers["Authorization"].FirstOrDefault(), activity);
|
||||
return this.Ok();
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
return this.Unauthorized();
|
||||
}
|
||||
}
|
||||
|
||||
//Change language and locale
|
||||
[HttpGet]
|
||||
public IActionResult Get(string lang, string locale)
|
||||
{
|
||||
currentLanguage = lang;
|
||||
currentLocale = locale;
|
||||
return new ObjectResult("Success!");
|
||||
}
|
||||
|
||||
private void SetLanguage(IBotContext context, string language) => context.State.User[@"Microsoft.API.translateTo"] = language;
|
||||
private void SetLocale(IBotContext context, string locale) => context.State.User[@"LocaleConverterMiddleware.fromLocale"] = locale;
|
||||
|
||||
protected bool IsSupportedLanguage(string language) => _supportedLanguages.Contains(language);
|
||||
protected async Task<bool> SetActiveLanguage(IBotContext context)
|
||||
{
|
||||
bool changeLang = false;//logic implemented by developper to make a signal for language changing
|
||||
//use a specific message from user to change language
|
||||
var messageActivity = context.Request.AsMessageActivity();
|
||||
if (messageActivity.Text.ToLower().StartsWith("set my language to"))
|
||||
{
|
||||
changeLang = true;
|
||||
}
|
||||
if (changeLang)
|
||||
{
|
||||
var newLang = messageActivity.Text.ToLower().Replace("set my language to", "").Trim();
|
||||
if (!string.IsNullOrWhiteSpace(newLang)
|
||||
&& IsSupportedLanguage(newLang))
|
||||
{
|
||||
SetLanguage(context, newLang);
|
||||
context.Reply($@"Changing your language to {newLang}");
|
||||
}
|
||||
else
|
||||
{
|
||||
context.Reply($@"{newLang} is not a supported language.");
|
||||
}
|
||||
//intercepts message
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
protected string GetActiveLanguage(IBotContext context)
|
||||
{
|
||||
if (currentLanguage != null)
|
||||
{
|
||||
//user has specified a different language so update the bot state
|
||||
if (currentLanguage != (string)context.State.User[@"Microsoft.API.translateTo"])
|
||||
{
|
||||
SetLanguage(context, currentLanguage);
|
||||
}
|
||||
}
|
||||
if (context.Request.Type == ActivityTypes.Message
|
||||
&& context.State.User.ContainsKey(@"Microsoft.API.translateTo"))
|
||||
{
|
||||
return (string)context.State.User[@"Microsoft.API.translateTo"];
|
||||
}
|
||||
|
||||
return "en";
|
||||
}
|
||||
protected async Task<bool> SetActiveLocale(IBotContext context)
|
||||
{
|
||||
bool changeLocale = false;//logic implemented by developper to make a signal for language changing
|
||||
//use a specific message from user to change language
|
||||
var messageActivity = context.Request.AsMessageActivity();
|
||||
if (messageActivity.Text.ToLower().StartsWith("set my locale to"))
|
||||
{
|
||||
changeLocale = true;
|
||||
}
|
||||
if (changeLocale)
|
||||
{
|
||||
var newLocale = messageActivity.Text.ToLower().Replace("set my locale to", "").Trim(); //extracted by the user using user state
|
||||
if (!string.IsNullOrWhiteSpace(newLocale)
|
||||
&& IsSupportedLanguage(newLocale))
|
||||
{
|
||||
SetLocale(context, newLocale);
|
||||
context.Reply($@"Changing your language to {newLocale}");
|
||||
}
|
||||
else
|
||||
{
|
||||
context.Reply($@"{newLocale} is not a supported locale.");
|
||||
}
|
||||
//intercepts message
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
protected string GetActiveLocale(IBotContext context)
|
||||
{
|
||||
if (currentLocale != null)
|
||||
{
|
||||
//the user has specified a different locale so update the bot state
|
||||
if (currentLocale != (string)context.State.User[@"LocaleConverterMiddleware.fromLocale"])
|
||||
{
|
||||
SetLocale(context, currentLocale);
|
||||
}
|
||||
}
|
||||
if (context.Request.Type == ActivityTypes.Message
|
||||
&& context.State.User.ContainsKey(@"LocaleConverterMiddleware.fromLocale"))
|
||||
{
|
||||
return (string)context.State.User[@"LocaleConverterMiddleware.fromLocale"];
|
||||
}
|
||||
|
||||
return "en-us";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.2" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\libraries\Microsoft.Bot.Builder.Ai\Microsoft.Bot.Builder.Ai.csproj" />
|
||||
<ProjectReference Include="..\..\libraries\Microsoft.Bot.Builder.BotFramework\Microsoft.Bot.Builder.BotFramework.csproj" />
|
||||
<ProjectReference Include="..\..\libraries\Microsoft.Bot.Builder\Microsoft.Bot.Builder.csproj" />
|
||||
<ProjectReference Include="..\..\libraries\Microsoft.Bot.Schema\Microsoft.Bot.Schema.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Update="templates\fr.template">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -0,0 +1,25 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Microsoft.Bot.Samples.Ai.QnA.Translator
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
BuildWebHost(args).Run();
|
||||
}
|
||||
|
||||
public static IWebHost BuildWebHost(string[] args) =>
|
||||
WebHost.CreateDefaultBuilder(args)
|
||||
.UseStartup<Startup>()
|
||||
.Build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Microsoft.Bot.Samples.Ai.QnA.Translator
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IHostingEnvironment env)
|
||||
{
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(env.ContentRootPath)
|
||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
|
||||
.AddEnvironmentVariables();
|
||||
Configuration = builder.Build();
|
||||
}
|
||||
|
||||
public IConfigurationRoot Configuration { get; }
|
||||
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton(_ => Configuration);
|
||||
services.AddMvc();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
app.UseDefaultFiles();
|
||||
app.UseStaticFiles();
|
||||
app.UseMvc();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"Logging": {
|
||||
"IncludeScopes": false,
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"System": "Information",
|
||||
"Microsoft": "Information"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"Logging": {
|
||||
"IncludeScopes": false,
|
||||
"LogLevel": {
|
||||
"Default": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
// Configure bundling and minification for the project.
|
||||
// More info at https://go.microsoft.com/fwlink/?LinkId=808241
|
||||
[
|
||||
{
|
||||
"outputFileName": "wwwroot/css/site.min.css",
|
||||
// An array of relative input file paths. Globbing patterns supported
|
||||
"inputFiles": [
|
||||
"wwwroot/css/site.css"
|
||||
]
|
||||
},
|
||||
{
|
||||
"outputFileName": "wwwroot/js/site.min.js",
|
||||
"inputFiles": [
|
||||
"wwwroot/js/site.js"
|
||||
],
|
||||
// Optionally specify minification options
|
||||
"minify": {
|
||||
"enabled": true,
|
||||
"renameLocals": true
|
||||
},
|
||||
// Optionally generate .map file
|
||||
"sourceMap": false
|
||||
}
|
||||
]
|
|
@ -0,0 +1,3 @@
|
|||
James
|
||||
Chris
|
||||
Je m'appelle (.*)
|
|
@ -0,0 +1,26 @@
|
|||
<!DOCTYPE html>
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title> QnAMaker Bot Sample </title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>QnAMaker Bot Sample</h1>
|
||||
<img src="https://docs.microsoft.com/en-us/azure/media/index/bot-service.svg" alt="Bot" height="128" width="128">
|
||||
<form action="/api/messages" method="get">
|
||||
<img src="https://png.icons8.com/ios/1600/language.png" height="30" />
|
||||
<select name="lang">
|
||||
<option value="en">English</option>
|
||||
<option value="fr">Francais</option>
|
||||
</select>
|
||||
<br />
|
||||
<img src="https://image.freepik.com/icones-gratuites/symbole-de-referencement-locale_318-50438.jpg" height="30" />
|
||||
<select name="locale">
|
||||
<option value="en-us">USA</option>
|
||||
<option value="fr-fr">France</option>
|
||||
</select>
|
||||
<br />
|
||||
<button type="submit">Click</button>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
Загрузка…
Ссылка в новой задаче