Search code examples
smsbotframeworkproactive

Create Conversation with User - (SMS channel)


I have looked at the proactiveMessages example and the createNewConveration bot examples provided in GIT. Both examples show that a conversation is started with a channel account after it initially interacts with the bot. I need to create a bot that can start a conversation with a user (specifically SMS) that HAS NEVER interacted with my bot previously. I have the valid ID (in this case sms number) to create a channel account object and the twilio phone number that I want to use as the producer of the message activity.

2 Questions: 1) In order to use a connector client and create a direct conversation is it necessary for the user to have previously interacted with my bot? If so, is there a way to load this channel account data into bot data store in order for me to create a conversation? 2) Will there be a future version that will allow our bot (via api) to start conversations with valid channel accounts? The api is a bit misleading by allowing me to CREATE a conversation. It should be named "resumeConversation".


Solution

  • I was able to start a conversation via the connector client with service url: https://sms.botframework.com and the bot app creds. I noticed in my trace logger that channel account id contains the country code in the sms number. In this case the channel accountId should be in this format +1[areacode][number]. My proactive greeting was sent successfully. So I believe the SMS channel will allow conversations to be started from BOT to user.

    ServiceUrl in this case is https://sms.botframework.com for sms channel. The

     var serviceUrl = GetServiceUrlByChannelId(channel);
      MicrosoftAppCredentials.TrustServiceUrl(serviceUrl, DateTime.Now.AddDays(7)); //todo change magic number
      var account = new MicrosoftAppCredentials([MicrosoftAppId], [MicrosoftAppPassword]);
    
      _connector = new ConnectorClient(new Uri(serviceUrl), account);
    
      var botAccount = new ChannelAccount { Id = bot.Id, Name = bot.Name };
      var toAccount = new ChannelAccount { Id = recipient.Id, Name = recipient.Name };
    
      if (!MicrosoftAppCredentials.IsTrustedServiceUrl(serviceUrl)) {
        throw new Exception("Cannot create conversation! Service URL is not trusted!");
      }
    
      var conversationResponse = _connector.Conversations.CreateDirectConversation(botAccount, toAccount);