Bot FrameworkとDirectLineを使った連携テスト(2)
このパートでは、DirectLine 承認を作成し、ボットの応答から値を取得します。
逆シリアル化が完了したので、今度はコレクションと全体から情報を取得し、承認の取得、API 呼び出しの実行、結果のアサートに使用します。
次の説明では、Bot Framework の動作に関する基本情報がすべて網羅されているわけではありません。理解できない場合は、公式ドキュメントを確認してください。
WebClient を使用した API 呼び出しの実行
API の呼び出しを容易にするために、utils クラスを作成しました。ここに、数回使用する関数を保存します。このクラスには、POST 用の uploadString と GET 用の downloadString が含まれています。
public class Utils
{
/// <summary>
/// Uploads to an URL and gets result
/// </summary>
/// <typeparam name="T">Type of object you are receiving</typeparam>
/// <param name="bearer">Token</param>
/// <param name="url">Url</param>
/// <param name="serializedJson">Serialized JSON to send</param>
/// <returns></returns>
public static T uploadString<T>(string bearer, string url, string serializedJson)
{
string serializedResult = "";
/// Webclient
using (var client = new WebClient())
{
/// Add headers
client.Headers.Add("Content-Type", "application/json");
client.Headers.Add("Authorization", $"Bearer {bearer}");
/// Upload string
serializedResult = client.UploadString(url, serializedJson);
}
/// Get result and return it as an object
return JsonConvert.DeserializeObject<T>(serializedResult);
}
/// <summary>
/// Downloads from URL
/// </summary>
/// <typeparam name="T">Type of object you are receiving</typeparam>
/// <param name="bearer">Token</param>
/// <param name="url">Url</param>
/// <returns></returns>
public static T downloadString<T>(string bearer, string url)
{
string serializedResult = "";
/// Webclient
using (var client = new WebClient())
{
/// Add headers
client.Headers.Add("Content-Type", "application/json");
client.Headers.Add("Authorization", $"Bearer {bearer}");
/// Download string
serializedResult = client.DownloadString(url);
}
/// Get result and return it as an object
return JsonConvert.DeserializeObject<T>(serializedResult);
}
}
DirectLine 認証
公式ドキュメントを読めばその方法がわかり、非常に簡単ですが、関数を使用するとさらに簡単になります。まず最初に、時間切れになった場合に備えて、ケースごとに認証を行っているため、foreach ステートメント内にいることを覚えておいてください。これは、テストが失敗することを意味します。
/// Arrange with current requested values
string token, newToken, conversationId;
/// Act
/// 1 - Get token using secret from DirectLine in BotFramework panel
token = Utils.uploadString<DirectLineAuth>(data.Secret, data.DirectLineGenerateTokenEndpoint, "").token;
これで、会話エンドポイントに対する以降のすべての呼び出しを行うために使用されるトークンが取得されました。
会話を作成しています。
ボットと会話するには、まず会話を作成する必要があります。この会話は会話 ID を含む新しいトークンを返します。
/// 2 -Create a new conversation
var createdConversation = Utils.uploadString<DirectLineAuth>(token, data.DirectLineConversationEndpoint, "");
// This returns a new token and a conversationId
newToken = createdConversation.token;
conversationId = createdConversation.conversationId;
また、newToken と conversationId も保存します。どちらもユーザーがボットにメッセージを送信するために必要です。
アクティビティを会話に送信する
conversationId と conversationEndpoint を使用して、json ファイルの request である Activity を送信する最終エンドポイントを作成できます。
/// 3 - Send an activity to the conversation with new token and conversationId
string directlineConversationActivitiesEndpoint = data.DirectLineConversationEndpoint + conversationId + "/activities";
Utils.uploadString<DirectLineAuth>(newToken, directlineConversationActivitiesEndpoint, JsonConvert.SerializeObject(entry.Request));
最新のメッセージを取得する
メッセージ履歴では、アクティビティを送信した後、ボットはすでに応答しているはずです。そのため、ウォーターマーク付きのすべてのメッセージを取得し、そのウォーターマークを使用して最新のメッセージ/アクティビティをフィルターする必要があります。
/// 4 - Get all activities, we get a List<activity> and a watermark
var getLastActivity = Utils.downloadString<ActivityResponse>(newToken, directlineConversationActivitiesEndpoint);
/// 5 - Get the latest activity which is the response we should be expecting
var latestResponse = getLastActivity.activities[Int32.Parse(getLastActivity.watermark)];
この部分はこれですべてです。次の部分には、json の assert からテキストを取得し、JavaScript で C# で eval() を使用するようなコードに変換し、Assert.isTrue() を使用して最終テスト結果を取得する部分が含まれます。
すべてのコードは私の github の this リポジトリに保存されていることに注意してください。