Skip to content

Send verify codes via Twilio's Verify API using OpenFaas

License

Notifications You must be signed in to change notification settings

kylos101/twilio-send-verify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

twilio-send-verify

A function to send a verification codes using Twilio's Verify API with OpenFaaS.

Refer to twilio-check-verify to check verification codes.

Overview

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.

A Sample Use Case

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).

Installation

  1. Create a Verify service in Twilio console. Document the Service ID, you'll need it in step 4.

  2. Save the AccountSid and AuthToken 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
  1. Make sure you have the csharp-httprequest template on your machine
faas-cli template store pull csharp-httprequest
  1. 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

About

Send verify codes via Twilio's Verify API using OpenFaas

Topics

Resources

License

Stars

Watchers

Forks

Languages