Integrate incoming Twilio SMS to Intercom

I use Twilio often to send automatic text messages, in this case, I was using it to send to clients with a reminder text message for their upcoming webinar. Originally I was having the text message replies give an automatic bounce back and then send me an email with the text message body. The problem with that is I wasn’t able to easily reply to these text messages. Since we already use Intercom chat on our websites I wanted to find a way to integrate replies on the SMS’s to our Intercom chat to allow us to easily respond. To do so I found a repo of someone that came up with a proof of concept how this could be done. The code had been created in 2015 so it no longer worked and lacked some functionality.

It was coded in Ruby in which I have never used until today and it was a good reason to learn it. I started with first modifying the code just to work. As it was written any incoming message would fire a webhook in Twilio that would call the Ruby app and create a new user and conversation in Intercom. When the admin replies the reply would go into a text message back to the user.

What I found orignally was each reply from the user on SMS would create a whole new thread in Intercom which made it really hard to follow. I got around this by first looking for a conversation from the user and if it existed then it would reply to that conversation instead of creating a new one. Another issue I noticed is it would send a SMS to ever user in intercom as long as they had a phone number defined. To get around this I tagged every new user with a SMS tag and that way when we responded we would only send a SMS to a user with that Tag.

Take a look at my code edits in the fork here https://github.com/coreyjansen/intercom-twilio-demo

Installation:

I chose to run this ruby code on a Google Virtual Machine and used a ngrok tunnel to allow it to be accessible from the web over https

Twilio Webhook Setup:

Add your webhook into the Twilio number you want to use under the messaging tab. You can see mine in the image above

Intercom Webhook Setup:

  1. Navigate to the Intercom Developers Hub https://developers.intercom.com/
  2. Create a new Intercom App
  3. Assign your Intercom App to your workspace
  4. Grab your token from the app attached to your workplace and save it, you will need this
  5. Create a webhook like the following

Feel free to critique my code and fork it and make any changes, as I mentioned this is the first time I have used Ruby so I am sure there is alot of best practices that were not followed and I would love to learn how I can improve this!

You may also like