ユニバーサル アクション モデルのコード サンプル - プロジェクト管理

このサンプルは、アダプティブ カード バージョン 1.4 以降で使用できるユニバーサル アクション モデルの実装を示しています。

前提条件

  • Outlook/OWA クライアントを使用でき、アカウントを持っています。
  • 有効な Azure サブシプション。
  • Azure Bot Framework の理解。

ボットのセットアップ

  • こちらの手順に従って、ボットを Azure Bot Serviceに登録します。
  • Outlook チャネルが有効になっていることを確認します。
    • Azure portalでボット リソースを開きます。
    • [ チャネル ] ウィンドウを開きます。
    • [使用可能なチャネル] セクションで [Outlookチャネル] を 選択します。
    • [ アクション可能なメッセージ ] タブの [ 適用 ] をクリックし、その後に 登録してください
    • アクセスを要求するには、登録フォームに入力します。 詳細については、「 アクション可能な電子メール開発者ダッシュボードにサービスを登録 する」を参照してください。
  • こちらの手順に従って、Bot Framework SDK を使用してボットを作成 します

手順 1: アダプティブ カード ペイロードの準備ができていることを確認する

プロジェクト管理シナリオでは、 JSON ペイロードについては、こちらを参照してください。 以下では、モバイル画面とデスクトップ画面でペイロードのレンダリングを確認できます。

ユニバーサル アクションの場合は、入力フィールドを収集し、型adaptiveCard/actionのアクティビティをInvokeターゲット ボットに送信する を使用Action.Executeする必要があります。 ターゲット ボットは、 フィールドを使用して実行されたアクションを verb 識別できます。 フィールドを使用して、追加の入力を data 送信できます。

JSON ペイロード

プロジェクト管理シナリオのアクションのスニペットを次に示します。

{
  "type": "ActionSet",
  "actions": [
    {
      "type": "Action.Execute",
      "title": "Mark complete",
      "verb": "markComplete",
      "data": {},
      "isPrimary": true,
      "style": "positive"
    },
    {
      "type": "Action.ShowCard",
      "title": "Add a comment",
      "card": {
        "type": "AdaptiveCard",
        "body": [
          {
            "type": "Input.Text",
            "id": "AddComment",
            "placeholder": "Add a comment",
            "isMultiline": true
          },
          {
            "type": "ActionSet",
            "actions": [
              {
                "type": "Action.Execute",
                "title": "Submit",
                "verb": "projectSubmitComment",
                "data": "{Reason: {{AddComment.value}}}"
              }
            ]
          }
        ],
        "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
        "padding": "None"
      }
    }
  ],
  "spacing": "None"
}

詳細については、「Action.Execute スキーマとプロパティ」を参照してください。

手順 2: プロジェクト管理用にボットにカスタム ビジネス ロジックを記述する

Azure ボットでは、OnAdaptiveCardInvokeAsync メソッドを使用して、フィールドを使用してアクションをverbキャプチャし、ビジネス ロジックを追加し、更新カードを Outlook に送信できます。

protected override async Task<AdaptiveCardInvokeResponse> OnAdaptiveCardInvokeAsync(
    ITurnContext<IInvokeActivity> turnContext,
    AdaptiveCardInvokeValue invokeValue,
    CancellationToken cancellationToken)
{
    try
    {
        if (invokeValue.Action.Verb == "markComplete")
        {
            // This function can contain your business logic
            // to capture the complete action and show the refresh card
            return await ProcessProjectMarkComplete();
        }
        else if (invokeValue.Action.Verb == "projectSubmitComment")
        {
            // This function can contain your business logic
            // to submit the comment and show the refresh car
            return await ProcessProjectSubmitComment();
        }
        else
        {
            throw new InvokeResponseException(HttpStatusCode.NotImplemented);
        }
    }
    catch (AdaptiveCardActionException e)
    {
        throw new InvokeResponseException(HttpStatusCode.NotImplemented, e.Response);
    }
}

ボットをローカルでテストすることも、 ボットを Azure にデプロイすることもできます。

手順 3: アクション可能なメッセージを送信する

ユニバーサル アクションに基づくアクション可能メッセージは、他のアクション可能メッセージと同様に送信できます。 詳細については、「 電子メールでアクション可能なメッセージを送信する」を参照してください。