SMS forwarding to an email using MailChimp
Depending on your application, you might receive multiple messages back to back to back – system notifications, alerts, user confirmations and so on – that would be inconvenient to redirect to your phone one by one. As an alternative, how about receiving a full list of the inbound texts sent to you as an email? Using MailChimp with Tropo, this can be accomplished with relative ease.
MailChimp allows users to send an email to multiple email addresses at once as an email blast; this blog will show how to implement MailChimp’s API with a Tropo Scripting app using Ruby. As is typical, first step is to set up an account for MailChimp.
Go here to sign up for an account, then once signed up and logged in, we need to create a list – this is how MailChimp sends emails. Unfortunately, lists cannot be made programmatically, so to set up a new list – sign-in, fill out the requirements, then click the tab Lists.
Once there, hit the “create a subscriber list” button to the left. Following that, some information will be requested – list name, default name, and default email (which is not the email that will send the text messages, but the email that receives confirmation that the emails were sent). When everything is filled out, hit save to create a new list.
Next, the first email address that will receive the email containing the texts needs to be added. This can be done by clicking “add people”.
After selecting “add people”, the only relevant information that needs to be added is the email address. Repeat this process to add multiple email destinations.
From here, the app takes over. To begin, a few requirements are necessary – uri, to format strings into proper URLs, net/http, to submit URLs to receive information and send the emails, and json, to read the received information.
%w(uri net/http json).each{|lib| require lib}Following this will be the config section, which are constants that can be set in the beginning.
**Important Note**
-In the API_URL the first three characters can change and will rely strictly on the API_KEY. An example to expect is: myapikey-us2, in this case the first three characters would be us2.
#CONFIG YOUR_NAME = URI.encode("Kevin Bond") API_URL = "http://us2.api.mailchimp.com/1.3" API_KEY = 'mailchimp_api_key' FROM_ADDRESS = "kbond@voxeo.com" SUBJECT = URI.encode("You just received a text message!")The next piece of code will extract the text and the caller id from the SMS. The text will be the "content" of the email (the body) and the caller id will be the "from_name".
# Get the initial Text, and callerId initial_text = $currentCall.initialText caller_id = $currentCall.callerIDNow it's finally time to utilize the MailChimp API. This piece of code will send a request to get the list id, which is the specific number that corresponds to the list we made earlier. Once received, it is parsed into JSON and read as the variable list_id.
#Get the list ID that corresponds to your email id =Net::HTTP.get_response(URI.parse("#{API_URL}/?method=listsForEmail&apikey=#{API_KEY}&email_address=#{FROM_ADDRESS}")) list_id = JSON.parse(id.body)The next step in sending this email blast is to create a new email campaign. To do this, there are some parameters that need to be added to the URL. There are four in total, two variables - "api key" and "type of email" - and two arrays or hashes. The code below shows the two hashes ("content" and "options").
#This will be the body of your email, which is the text message content = "[text]=#{URI.encode(initial_text)}" #Setting the url variables options = "& options[list_id]=#{list_id[0]}& options[subject]=#{SUBJECT}& options[from_email]=#{FROM_ADDRESS}& options[from_name]=#{URI.encode(caller_id)}& options[to_name]=#{YOUR_NAME}".gsub("\n","")The other two parameters are just inserted individually. Below is the variable parameters, which holds all of the contents.
#Parameters will be the variable that goes inside of the url #To satisfy mailchimps method, you need 4 fields, the apikey, type of email, #options - which is an array or hash, and content - which is also an array or hash parameters = "apikey=#{API_KEY}&type=plaintext&options=#{options}&content#{content}"The next MailChimp API request creates a new email campaign, it doesn't actually send the emails. The request returns the campaign id, called cid, which is used to launch that campaign.
#This response creates a new campaign and returns the id for that campaign id = Net::HTTP.get_response(URI.parse("#{API_URL}/?method=campaignCreate&#{parameters}")) cid = id.body[1..10]Now that the email campaign is created, all that is left to do is send it. The code below sets the URL, creates a new HTTP post, and finally initiates the post. The result should return "true" if the campaign successfully sent.
#This is a post to send the email using the cid, which is the campaign id url = URI.parse("#{API_URL}/?method=campaignSendNow&apikey=#{API_KEY}&cid=#{cid}") req = Net::HTTP::Post.new("#{API_URL}/?method=campaignSendNow&apikey=#{API_KEY}&cid=#{cid}") res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }Once this executes, all of the email adresses from the list will receive the email with the text message contents.
Hope this was helpful, you can go here to view the app in full. Questions or concerns can be added in the comments, or opened as a forum post here.
©2011 The Tropo Blog. All Rights Reserved.
.Related posts:
Originally from All Voxeo Blogs Feed<



Leave a Reply