恢复一个实例处理的 3 个不同机器人的对话

resume conversation for 3 different bots handled by one instance

我做了第三方服务。当用于恢复与一个机器人实例的对话时,该服务运行良好。 我想恢复多个机器人的对话。

在我的通知控制器中有这个:

 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);
        }
    }
}

我正在发送 ConversationModel 列表。在此列表中存储了所有用户。用户应该得到通知。

//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)
        {

        }
    }

如何在此处添加凭据提供程序?

我在用户写信给我的机器人时使用的代码:

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());
        }
    }

根据授权,我是 运行 合适的机器人。这是消息控制器的授权。我想为 resume 方法添加类似于我的通知控制器的东西。

有什么想法吗?

好的。我想通了:)

在我的数据库中存储了 ToName 字段。此字段用于机器人名称。

我正在根据此字段获取正确的凭据。

然后,我只是使用正确的凭据来解析连接器:

 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);