前回までテンプレートで作成されたソリューションを見てきました。今回は新しくメッセージタイプのアクションを追加してみます。
追加するアクション
現在時刻を返すアクションを追加します。
LUIS の更新
メッセージタイプは LUIS で判定を行うため、まずは LUIS の更新から行います。
1. Deployment\Resources\LU\en-us\HelloSkill.lu を開き、以下を追加。
## GetTime
- what time is it now?
- tell me current time
- show me current time
- Do you know what time it is now?
- get current time
2. PowerShell よりモデルを更新。
cd <プロジェクトフォルダ>
.\Deployment\Scripts\update_cognitive_models.ps1
5. Services\HelloSkillLuis.cs に新しいインテントがあることを確認。
Response の追加
次にメッセージを追加します。本来は全言語追加しますが、ここでは英語のみとします。
1. Responses フォルダに GetTimeResponses.lg を追加。
> ------------ Activity templates ------------
# CurrentTimeMessage
[Activity
Text = @{CurrentTimeText()}
InputHint = acceptingInput
]
> ------------ Text templates ------------
# CurrentTimeText
- It's @{CurrentTime}
2. 言語ファイルはサポートする分必要なため、ファイルをコピーして拡張子をそれぞれの言語に設定。
3. Startup.cs でテンプレートに追加。
var templateFiles = new List<string>() { "MainResponses", "SampleResponses", "GetTimeResponses" };
4. Visual Studio よりそれぞれのファイルのプロパティで「Copy if newer」を選択。
マニフェストの更新
dispatchModels\intents に GetTime を追加するだけです。
GetTimeDialog の追加
1. Dialogs フォルダに GetTimeDialog.cs を追加。
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
namespace HelloSkill.Dialogs
{
public class GetTimeDialog : SkillDialogBase
{
public GetTimeDialog(
IServiceProvider serviceProvider,
IBotTelemetryClient telemetryClient)
: base(nameof(GetTimeDialog), serviceProvider, telemetryClient)
{
var getTimeWaterfall = new WaterfallStep[]
{
GetCurrentTime,
End,
};
AddDialog(new WaterfallDialog(nameof(GetTimeDialog), getTimeWaterfall));
InitialDialogId = nameof(GetTimeDialog);
}
private async Task<DialogTurnResult> GetCurrentTime(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
dynamic currentTime = new { CurrentTime = DateTime.Now.ToString() };
var response = TemplateEngine.GenerateActivityForLocale("CurrentTimeMessage", currentTime);
await stepContext.Context.SendActivityAsync(response);
return await stepContext.NextAsync();
}
private Task<DialogTurnResult> End(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
return stepContext.EndDialogAsync();
}
}
}
2. Startup.cs にダイアログを追加。
// Register dialogs
services.AddTransient<SampleDialog>();
services.AddTransient<SampleAction>();
services.AddTransient<GetTimeDialog>();
services.AddTransient<MainDialog>();
3. MainDialog.cs で MainDialog クラスプロパティにGetTimeDialog を追加。
private GetTimeDialog _getTimeDialog;
4. コンストラクタでインスタンスを取得。
_getTimeDialog = serviceProvider.GetService<GetTimeDialog>();
AddDialog(_getTimeDialog);
5. RouteStepAsync メソッドで分岐を追加。
switch (intent)
{
case HelloSkillLuis.Intent.Sample:
{
return await stepContext.BeginDialogAsync(_sampleDialog.Id);
}
case HelloSkillLuis.Intent.GetTime:
{
return await stepContext.BeginDialogAsync(_getTimeDialog.Id);
}
case HelloSkillLuis.Intent.None:
default:
{
// intent was identified but not yet implemented
await stepContext.Context.SendActivityAsync(_templateEngine.GenerateActivityForLocale("UnsupportedMessage"));
return await stepContext.NextAsync();
}
}
動作確認
実装が終わったため、早速テストしてみます。
ローカルでのテスト
実装が終わったのでまず単体でテストします。F5 を押下してデバッグ実行。エミュレーターで動作を確認。
公開してテスト
今回はアシスタントボット経由でのテストを行います。
1. publish.ps1 スクリプトでスキルを公開。
.\Deployment\Scripts\publish.ps1 -name kenakamuhelloskill-om7pvdw -resourceGroup kenakamuhelloskill
3. アシスタントボット側も botskills update でスキルを更新。
botskills update --remoteManifest "https://kenakamuhelloskill-om7pvdw.azurewebsites.net/manifest/manifest-1.1.json" --cs --luisFolder "<HelloSkill へのパス>\Deployment\Resources\LU\"
4. アシスタントボット用の Dispatch LUIS アプリで HellSkill に新しい例文が追加されたことを確認。
5. アシスタントボットも Azure に発行して動作を確認。
.\Deployment\Scripts\publish.ps1 -name kenakamumyassistant-qbp5igb -resourceGroup kenakamumyassistant
まとめ
メッセージタイプのスキルは、これまでの知識でほぼ動作が成り立ちます。次回はイベントタイプのスキルについて見ていきます。