2017-04-04 3 views
0

私は第三者サービスを行った。このサービスは、ボットの1つのインスタンスとの会話を再開するために使用される場合にうまく機能します。 複数のボットの会話を再開したい。1つのインスタンスで処理される3つの異なるボットの会話を再開する

は私の通知コントローラでこれを持っている:

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

と承認に応じて、私は適切なボットを実行していますよ。それがメッセージコントローラの認可です。私はレジュームメソッドの通知コントローラに似たものを追加したいと思います。

答えて

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

その後、私はちょうどコネクターを解決するために、右の資格情報を使用しています

関連する問題