LoginSignup
0
0

More than 3 years have passed since last update.

Bot Builder v4 でボット開発 : スキルを開発する - その 4 メッセージタイプのアクション追加

Last updated at Posted at 2020-03-20

前回までテンプレートで作成されたソリューションを見てきました。今回は新しくメッセージタイプのアクションを追加してみます。

追加するアクション

現在時刻を返すアクションを追加します。

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

3. 結果を確認。
image.png

4. LUIS 側でも確認。
image.png

5. Services\HelloSkillLuis.cs に新しいインテントがあることを確認。
image.png

Response の追加

次にメッセージを追加します。本来は全言語追加しますが、ここでは英語のみとします。

1. Responses フォルダに GetTimeResponses.lg を追加。

> ------------ Activity templates ------------

# CurrentTimeMessage
[Activity
    Text = @{CurrentTimeText()}
    InputHint = acceptingInput
]


> ------------ Text templates ------------

# CurrentTimeText
- It's @{CurrentTime}

2. 言語ファイルはサポートする分必要なため、ファイルをコピーして拡張子をそれぞれの言語に設定。
image.png

3. Startup.cs でテンプレートに追加。

var templateFiles = new List<string>() { "MainResponses", "SampleResponses", "GetTimeResponses" };

4. Visual Studio よりそれぞれのファイルのプロパティで「Copy if newer」を選択。
image.png

マニフェストの更新

dispatchModels\intents に GetTime を追加するだけです。
image.png

GetTimeDialog の追加

1. Dialogs フォルダに GetTimeDialog.cs を追加。

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 を追加。

MainDialog.cs
private GetTimeDialog _getTimeDialog;

4. コンストラクタでインスタンスを取得。

MainDialog.cs
_getTimeDialog = serviceProvider.GetService<GetTimeDialog>();
AddDialog(_getTimeDialog);

5. RouteStepAsync メソッドで分岐を追加。

MainDialog.cs
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 を押下してデバッグ実行。エミュレーターで動作を確認。
image.png

公開してテスト

今回はアシスタントボット経由でのテストを行います。

1. publish.ps1 スクリプトでスキルを公開。

.\Deployment\Scripts\publish.ps1 -name kenakamuhelloskill-om7pvdw -resourceGroup kenakamuhelloskill

2. 変更されたマニフェストを確認。
image.png

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 に新しい例文が追加されたことを確認。
image.png

5. アシスタントボットも Azure に発行して動作を確認。

.\Deployment\Scripts\publish.ps1 -name kenakamumyassistant-qbp5igb -resourceGroup kenakamumyassistant

image.png

まとめ

メッセージタイプのスキルは、これまでの知識でほぼ動作が成り立ちます。次回はイベントタイプのスキルについて見ていきます。

次の記事へ
目次に戻る

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0