1
votes

I am creating a bot to proactively start a conversation with an account I have never had a previous conversation with. I have created another controller that I am posting to and doing the following steps:

  public class OutboundController : ApiController {
public HttpResponseMessage Post([FromUri] int id, [FromBody] OutboundData outboundData) {

  MicrosoftAppCredentials.TrustServiceUrl(outboundData.ServiceUrl);

  //create conversation
  var connector = new ConnectorClient(new Uri(outboundData.ServiceUrl));

  var botAccount = new ChannelAccount { Id = outboundData.FromAccountId, Name = outboundData.FromAccountName };
  var toAccount = new ChannelAccount { Id = outboundData.ToAccountId, Name = outboundData.ToAccountName };

  if(!MicrosoftAppCredentials.IsTrustedServiceUrl(outboundData.ServiceUrl)) {
    throw new Exception("service URL is not trusted!");
  }
  var conversationResponse = connector.Conversations.CreateDirectConversation(botAccount, toAccount);

  var client = new BuslogicClient();
  var confirmData = client.GetOutboundData(id);
  var greetingMessage = CreateGreetingMessage(confirmData);

  var convoMessage = Activity.CreateMessageActivity();
  convoMessage.Text = greetingMessage;
  convoMessage.From = botAccount;
  convoMessage.Recipient = toAccount;
  convoMessage.Conversation = new ConversationAccount(id: conversationResponse.Id);
  convoMessage.Locale = "en-Us";
  connector.Conversations.SendToConversationAsync((Activity)convoMessage);


  string message = string.Format("I received correlationid:{0} and started conversationId:{1}", id, conversationResponse.Id);
  var response = Request.CreateResponse(HttpStatusCode.OK, message);
  return response;
}

When I call connector.Conversations.CreateDirectConversation I am getting the following exception: Additional information: Authorization for Microsoft App ID [ID] failed with status code Unauthorized and reason phrase 'Unauthorized'. If I do this with appId and password blank everything works fine in the channel emulator. I've tried providing the MicrosoftAppCredentials to the constructor of the ConnectorClient, but that has no affect. I've read on other threads that the service URL must be trusted so I used MicrosoftAppCredentials.TrustServiceUrl.

versions I am using: BotBuilder 3.5.3 Channel Emulator 3.0.0.59

The use-case for my bot is to post to the outbound controller with some user info to create a proactive message to be sent out (specifically SMS). If the user responds to my message it will be intercepted by the messages controller and passed to my dialogs for further processing and conversation responses on that same channel.

I've also taken a look at: https://github.com/Microsoft/BotBuilder/issues/2155 but don't quite understand solution described in the comments or if it even pertains to the issue I'm trying to solve.

Any suggestions or help would be appreciated!

1
@artem The scenario is a bit different because I'm not resuming a previous incoming conversation. I am initiating a new conversation. In the link you provided someone asked about CreateDirectConversationAsync and you replied " sure I did. It doesn't work. Apparently error is not related with the way conversation is created" I'm wondering if I have to post to the connector api the account data first so that connector can fully trust the conversation creation. - Jay Tina

1 Answers

0
votes

You need to pass credentials explicitly to connector:

var credentials = new MicrosoftAppCredentials("YoursMicrosoftAppId", "YoursMicrosoftAppPassword");
var connector = new ConnectorClient(serviceUrl, credentials);