A function to send a verification codes using Twilio's Verify API with OpenFaaS.
Refer to twilio-check-verify to check verification codes.
This function requests Twilio to send a Verification Code via a channel:
- sms
- call
- email (requires extra setup in Twilio, which is not covered here)
When the verification code arrives (which is super fast) it looks like this:
Your <COOL_APP_NAME> verification code is 123456.
Let's pretend you have another function written in Python that does user profile updates, and it handles the following scenario.
Given a request to update a user's profile, when the profile update includes opting into receive notifications via SMS, then we want to send a verification code using SMS to the user's phone.
Sending a request to twilio-send-verify
to ask Twilio to send a verification code may look like:
# I opted to use async, so a failure on the Twilio side doesn't cause my user update to fail
url = "http://gateway.openfaas.svc.cluster.local:8080/async-function/twilio-send-verify.openfaas-fn"
body = {"To": "15551234567", "Channel": "sms"}
response = requests.post(url, json=body)
response.raise_for_status()
This will result in the end-user receiving a message with a verification code from Twilio (you can customize the message in Twilio).
-
Create a Verify service in Twilio console. Document the
Service ID
, you'll need it in step 4. -
Save the
AccountSid
andAuthToken
from Twilio as a secret in Kubernetes
# get these from https://www.twilio.com/console
TWILIO_ACCOUNT_SID=<YOUR_ACCOUNT_SID>
TWILIO_AUTH_TOKEN=<YOUR_AUTH_TOKEN>
faas-cli secret create twilio-account-sid --from-literal=$TWILIO_ACCOUNT_SID
faas-cli secret create twilio-auth-token --from-literal=$TWILIO_AUTH_TOKEN
- Make sure you have the
csharp-httprequest
template on your machine
faas-cli template store pull csharp-httprequest
- Configue it to your liking and ship it to OpenFaaS
version: 1.0
provider:
name: openfaas
gateway: http://127.0.0.1:8080
functions:
twilio-send-verify:
lang: csharp-httprequest
handler: ./twilio-send-verify
secrets:
- twilio-account-sid
- twilio-auth-token
labels:
com.openfaas.scale.zero: false
environment:
twilio_verify_endpoint: https://verify.twilio.com/v2/Services/<YOUR_VERIFY_SERVICE_ID>/Verifications