Day 14 of the Anvil Advent Calendar

Build a web app every day until Christmas, with nothing but Python!

Get a robot to call your Aunt

Today we have an app for those of you who can never quite find the time to call up your least-favourite Aunt and wish her ‘Merry Christmas’. Wouldn’t it be useful if there were an app for that? No? Well we’ve built one anyway.

Enter her number and a robot voice will wish her a Merry Christmas over the phone:

You can get a copy of the app’s source code here:

How it works

The Twilio API makes automating voice calls a breeze. You’ll need to start by registering with Twilio and setting up a phone number. It only took me a couple of minutes to do, then I stored the Client ID and Secret Key in my Anvil app using the Anvil Secrets Service.

Once that setup was done, it was the work of just a few more minutes to set up a UI for entering my Aunt’s phone number, a server function to place the call, and an HTTP API callback endpoint to script the call when it’s answered:

@anvil.server.callable
def make_call(to_number):
  
  # Load the Twilio-account-specific details from the Anvil Secrets Service. You will need to set these values to use the app yourself!
  account_sid = anvil.secrets.get_secret('twilio_account_sid')
  auth_token = anvil.secrets.get_secret('twilio_auth_token')
  from_number = anvil.secrets.get_secret('twilio_number')

  # Make a REST API request to place the voice call, passing a callback URL where Twilio will get its instructions when the call connects.
  anvil.http.request(
    f"https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Calls.json",
    data={
      "Url": f"{anvil.server.get_api_origin()}/call-connected",
      "From": from_number,
      "To": to_number,
    },
    method="POST",
    username=account_sid,
    password=auth_token
  )

    
    
# This function provides the API callback endpoint where Twilio gets its instructions when an outgoing call connects.    
@anvil.server.http_endpoint("/call-connected")
def call_connected(**kwargs):
  return """
    <Response>
        <Say>Dear Auntie, I hope you have a very merry Christmas. Sorry I didn't have time to phone you myself. Lots of love, me.</Say>
    </Response>
  """

You can clone the app to adapt it yourself here:

I’m sure my Aunt would be delighted to get a call with my automated Christmas wishes, but your mileage may vary. Just don’t blame us if you receive particularly hideous socks next year.


Give the Gift of Python

Share this post: