2017-04-04 102 views
0

我做了第三方服務。當用於恢復與一個機器人實例的對話時,此服務運行良好。 我想恢復多個機器人的對話。恢復一個實例處理的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()); 
     } 
    } 

,並根據授權,我正在正確的機器人。這是消息控制器的授權。我想添加類似於我的通知控制器的恢復方法。

有什麼想法?

回答

1

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); 
相關問題