Bob Swager
Bob Swager

Reputation: 892

resume conversation for 3 different bots handled by one instance

I made third party service. This service works well when is used to resume conversation with one instance of bot. I want to resume conversation for multiple bots.

Have this in my notification controller :

 public class NotificationController : ApiController
{
    [HttpPost]

    public async Task<HttpResponseMessage> SendMessage()
    {

        try
        {
            HttpContent requestContent = Request.Content;
            string jsonContent = requestContent.ReadAsStringAsync().Result;
            List<ConversationModel> model = new List<ConversationModel>();
            model = JsonConvert.DeserializeObject<List<ConversationModel>>(jsonContent);
            for (int i = 0; i < model.Count; i++)
            {
                await ConversationHelper.Resume(model[i]);
            }
            var resp = new HttpResponseMessage(HttpStatusCode.OK);
            resp.Content = new StringContent($"<html><body>Message sent, thanks.</body></html>", System.Text.Encoding.UTF8, @"text/html");
            return resp;
        }

        catch (Exception ex)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
        }
    }
}

I'm sending List of ConversationModel. In this List are stored all the users. Users should get notified.

//this resumes conversation
        public static async Task Resume(ConversationModel model)
        {
            try
            {
                string appdId = ConfigurationManager.AppSettings["MicrosoftAppId"].ToString();
                string appPassword = ConfigurationManager.AppSettings["MicrosoftAppPassword"].ToString();
            MicrosoftAppCredentials.TrustServiceUrl(model.serviceUrl);

            var userAccount = new ChannelAccount(model.toId, model.toName);
            var botAccount = new ChannelAccount(model.fromId, model.fromName);

            var connector = new ConnectorClient(new Uri(model.serviceUrl), microsoftAppId : appdId, microsoftAppPassword : appPassword);


            IMessageActivity message = Activity.CreateMessageActivity();
            if (!string.IsNullOrEmpty(model.conversationId) && !string.IsNullOrEmpty(model.channelId))
            {
                message.ChannelId = model.channelId;
            }
            else
            {

                model.conversationId = (await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount)).Id;
            }
            message.From = userAccount;
            message.Recipient = botAccount;
            message.Conversation = new ConversationAccount(id: model.conversationId);
            message.Text = "This is a test notification.";
            message.Locale = "en-Us";

            message.ChannelData =
               JObject.FromObject(new
               {
                   notification_type = "REGULAR"
               });


            await connector.Conversations.SendToConversationAsync((Activity)message);

        }
        catch (Exception e)
        {

        }
    }

How to add credential provider here ?

This code i'm using when user writes to my bot :

public class MultiCredentialProvider : ICredentialProvider
    {
        public Dictionary<string, string> Credentials = new Dictionary<string, string>
        {

            { "user1", "pass1" },
            { "user2", "pass2" },
            { "user3", "pass3" }
        };

        public Task<bool> IsValidAppIdAsync(string appId)
        {
            return Task.FromResult(this.Credentials.ContainsKey(appId));
        }

        public Task<string> GetAppPasswordAsync(string appId)
        {
            return Task.FromResult(this.Credentials.ContainsKey(appId) ? this.Credentials[appId] : null);
        }

        public Task<bool> IsAuthenticationDisabledAsync()
        {
            return Task.FromResult(!this.Credentials.Any());
        }
    }

And depending on authorization, i'm running proper bot. That's authorization for message controller. I want to add something similar to my notification controller for resume method.

Any idea ?

Upvotes: 0

Views: 172

Answers (1)

Bob Swager
Bob Swager

Reputation: 892

Okey. I figured this out :)

In my database is stored ToName field. This field is for bot Name.

I'm getting proper credentials based on this field.

Then, i'm just using right credentials to resolve connector :

 string appdId = GetApp(model);
                string appPassword = GetPassword(model);

                MicrosoftAppCredentials.TrustServiceUrl(model.serviceUrl);

                var userAccount = new ChannelAccount(model.toId, model.toName);
                var botAccount = new ChannelAccount(model.fromId, model.fromName);

                var connector = new ConnectorClient(new Uri(model.serviceUrl), microsoftAppId : appdId, microsoftAppPassword : appPassword);

Upvotes: 1

Related Questions