|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using Microsoft.Bot.Connector.DirectLine; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Configuration; |
| 8 | +using System.Linq; |
| 9 | +using System.Threading; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace Microsoft.PowerVirtualAgents.Samples.BotConnectorApp |
| 13 | +{ |
| 14 | + public class BotConnectorApp |
| 15 | + { |
| 16 | + private static string _watermark = null; |
| 17 | + private const int _botReplyWaitIntervalInMilSec = 3000; |
| 18 | + private const string _botDisplayName = "Bot"; |
| 19 | + private const string _userDisplayName = "You"; |
| 20 | + private static string s_endConversationMessage; |
| 21 | + private static BotService s_botService; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Start BotConnectorApp console |
| 25 | + /// See <see cref="README.md"/> for information on how to update bot settings in App.config |
| 26 | + /// Takes user input and output bot reply, until user types EndConversationMessage |
| 27 | + /// </summary> |
| 28 | + public static void Main(string[] args) |
| 29 | + { |
| 30 | + var botId = ConfigurationManager.AppSettings["BotId"] ?? string.Empty; |
| 31 | + var tenantId = ConfigurationManager.AppSettings["BotTenantId"] ?? string.Empty; |
| 32 | + var botTokenEndpoint = ConfigurationManager.AppSettings["BotTokenEndpoint"] ?? string.Empty; |
| 33 | + var botName = ConfigurationManager.AppSettings["BotName"] ?? string.Empty; |
| 34 | + s_endConversationMessage = ConfigurationManager.AppSettings["EndConversationMessage"] ?? "quit"; |
| 35 | + if (string.IsNullOrEmpty(botId) || string.IsNullOrEmpty(tenantId) || string.IsNullOrEmpty(botTokenEndpoint) || string.IsNullOrEmpty(botName)) |
| 36 | + { |
| 37 | + Console.WriteLine("Update App.config and start again."); |
| 38 | + Console.WriteLine("Press any key to exit"); |
| 39 | + Console.Read(); |
| 40 | + Environment.Exit(0); |
| 41 | + } |
| 42 | + |
| 43 | + s_botService = new BotService() |
| 44 | + { |
| 45 | + BotName = botName, |
| 46 | + BotId = botId, |
| 47 | + TenantId = tenantId, |
| 48 | + TokenEndPoint = botTokenEndpoint, |
| 49 | + }; |
| 50 | + StartConversation().Wait(); |
| 51 | + } |
| 52 | + |
| 53 | + private static async Task StartConversation() |
| 54 | + { |
| 55 | + var token = await s_botService.GetTokenAsync(); |
| 56 | + using (var directLineClient = new DirectLineClient(token)) |
| 57 | + { |
| 58 | + var conversation = await directLineClient.Conversations.StartConversationAsync(); |
| 59 | + var conversationtId = conversation.ConversationId; |
| 60 | + string inputMessage; |
| 61 | + |
| 62 | + while (!string.Equals(inputMessage = GetUserInput(), s_endConversationMessage, StringComparison.OrdinalIgnoreCase)) |
| 63 | + { |
| 64 | + // Send user message using directlineClient |
| 65 | + await directLineClient.Conversations.PostActivityAsync(conversationtId, new Activity() |
| 66 | + { |
| 67 | + Type = ActivityTypes.Message, |
| 68 | + From = new ChannelAccount { Id = "userId", Name = "userName" }, |
| 69 | + Text = inputMessage, |
| 70 | + TextFormat = "plain", |
| 71 | + Locale = "en-Us", |
| 72 | + }); |
| 73 | + |
| 74 | + Console.WriteLine($"{_botDisplayName}:"); |
| 75 | + Thread.Sleep(_botReplyWaitIntervalInMilSec); |
| 76 | + |
| 77 | + // Get bot response using directlinClient |
| 78 | + List<Activity> responses = await GetBotResponseActivitiesAsync(directLineClient, conversationtId); |
| 79 | + BotReply(responses); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Prompt for user input |
| 86 | + /// </summary> |
| 87 | + /// <returns>user message as string</returns> |
| 88 | + private static string GetUserInput() |
| 89 | + { |
| 90 | + Console.WriteLine($"{_userDisplayName}:"); |
| 91 | + var inputMessage = Console.ReadLine(); |
| 92 | + return inputMessage; |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Use directlineClient to get bot response |
| 97 | + /// </summary> |
| 98 | + /// <returns>List of DirectLine activities</returns> |
| 99 | + /// <param name="directLineClient">directline client</param> |
| 100 | + /// <param name="conversationtId">current conversation ID</param> |
| 101 | + /// <param name="botName">name of bot to connect to</param> |
| 102 | + private static async Task<List<Activity>> GetBotResponseActivitiesAsync(DirectLineClient directLineClient, string conversationtId) |
| 103 | + { |
| 104 | + ActivitySet response = null; |
| 105 | + List<Activity> result = new List<Activity>(); |
| 106 | + |
| 107 | + do |
| 108 | + { |
| 109 | + response = await directLineClient.Conversations.GetActivitiesAsync(conversationtId, _watermark); |
| 110 | + if (response == null) |
| 111 | + { |
| 112 | + // response can be null if directLineClient token expires |
| 113 | + Console.WriteLine("Conversation expired. Press any key to exit."); |
| 114 | + Console.Read(); |
| 115 | + directLineClient.Dispose(); |
| 116 | + Environment.Exit(0); |
| 117 | + } |
| 118 | + |
| 119 | + _watermark = response?.Watermark; |
| 120 | + result = response?.Activities?.Where(x => |
| 121 | + x.Type == ActivityTypes.Message && |
| 122 | + string.Equals(x.From.Name, s_botService.BotName, StringComparison.Ordinal)).ToList(); |
| 123 | + |
| 124 | + if (result != null && result.Any()) |
| 125 | + { |
| 126 | + return result; |
| 127 | + } |
| 128 | + |
| 129 | + Thread.Sleep(1000); |
| 130 | + } while (response != null && response.Activities.Any()); |
| 131 | + |
| 132 | + return new List<Activity>(); |
| 133 | + } |
| 134 | + |
| 135 | + /// <summary> |
| 136 | + /// Print bot reply to console |
| 137 | + /// </summary> |
| 138 | + /// <param name="responses">List of DirectLine activities <see cref="https://github.com/Microsoft/botframework-sdk/blob/master/specs/botframework-activity/botframework-activity.md"/> |
| 139 | + /// </param> |
| 140 | + private static void BotReply(List<Activity> responses) |
| 141 | + { |
| 142 | + responses?.ForEach(responseActivity => |
| 143 | + { |
| 144 | + // responseActivity is standard Microsoft.Bot.Connector.DirectLine.Activity |
| 145 | + // See https://github.com/Microsoft/botframework-sdk/blob/master/specs/botframework-activity/botframework-activity.md for reference |
| 146 | + // Showing examples of Text & SuggestedActions in response payload |
| 147 | + if (!string.IsNullOrEmpty(responseActivity.Text)) |
| 148 | + { |
| 149 | + Console.WriteLine(string.Join(Environment.NewLine, responseActivity.Text)); |
| 150 | + } |
| 151 | + |
| 152 | + if (responseActivity.SuggestedActions != null && responseActivity.SuggestedActions.Actions != null) |
| 153 | + { |
| 154 | + var options = responseActivity.SuggestedActions?.Actions?.Select(a => a.Title).ToList(); |
| 155 | + Console.WriteLine($"\t{string.Join(" | ", options)}"); |
| 156 | + } |
| 157 | + }); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments