This post was featured on PyCoders Weekly #157.
Recently I played with Twilio API. Their service is mind-blowingly simple - it took me about 50 lines of Python code to set up a MVP. No, I’m not going to start a Twitter-meets-audio kind of startup - you can steal the idea. Basically I just have to call a specific number, tell my message and the audio goes to my website. You can find the humblingly minimal audioblogging platform here: https://twog.herokuapp.com/.
##Okay, how can I get this to work? The shortest way is to follow the instructions on my Github repository, which I’m copying here as well.
- Create a free Twilio account and buy a voice number.
- Clone this repository.
- Change
AUTH_TOKEN
andACCOUNT_SID
inconfig.py
. You can find them under your account settings - Create a Heroku app and push your code into the app. If you’re not familiar with Heroku I suggest starting here.
- Change the request url in your manage number page. This should be in the form of yourappname.herokuapp.com/new.
- Call the number in manage number to record your first post.
- Yay! You have a voice blog!
P.S. Twilio service is not free, but there is an initial 30$ credit, which should be enough for playing with their service. You can also delete posts from Twilio website.
What’s under the hood?
It’s built with Flask and Bootstrap and all the logic is in app.py
. When we call our magic number Twilio accesses the yourappname.herokuapp.com/new and get’s instructions in TwiML, which is a Twilio’s markup language. Basically the view checks if the call is made from your number and after you have pressed 1, the yourappname.herokuapp.com/handle-key is accessed.
In handle-key Twilio gets instructions of what to say to you and what to do after the call has finished.
If your call reaches 10 seconds, you’re redirected to /handle-recording
, and informed that your post is now live.
Now we only need a view for displaying the posts:
It’s very straightforward - we fetch recordings and pass them into render_template
function, which then produces the necessary html for displaying all the posts we have created. So that’s that. Simple, huh?
https://github.com/techstonia/twog