1

I am developing a python script that will find the LAtitude and Longitude of a particular location and store it in a text file . The location will be sent through an SMS to the python script .I can do the later part of finding the latitude and longitude using 'geocode' library in python , but i'm not sure as to how to accept the data from SMS .

I have worked with the TWILIO module previously and i know how to Send SMS via Python script to a phone number using it . But I want to know the process of receiving SMS message sent from a phone number and then using that message body in the python script for further operation .

This is how I have been sending SMS to phone numbers -

import os
from twilio.rest import TwilioRestClient
accountSID = '****'
authToken = '***'
twilioCli = TwilioRestClient(accountSID, authToken)
myTwilioNumber = '+1 ***'
myCellPhone = '+***'
message = twilioCli.messages.create(body=string, from_=myTwilioNumber,  to=myCellPhone)

If you can provide a solution to do the opposite of the above process i.e receive an SMS instead of sending it , through TWILIO service , itll be very helpful.

2 Answers 2

3

If you are familiar with Twilio, then you can also use it to receive short messages. Just register a number (this will be our receiver) and add webhook in Messaging -> A message comes in section. Your webhook is an end-point which implements getting messages on POST action. Then you can process received messages as you want.

You can find a detailed tutorial on official website.

Edit:

What is the flow?

  1. You prepare an application (in tutorial - implemented in flask). You have to run it on application server. Your domain is, for example, john-smith.com.
  2. You register an end-point as webhook. Your end-point is john-smith.com/sms.
  3. You send a short message to your number registered in Twilio.
  4. When the short messages come, it's handled by Twilio and sent as POST request to your application to URL specified as webhook.

This is your python code (a flask application) from tutorial. This is the place where you extract number and message. This is also your application.

from flask import Flask, request 
from twilio import twiml

app = Flask(__name__)

@app.route('/sms', methods=['POST']) 
def sms():
    number = request.form['From']
    message_body = request.form['Body']

    resp = twiml.Response()
    resp.message('Hello {}, you said: {}'.format(number, message_body))
    return str(resp)   

if __name__ == '__main__':
    app.run()

A message_body variable is received message of SMS. You can do whatever you want with it.

Sign up to request clarification or add additional context in comments.

3 Comments

I am a bit confused with that webhook part . what do i specify there ? Also once i add the required parameters in the webhook section , how do i extract it in my python code ? any sample examples will be appreciated.
@johnsmith Hello, I have edited an answer, is it clean now? :)
Thanks this helps . I'll update incase of any problems .
1

A simple code for sms sending in Python:

from smsapi import SmsApi

apikey = "675031878xxxxxxxxx" sender = "SEDEMO"

sms = SmsApi(apikey, sender)

sms = sms.send_sms(

recipient="902000xxxx",

message="test message"

)

Make sure you have copied smsapi.py class file from https://github.com/spring-edge/ Github repository and placed in same directory. In above code recipient is the destination mobile number and sms body as message.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.