恢复一个实例处理的3个不同bot的对话
问题描述:
我做了第三方服务。当用于恢复与一个机器人实例的对话时,此服务运行良好。 我想恢复多个机器人的对话。恢复一个实例处理的3个不同bot的对话
有这在我的通知控制器:
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)
{
}
}
如何在此添加凭证提供程序?
此代码时用户需要写我的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());
}
}
,并根据授权,我正在正确的机器人。这是消息控制器的授权。我想添加类似于我的通知控制器的恢复方法。
有什么想法?
答
Okey。我想通了:)
在我的数据库存储的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);