2

OK possible noob question here: While learning Django, I thought it might be cool to explore telephony with Twilio. My immediate goal is to create a page with a button that, when clicked, causes a "Hello World" SMS to be sent to my phone. After sorting that out I have some ideas for cooler stuff.

I've completed several Django tutorials so far, and made a few little apps with simple views. But nothing I've learned has particularly shed any light on how to do something like this. I've also investigated (and installed) the Django-Twilio app and the Twilio Python Helper Library, but the docs for neither of these show how to send "hello world" SMS's.

Can anyone point out a resource that might show how to do this? Or, if it's trivially easy, just post some example code?

Edit in response to Kevin Burke:

Thanks for getting back to me, Kevin. After modifying my urls.py to include:

urlpatterns = patterns('',
# ...
url(r'^sms/$', 'django_twilio.views.sms', {
    'message': 'Hello world',
    'to': '+12223334444',
    'sender': '+18882223333',
    'status_callback': '/sms/completed/',
    }, name = 'send_message'),
# ...
)

and pointing my browser at

http://127.0.0.1:8000/sms/

the following error arises:

Exception Type: TwimlException at /sms/
Exception Value: Invalid method parameter, must be 'GET' or 'POST'

Perhaps this is because I have failed to make appropriate modifications to the view. But I don't have a good way of figuring out what I'm doing wrong from the minimal examples in the tutorial. /Edit

1
  • It's a little tricky to help out without knowing more about your situation. It's definitely possible to send "Hello World" using the twilio-python helper library, see for example twilio.com/docs/python/install, but without knowing where you are getting stuck it's tricky to offer more feedback. Commented Sep 23, 2013 at 4:05

3 Answers 3

3

twilio employee here. The problem here is that the built in views for django_twilio run through a series of validation checks to make sure they're receiving content from twilio.com and only twilio.com. This is a security measure built into django-twilio.

There are two things you can do:

  1. Make sure your settings.DEBUG = True in your Django settings, this will turn off the validation. You can then send a cURL request on your local machine whilst it is running like this in your terminal:

    $ curl http://localhost:8000/sms/
    

    This should return some TWiML like so:

    <Response><Sms>Hello world</Sms></Response>
    
  2. When you're running this online and you want to do to test this, set up your twilio number to point to http://mywebsite.com/sms/ and text the number. Ensure that settings.DEBUG = False and you should get back a message.

If you have anymore problems, let me know.

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

Comments

0

Here's the official docs: django-twilio official docs. More specifically, read this part about sending SMS: Sending sms messages

Comments

0

Here is a simple solution :

django startproject projectname

urls.py

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('message_api.urls')),
]

settings.py

TWILIO_ACCOUNT_SID = TWILIO_ACCOUNT_SID
TWILIO_AUTH_TOKEN = TWILIO_AUTH_TOKEN
DJANGO_TWILIO_FORGERY_PROTECTION = False
DJANGO_TWILIO_BLACKLIST_CHECK = True

Start new application

python manage.py startapp appname

Inside the app folder:`

urls.py

from django.conf.urls import url
import django_twilio
from . import views

urlpatterns = [
     url(r'^api/$', views.home),
     url(r'^send/', views.sms),
]

views.py

from django.shortcuts import render
from twilio.rest import Client
from twilio_api import settings

def home(request):
    return render(request, 'index.html', {})

def sms(request):
    client = Client(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)

    message = client.messages.create(to='TO NUMBER', from_='YOUR TWILIO NUMBER', body='This message is sent through twilio api using django framework by akshat.')

    print(message.sid)

    return render(request, 'thankyou.html')

Make a templates directory inside your app folder

index.html

<body>
<a href="/send/"><button class="btn btn-outline-primary">Send Message</button></a>
</body>

thankyou.html

<body>
<h1>Success</h1>
</body>

`

1 Comment

where the message sends action done. Is it in print(message.sid) ??

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.