Skip to content

Commit ed22187

Browse files
committed
working on code for new blog post
1 parent 9332313 commit ed22187

File tree

17 files changed

+292
-2
lines changed

17 files changed

+292
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,4 @@ monitor-django-apps/djmonitor/db.sqlite3
9999
flask-auth-okta/openidconnect_secrets.json
100100
flask-auth-okta/.env
101101
auth-existing-flask-app/completed/openidconnect_secrets.json
102+
django-accurate-twilio-voice-transcriptions/djtranscribe/.env

accurate-twilio-voice-call-recording-transcriptions-assemblyai/app.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
BASE_URL = os.getenv("BASE_URL")
1313
twiml_instructions_url = "{}/record".format(BASE_URL)
14-
recording_callback_url = "{}/callback".format(BASE_URL)
15-
1614
twilio_phone_number = os.getenv("TWILIO_PHONE_NUMBER")
1715

1816

django-accurate-twilio-voice-transcriptions/djtranscribe/caller/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class CallerConfig(AppConfig):
5+
name = 'caller'

django-accurate-twilio-voice-transcriptions/djtranscribe/caller/migrations/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.db import models
2+
3+
# Create your models here.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# djtranscribe/caller/urls.py
2+
from django.conf.urls import url
3+
from . import views
4+
5+
urlpatterns = [
6+
url(r'dial/(\d+)/$', views.dial, name="dial"),
7+
url(r'record/$', views.record_twiml, name="record-twiml"),
8+
url(r'get-recording-url/([A-Za-z0-9]+)/$', views.get_recording_url,
9+
name='recording-url'),
10+
]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from django.conf import settings
2+
from django.http import HttpResponse
3+
from django.views.decorators.csrf import csrf_exempt
4+
5+
from twilio.rest import Client
6+
from twilio.twiml.voice_response import VoiceResponse
7+
8+
9+
10+
11+
def dial(request, phone_number):
12+
"""Dials an outbound phone call to the number in the URL. Just
13+
as a heads up you will never want to leave a URL like this exposed
14+
without authentication and further phone number format verification.
15+
phone_number should be just the digits with the country code first,
16+
for example 14155559812."""
17+
# pulls credentials from environment variables
18+
twilio_client = Client()
19+
call = twilio_client.calls.create(
20+
to='+{}'.format(phone_number),
21+
from_=settings.TWILIO_PHONE_NUMBER,
22+
url=settings.TWIML_INSTRUCTIONS_URL,
23+
)
24+
print(call.sid)
25+
return HttpResponse("dialing +{}. call SID is: {}".format(
26+
phone_number, call.sid))
27+
28+
29+
@csrf_exempt
30+
def record_twiml(request):
31+
"""Returns TwiML which prompts the caller to record a message"""
32+
# Start our TwiML response
33+
response = VoiceResponse()
34+
35+
# Use <Say> to give the caller some instructions
36+
response.say('Ahoy! Call recording starts now.')
37+
38+
# Use <Record> to record the caller's message
39+
response.record()
40+
41+
# End the call with <Hangup>
42+
response.hangup()
43+
44+
return HttpResponse(str(response), content_type='application/xml')
45+
46+
47+
def get_recording_url(request, call_sid):
48+
# pulls credentials from environment variables
49+
twilio_client = Client()
50+
recording_urls = ""
51+
call = twilio_client.calls.get(call_sid)
52+
for r in call.recordings.list():
53+
recording_urls="\n".join([recording_urls, "".join(['https://api.twilio.com', r.uri])])
54+
return HttpResponse(str(recording_urls), 200)

0 commit comments

Comments
 (0)