バーチャルアシスタント。
NS#
BotBuilder.Community.Adapters.AlexaはVAでは機能しないようです。
こちらの手順に従ってください-https : //github.com/BotBuilderCommunity/botbuilder-community-dotnet/tree/develop/libraries/Bot.Builder.Community.Adapters.Alexa
1)Startup.csで、次のNuGetパッケージを追加しました。
Bot.Builder.Community.Adapters.Alexa;
Bot.Builder.Community.Adapters.Alexa.Integration.AspNet.Core;
Bot.Builder.Community.Adapters.Alexa.Middleware;
このコードの前
// Configure bot
services.AddTransient<IBot, DialogBot<MainDialog>>();
追加した:
// Registering the AlexaHttpAdapter
services.AddSingleton<IAlexaHttpAdapter>((sp) =>
{
var alexaHttpAdapter = new AlexaHttpAdapter(validateRequests: true)
{
OnTurnError = async (context, exception) =>
{
await context.SendActivityAsync("Sorry, something went wrong. Please try again later." + context + " Exception:" + exception);
},
ShouldEndSessionByDefault = true,
ConvertBotBuilderCardsToAlexaCards = false,
};
alexaHttpAdapter.Use(new AlexaIntentRequestToMessageActivityMiddleware());
return alexaHttpAdapter;
});
services.AddSingleton<AlexaIntentRequestToMessageActivityMiddleware>();
2)Alexaコントローラーを作成しました:
using Bot.Builder.Community.Adapters.Alexa.Integration.AspNet.Core;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace GuideDogs_VA.Controllers
{
[Route("api/skillrequests")]
[ApiController]
public class AlexaBotController : ControllerBase
{
private readonly IAlexaHttpAdapter _adapter;
private readonly IBot _bot;
public AlexaBotController(IAlexaHttpAdapter adapter, IBot bot)
{
_adapter = adapter;
_bot = bot;
}
[HttpPost]
public async Task PostAsync()
{
await _adapter.ProcessAsync(Request, Response, _bot);
}
}
}
3)次のJSONを使用してAlexaスキルを作成しました。
{
"interactionModel": {
"languageModel": {
"invocationName": "guide dogs",
"intents": [
{
"name": "GetUserIntent",
"slots": [
{
"name": "phrase",
"type": "phrase"
}
],
"samples": [
"{phrase}"
]
},
{
"name": "AMAZON.FallbackIntent",
"samples": []
},
{
"name": "AMAZON.NavigateHomeIntent",
"samples": []
},
{
"name": "AMAZON.CancelIntent",
"samples": []
},
{
"name": "AMAZON.HelpIntent",
"samples": []
},
{
"name": "AMAZON.StopIntent",
"samples": []
}
],
"types": [
{
"name": "phrase",
"values": [
{
"name": {
"value": "hi there Alexa"
}
},
{
"name": {
"value": "you are just going to repeat what I said aren't you"
}
},
{
"name": {
"value": "what colour is the sky?"
}
}
]
}
]
}
}
}
Alexaの開発者コンソールまたはAlexaデバイスを介してAlexaスキルをテストすると、応答しますが、残念ながら、アクティビティタイプがメッセージタイプであってもOnEventSync()を呼び出すことになります。
MainDialog.csファイルからスローされたSystem.NullReferenceException:
VAからAlexaへの応答(Startup.cs context.SendActi vityAsyncから生成された応答
{
"body": {
"version": "1.0",
"response": {
"outputSpeech": {
"type": "PlainText",
"text": "Sorry, something went wrong. Please try again later.Microsoft.Bot.Builder.TurnContext Exception:System.NullReferenceException: Object reference not set to an instance of an object.\r\n at GuideDogs_VA.Dialogs.MainDialog.OnEventAsync(DialogContext dc, CancellationToken cancellationToken) in C:\\Users\\hussel\\source\\repos\\GuideDogs-VA\\GuideDogs_VA\\Dialogs\\MainDialog.cs:line 204\r\n at Microsoft.Bot.Builder.Solutions.Dialogs.RouterDialog.OnContinueDialogAsync(DialogContext innerDc, CancellationToken cancellationToken)\r\n at Microsoft.Bot.Builder.Dialogs.ComponentDialog.BeginDialogAsync(DialogContext outerDc, Object options, CancellationToken cancellationToken)\r\n at Microsoft.Bot.Builder.Dialogs.DialogContext.BeginDialogAsync(String dialogId, Object options, CancellationToken cancellationToken)\r\n at Microsoft.Bot.Builder.Dialogs.DialogExtensions.RunAsync(Dialog dialog, ITurnContext turnContext, IStatePropertyAccessor`1 accessor, CancellationToken cancellationToken)\r\n at GuideDogs_VA.Bots.DialogBot`1.OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken) in C:\\Users\\hussel\\source\\repos\\GuideDogs-VA\\GuideDogs_VA\\Bots\\DialogBot.cs:line 39\r\n at Bot.Builder.Community.Adapters.Alexa.Middleware.AlexaIntentRequestToMessageActivityMiddleware.OnTurnAsync(ITurnContext context, NextDelegate next, CancellationToken cancellationToken)\r\n at Microsoft.Bot.Builder.MiddlewareSet.ReceiveActivityWithStatusAsync(ITurnContext turnContext, BotCallbackHandler callback, CancellationToken cancellationToken)\r\n at Microsoft.Bot.Builder.BotAdapter.RunPipelineAsync(ITurnContext turnContext, BotCallbackHandler callback, CancellationToken cancellationToken)"
},
"directives": [],
"shouldEndSession": true,
"type": "_DEFAULT_RESPONSE"
},
"sessionAttributes": {}
}
}
これはおそらく、デフォルトのRouterDialog実装が原因です。 試すオプションの1つは、MainDialogのContinueDialogAsyncメソッドを必要な動作でオーバーライドすることです。
@samaea問題を再現しました。 @ lauren-millsは正しかった。これは、RouterDialogがアクティビティの値を探し、それを通常のメッセージアクティビティではなく、イベントとして扱うためです。
回避策として、次のオーバーライドをMainDialogに追加できます。 唯一の変更点は、チャンネルIDがAlexaであるかどうかを確認していることです。
この変更を追加した後、Alexaを介してVAから正常に応答を得ることができます。 それがあなたの問題を解決することを願っています。
protected override async Task<DialogTurnResult> OnContinueDialogAsync(DialogContext innerDc, CancellationToken cancellationToken = default)
{
var activity = innerDc.Context.Activity;
if (activity.IsStartActivity())
{
await OnStartAsync(innerDc);
}
switch (activity.Type)
{
case ActivityTypes.Message:
{
if (activity.Value != null && activity.ChannelId != "alexa")
{
await OnEventAsync(innerDc);
}
else if (!string.IsNullOrEmpty(activity.Text))
{
var result = await innerDc.ContinueDialogAsync();
switch (result.Status)
{
case DialogTurnStatus.Empty:
{
await RouteAsync(innerDc);
break;
}
case DialogTurnStatus.Complete:
{
await CompleteAsync(innerDc);
// End active dialog
await innerDc.EndDialogAsync();
break;
}
default:
{
break;
}
}
}
break;
}
case ActivityTypes.Event:
{
await OnEventAsync(innerDc);
break;
}
default:
{
await OnSystemMessageAsync(innerDc);
break;
}
}
return EndOfTurn;
}
ありがとう@garypretty! 次のリリースでは、RouterDialogをリファクタリングしてSDKとの整合性を高めるための未解決の作業項目があるため、このフィードバックをその作業にも取り入れることができます。
最も参考になるコメント
@samaea問題を再現しました。 @ lauren-millsは正しかった。これは、RouterDialogがアクティビティの値を探し、それを通常のメッセージアクティビティではなく、イベントとして扱うためです。
回避策として、次のオーバーライドをMainDialogに追加できます。 唯一の変更点は、チャンネルIDがAlexaであるかどうかを確認していることです。
この変更を追加した後、Alexaを介してVAから正常に応答を得ることができます。 それがあなたの問題を解決することを願っています。