From 71a1953083c8e9ba1054a59e2e559b6917ccb8fa Mon Sep 17 00:00:00 2001 From: coreyjansen Date: Tue, 29 Sep 2020 08:40:51 -0500 Subject: [PATCH 1/4] Update app.rb --- app.rb | 74 +++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 21 deletions(-) diff --git a/app.rb b/app.rb index 433fb95..020e9d7 100644 --- a/app.rb +++ b/app.rb @@ -4,10 +4,8 @@ require 'sinatra' require 'nokogiri' -INTERCOM = Intercom::Client.new( - app_id: ENV['INTERCOM_APP_ID'], - api_key: ENV['INTERCOM_API_KEY'] -) + +INTERCOM = Intercom::Client.new(token: ENV['INTERCOM_TOKEN']) Twilio.configure do |config| config.account_sid = ENV['TWILIO_SID'] @@ -16,27 +14,51 @@ TWILIO = Twilio::REST::Client.new +#Tag ID that you want to associate to people that come in through a SMS so that you dont send sms's to every person in intercom +TAGID = ENV['INTERCOM_TAG'], + + post '/incoming_from_twilio' do from = params[:From] body = params[:Body] + +# Search for contacts by email +users = INTERCOM.contacts.search( + "query": { + "field": 'phone', + "operator": '=', + "value": from + } +) +user= users.first +unless user # Create or update the user - user = INTERCOM.users.create( - user_id: from - ) + user = INTERCOM.contacts.create(phone: from, role: "lead") + +end + tag = INTERCOM.tags.find(id:TAGID) + user.add_tag(id: TAGID) # Start a new conversation - INTERCOM.messages.create( +conversations = INTERCOM.conversations.find_all(intercom_user_id: user.id, type:'user') +conversation = conversations.first +#If there is a past conversation then reply to it otherwise create a new conversation +if conversation + INTERCOM.conversations.reply_to_last(intercom_user_id: user.id, type: 'user', message_type: 'comment', body: body) + else + INTERCOM.messages.create({ from: { type: 'user', id: user.id }, body: body - ) - + }) +end "ok" end + post '/incoming_from_intercom' do request.body.rewind intercom_params = JSON.parse(request.body.read) @@ -44,17 +66,27 @@ # Extract the new message, and convert it to plaintext last_message_html = intercom_params['data']['item']['conversation_parts']['conversation_parts'][-1]['body'] last_message = Nokogiri::HTML(last_message_html).text - + found = false # Load the user who we will SMS - user = INTERCOM.users.find(id: intercom_params['data']['item']['user']['id']) - - # Send the response to Twilio - unless last_message.strip.empty? - TWILIO.messages.create( - from: ENV['TWILIO_NUMBER'], - to: user.user_id, - body: last_message - ) + user = INTERCOM.contacts.find(id: intercom_params['data']['item']['user']['id']) + unless user.phone.strip.empty? + # See if the user containts the tag to specifify if you should send the text to them or not + tags = user.tags.each {|t| if t.id == TAGID + found = true + end} + + print tags + if found == true + # Send the response to Twilio + unless last_message.strip.empty? + TWILIO.messages.create( + from: ENV['TWILIO_NUMBER'], + to: user.phone, + body: last_message + ) + end + end - "ok" + end + "ok" end From b51d94ecb17ed45bf56e9b916ed0c440129c64c1 Mon Sep 17 00:00:00 2001 From: coreyjansen Date: Tue, 29 Sep 2020 08:41:14 -0500 Subject: [PATCH 2/4] Revert "Update app.rb" This reverts commit 71a1953083c8e9ba1054a59e2e559b6917ccb8fa. --- app.rb | 74 +++++++++++++++++----------------------------------------- 1 file changed, 21 insertions(+), 53 deletions(-) diff --git a/app.rb b/app.rb index 020e9d7..433fb95 100644 --- a/app.rb +++ b/app.rb @@ -4,8 +4,10 @@ require 'sinatra' require 'nokogiri' - -INTERCOM = Intercom::Client.new(token: ENV['INTERCOM_TOKEN']) +INTERCOM = Intercom::Client.new( + app_id: ENV['INTERCOM_APP_ID'], + api_key: ENV['INTERCOM_API_KEY'] +) Twilio.configure do |config| config.account_sid = ENV['TWILIO_SID'] @@ -14,51 +16,27 @@ TWILIO = Twilio::REST::Client.new -#Tag ID that you want to associate to people that come in through a SMS so that you dont send sms's to every person in intercom -TAGID = ENV['INTERCOM_TAG'], - - post '/incoming_from_twilio' do from = params[:From] body = params[:Body] - -# Search for contacts by email -users = INTERCOM.contacts.search( - "query": { - "field": 'phone', - "operator": '=', - "value": from - } -) -user= users.first -unless user # Create or update the user - user = INTERCOM.contacts.create(phone: from, role: "lead") - -end + user = INTERCOM.users.create( + user_id: from + ) - tag = INTERCOM.tags.find(id:TAGID) - user.add_tag(id: TAGID) # Start a new conversation -conversations = INTERCOM.conversations.find_all(intercom_user_id: user.id, type:'user') -conversation = conversations.first -#If there is a past conversation then reply to it otherwise create a new conversation -if conversation - INTERCOM.conversations.reply_to_last(intercom_user_id: user.id, type: 'user', message_type: 'comment', body: body) - else - INTERCOM.messages.create({ + INTERCOM.messages.create( from: { type: 'user', id: user.id }, body: body - }) -end + ) + "ok" end - post '/incoming_from_intercom' do request.body.rewind intercom_params = JSON.parse(request.body.read) @@ -66,27 +44,17 @@ # Extract the new message, and convert it to plaintext last_message_html = intercom_params['data']['item']['conversation_parts']['conversation_parts'][-1]['body'] last_message = Nokogiri::HTML(last_message_html).text - found = false + # Load the user who we will SMS - user = INTERCOM.contacts.find(id: intercom_params['data']['item']['user']['id']) - unless user.phone.strip.empty? - # See if the user containts the tag to specifify if you should send the text to them or not - tags = user.tags.each {|t| if t.id == TAGID - found = true - end} - - print tags - if found == true - # Send the response to Twilio - unless last_message.strip.empty? - TWILIO.messages.create( - from: ENV['TWILIO_NUMBER'], - to: user.phone, - body: last_message - ) - end - - end + user = INTERCOM.users.find(id: intercom_params['data']['item']['user']['id']) + + # Send the response to Twilio + unless last_message.strip.empty? + TWILIO.messages.create( + from: ENV['TWILIO_NUMBER'], + to: user.user_id, + body: last_message + ) end - "ok" + "ok" end From 01ffb7b6b34d7d416b03d50dce7bd2bc1292091d Mon Sep 17 00:00:00 2001 From: coreyjansen Date: Tue, 29 Sep 2020 08:45:21 -0500 Subject: [PATCH 3/4] Updated App.rb - Changed Intercom client to use token instead of app id and key - Changed function from using the users api function to contacts as it no longer worked - When a new user is created it will Tag them when the desired tag. I used a tag called SMS to allow me to only send text messages back to people that contacted through the SMS originally vs everyone who came through my intercom - Replies to conversation of the user if one exists if it doesnt then it will create a new one --- app.rb | 74 +++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 21 deletions(-) diff --git a/app.rb b/app.rb index 433fb95..020e9d7 100644 --- a/app.rb +++ b/app.rb @@ -4,10 +4,8 @@ require 'sinatra' require 'nokogiri' -INTERCOM = Intercom::Client.new( - app_id: ENV['INTERCOM_APP_ID'], - api_key: ENV['INTERCOM_API_KEY'] -) + +INTERCOM = Intercom::Client.new(token: ENV['INTERCOM_TOKEN']) Twilio.configure do |config| config.account_sid = ENV['TWILIO_SID'] @@ -16,27 +14,51 @@ TWILIO = Twilio::REST::Client.new +#Tag ID that you want to associate to people that come in through a SMS so that you dont send sms's to every person in intercom +TAGID = ENV['INTERCOM_TAG'], + + post '/incoming_from_twilio' do from = params[:From] body = params[:Body] + +# Search for contacts by email +users = INTERCOM.contacts.search( + "query": { + "field": 'phone', + "operator": '=', + "value": from + } +) +user= users.first +unless user # Create or update the user - user = INTERCOM.users.create( - user_id: from - ) + user = INTERCOM.contacts.create(phone: from, role: "lead") + +end + tag = INTERCOM.tags.find(id:TAGID) + user.add_tag(id: TAGID) # Start a new conversation - INTERCOM.messages.create( +conversations = INTERCOM.conversations.find_all(intercom_user_id: user.id, type:'user') +conversation = conversations.first +#If there is a past conversation then reply to it otherwise create a new conversation +if conversation + INTERCOM.conversations.reply_to_last(intercom_user_id: user.id, type: 'user', message_type: 'comment', body: body) + else + INTERCOM.messages.create({ from: { type: 'user', id: user.id }, body: body - ) - + }) +end "ok" end + post '/incoming_from_intercom' do request.body.rewind intercom_params = JSON.parse(request.body.read) @@ -44,17 +66,27 @@ # Extract the new message, and convert it to plaintext last_message_html = intercom_params['data']['item']['conversation_parts']['conversation_parts'][-1]['body'] last_message = Nokogiri::HTML(last_message_html).text - + found = false # Load the user who we will SMS - user = INTERCOM.users.find(id: intercom_params['data']['item']['user']['id']) - - # Send the response to Twilio - unless last_message.strip.empty? - TWILIO.messages.create( - from: ENV['TWILIO_NUMBER'], - to: user.user_id, - body: last_message - ) + user = INTERCOM.contacts.find(id: intercom_params['data']['item']['user']['id']) + unless user.phone.strip.empty? + # See if the user containts the tag to specifify if you should send the text to them or not + tags = user.tags.each {|t| if t.id == TAGID + found = true + end} + + print tags + if found == true + # Send the response to Twilio + unless last_message.strip.empty? + TWILIO.messages.create( + from: ENV['TWILIO_NUMBER'], + to: user.phone, + body: last_message + ) + end + end - "ok" + end + "ok" end From 642ac1b56e0453b7bb9c938ff5c7a81417f3a227 Mon Sep 17 00:00:00 2001 From: Corey Jansen Date: Tue, 29 Sep 2020 08:46:44 -0500 Subject: [PATCH 4/4] Update readme.md --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 50bbdb1..55d57ca 100644 --- a/readme.md +++ b/readme.md @@ -16,8 +16,8 @@ SMS (or "texts") have an extremely low barrier to entry, and yet are reported as Let's imagine a simple series of actions we want to neatly support: * Customer sends in an SMS to our helpline number -* A user is automatically created in Intercom, and the phone number is stored -* A new conversation is created in Intercom +* A user is automatically created in Intercom, and the phone number is stored, the user is tagged with the defined tag +* A new conversation is created in Intercom or replied too if this isnt the first SMS reply * Admin replies to that conversation get sent to the user over SMS Using Twilio's webhooks, we can write a handler to accomplish the first 3 steps via the Intercom API: