I usually receive various reminder notifications by email or push notification, but I tend to overlook it, so I thought it would be nice if you could notify me of Google Calendar reminders by voice.
I will write the general flow.
There are many steps, but except for writing Lambda code, I just clicked on the screen.
First, sign up at Twilio.
After signing up and logging in, go to the "Console Dashboard" and make a note of the contents of "ACCOUNT SID" and "AUTH TOKEN". This will be used later in the Lambda function.
Then purchase the phone number you need to make the call. You need to charge for the purchase of the phone number, so charge the points in advance on the "Billing" page.
After charging points, go from "Console Dashboard" to "Buy a Number". Since the phone number search screen is displayed
You should see a list of available phone numbers in the search results, so buy the number you like.
When you purchase a phone number, you should see the phone number you purchased in "Phone Number" and "Active Phone Number". This number is also used in the Lambda function, so keep it in mind.
___ * If you get a Twilio account, you will get one phone number for a free trial, so that may be fine. I've charged so quickly that I'm not sure how many calls I can make for free. ___
Notification settings from Google Calendar to Gmail are easy, so I won't write them.
Gmail sets the condition to "from: ([email protected])" etc. in "Settings"-> "Filters and blocked addresses"-> "Create new filter" and forwards to the following address It is OK if you set the forwarding address with.
For more information on AWS SES settings, please refer to the following articles in detail.
AWS Lambda Next, let's create a Lambda function.
First, write the code locally.
$ mkdir voice_reminder
$ cd voice_reminder
$ vim lambda_function.py
lambda_function.py
# -*- coding: utf-8 -*-
import os
import re
import json
import email
import base64
import urllib
import boto3
from twilio.rest import Client
account_sid = os.environ['ACCOUNT_SID']
auth_token = os.environ['AUTH_TOKEN']
#Set the Account SID and Auth Token on your Twilio account page
client = Client(account_sid, auth_token)
s3 = boto3.client('s3')
def lambda_handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8')
titleText = None
timeText = None
try:
#Extract the mail data transferred from Gmail
response = s3.get_object(Bucket=bucket, Key=key)
b = email.message_from_string(response['Body'].read())
titleRegex = re.compile("title: (.+)")
timeRegex = re.compile("Date and time: \d{4}/\d{2}/\d{2} \(.+\) (\d{2}\:\d{2}) ~ (\d{2}\:\d{2}) .+")
if b.is_multipart():
for payload in b.get_payload():
line = base64.b64decode(payload.get_payload())
#Extract the title and date and time of the email body
title = titleRegex.search(line)
if title is not None:
titleText = title.group(1)
time = timeRegex.search(line)
if time is not None:
timeText = time.group(1)
except Exception as e:
print(e)
raise e
if titleText is not None and timeText is not None:
twiml = "<Response><Pause length='3'/><Say voice='woman' language='ja-jp'>This is a reminder. today%From s%There is a schedule for s.</Say></Response>" % (timeText, titleText)
#Remind text(Made TwiML)To Twimlets Echo and call the designated phone number
call = client.api.account.calls\
.create(to=os.environ['REMIND_TO'],
from_=os.environ['REMIND_FROM'],
url="http://twimlets.com/echo?Twiml="+urllib.quote(twiml.encode("utf-8")))
print(call.sid)
After writing the code, install the SDK called twilio-python in the same directory as your Lambda function.
$ pip install twilio -t /path/to/voice_reminder
After installing the SDK, create a Zip file.
$ zip -r voice_reminder.zip .
Next, create a Lambda function.
"Creating a Lambda function"
Select "Blank Function"
In "Trigger settings", specify the S3 Bucket created when setting SES.
Select the trigger as S3
Select the pre-created S3 in "Bucket"
Set "Event Type" to "Put"
Set the prefix (if the Object key prefix was set when setting SES, make it the same)
Check the activation of the trigger
Set each item in "Function settings".
Enter the name of the function
Select Python 2.7 for "Runtime"
Select "Upload .ZIP file" in "Code entry type" and select the Zip file created earlier.
Enter the following items in the environment variables.
Set "ACCOUNT_SID" to the Account SID obtained by Twilio
Set "AUTH_TOKEN" to the Auth Token you got with Twilio
Set the phone number you want to remind "REMIND_TO" (+8190 ******** etc.)
Set "REMIND_FROM" to the phone number you purchased from Twilio (+8150 ********, etc.)
Specify "Role" and "Existing role" as appropriate. (Since S3 access, make it in advance with IAM)
Set "Timeout" in "Detailed Settings" to about 30 seconds.
Create a function
Then set an appointment in Google Calendar and see if Twilio sends a voice reminder.
I will not explain Google Calendar in particular, but you can set the notification timing by selecting "Edit notification"-> "Notification of appointment" from "Calendar settings", so please set your favorite time.
When I first touched Twilio this time, I felt that it would be interesting to combine it with a serverless architecture like AWS Lambda (now).
Recommended Posts