Twilio: receive SMS and do something with body content

It's hard for me to understand Twilio. I read the documents and plan to read them again, but I was hoping for some pointers. I am using Ruby on Rails for my application.

What I want to do is get a text message from the user with some kind of text text. Then, I want to save this text in my model somehow. How should I do it?

Thanks!

+4
source share
4 answers

The Twilio number may be associated with a callback URL that it will send a POST request when it receives an SMS message. This callback URL must be configured to point to the controller in your application that you will use to process SMS. From there, you can simply read the hash of the parameters to get detailed information about the received SMS message. Note: params['From'] and params['Body'] . Save the text from these parameters to any model you like!

Twilio Callback URL

 http<s>://<your domain.com>/sms 

Route

 resource :sms, only: :create 

controller

 class SmsController < ApplicationController skip_before_filter :force_ssl # You may need this if your app uses https normally def create # Do something with params['From'] -- contains the phone number the SMS came from # Do something with params['Body'] -- contains the text sent in the SMS # <Reponse/> is the minimum to indicate a "no response" from Twilio render xml: "<Response/>" end end 
+5
source

user2515526,

I recently set up my first Rails application that uses some Twilio features - primarily for receiving, processing, and replying to SMS messages. And I agree: in terms of noobase, the Twillio documentation can be explicitly cleared ^^.

I found this git repo to be extremely useful. The guy, in fact, spends almost everything you need to know to get up and run.

+1
source

You can receive the body of an SMS message in the same way as you receive content from a text field in a web application. In python, it will be

 mybody = request.args.get('body') 
0
source

If you are using Node Express, here is a snippet that worked for me:

 app.post('/respondToMessage', function(req, res) { var twiml = new twilio.TwimlResponse(); console.log('sms message =', req.body.Body); twiml.message('respondToMessage'); res.type('text/xml'); res.send(twiml.toString()); }); 

You are analyzing the Body JSON attribute from a response, which usually looks like this:

 {   "ToCountry": "",   "ToState": "",   "SmsMessageSid": "",   "NumMedia": "",   "ToCity": "",   "FromZip": "",   "SmsSid": "",   "FromState": "",   "SmsStatus": "",   "FromCity": "",   "Body": "The attribute you're parsing for",   "FromCountry": "",   "To": "",   "MessagingServiceSid": "",   "ToZip": "",   "NumSegments": "",   "MessageSid": "",   "AccountSid": "",   "From": "+",   "ApiVersion": "" } 
0
source

Source: https://habr.com/ru/post/1500063/


All Articles