Search code examples
twiliobotframeworkdirect-line-botframework

Bot Framework Twilio With Multiple Numbers


I have a bot that I've built that's running on the Azure Bot Service with a Twilio Channel. I'm sending Proactive activities via my Twilio channel. Everything is working fine. I just got a request that a customer wants to have their own phone number. I would like to just have 1 bot service running but have multiple Twilio phone numbers go into this.

My thought was that I could setup an API service which would then be the incoming message call back / webhook from Twilio which then would use the Directline API to the Bot Framework. It would essentially just replace the https://sms.botframework.com/api/sms service. The problem is that I'm not sure I could still have the proactive messages working - it seems like the Directline 3.0 API works only when a conversation is started first with it.

Does anyone have any thoughts on this if this would work or have any other ideas?

Thanks


Solution

  • Yes, the approach which you mentioned above would be ideal. Each Web App Bot/Bot Channels Registration can only be associated with one Twilio number. I will elaborate on the steps which you mentioned above:

    • Create a server running the Twilio SMS API code which forwards the messages to the bot via the DirectLine API. The user sends a message to this server.
    • For every activity sent to the bot, make sure to include the number: Activity.ChannelData = new { fromNumber: <123-456-7890> }. The Server forwards the message to the bot.
    • You will need to re-attach the fromNumber to the bot's outgoing activity so that your Twilio API server knows where to send the outgoing message to. The Bot sends the reply to the server.
    • The Twilio API server sends Activity.Text to the user. The Server forwards message from bot to user.

    For the proactive messages part, you can add a conversation property to the address param, and set the id to the user's phone number.

    Example:

    bot.beginDialog(
      {
        user: { id: '+1234567890' },
        bot: { id: '+9876543210' },
        conversation: { id: '+1234567890' },
        channelId: 'sms',
        serviceUrl: 'https://sms.botframework.com'
      },
    );
    

    Hope this helps.