/// <summary> /// Get a client connected to the system at present /// </summary> /// <param name="activity">The activity from the api controller</param> /// <returns></returns> private BotClient GetClient([FromBody] Microsoft.Bot.Connector.Activity activity) { // Get the client (if existing) from the connection BotClient connectingClient = Global.botClients.FirstOrDefault(bc => ((bc.BotURL == activity.ServiceUrl) && (bc.UserID == activity.From.Id))); // Check if we have a valid client... if (connectingClient == null) { // ... else create a connecting client connectingClient = new BotClient(); connectingClient.UserID = activity.From.Id; connectingClient.UserName = activity.From.Name; connectingClient.BotURL = activity.ServiceUrl; connectingClient.ConversationAccount = activity.Conversation; connectingClient.FromID = activity.Recipient.Id; connectingClient.ChannelType = activity.ChannelId; // Add it to the list of clients - will be useful for later Global.botClients.Add(connectingClient); // Tell the client that a connection has been established BotClientUtility.SendMessage(connectingClient, "Connection Established"); } // Return the client for use elsewhere return(connectingClient); }
/// <summary> /// From the Post/API messages... /// ... respond to anything sent over. /// </summary> /// <param name="activity"></param> /// <returns></returns> public async Task <HttpResponseMessage> Post([FromBody] Microsoft.Bot.Connector.Activity activity) { // Get a connecting client BotClient connectingClient = GetClient(activity); // Check the activity type if (activity.Type == ActivityTypes.Message) // message is a message coming to the client { string responseString = "The length of your message to me was: " + activity.Text.Length.ToString(); BotClientUtility.SendMessage(connectingClient, responseString); } var response = Request.CreateResponse(HttpStatusCode.OK); return(response); }