Skip to main content

A collection of webhooks for Twilio using Flask.

Project description

twilio-webhooks

A collection of webhooks for Twilio using Flask.

Installation

pip install twilio-webhooks

Availible webhooks:

  • SMSCommand - When receiving a properly formated SMS runs a user-defined callable.
  • ReceiveFax - When receving a fax saves a pdf file to a user-defined path.

SMSCommand

Webhook URL format:

http://example.com/smscommand

You set this under "Phone Numbers / Manage Numbers / Active Numbers". Click your desired phone number. Look under a section called "Messaging", and with the "CONFIGURE WITH" drop-down menu select "Webhooks, TwiML Bins, Functions, Studio or Proxy". Now under the "A MESSAGE COMES IN" drop-down menu select "Webhook" and in the adjacent box place your URL.

Callback format:

The callable should accept two arguments.

  1. A string containing who the SMS is from in E.164 format.
  2. A string that is either emtpy or the arg (see below) sent in the SMS.

A return value will be sent as a reply.

SMS Format:

command [arg]

  • command (required) is a case-insensitive string that was assigned to a user-defined callable.
  • arg (optional) will be passed to the callable as the second argument.

Deployment:

Deploy this as you would any other Flask app. See Deployment Options for more information.

Example Code:

import psutil
import wakeonlan
from twilio_webhooks import SMSCommand

# Simple example

def cpu_usage(from_, arg):
    """Check CPU usage"""
    return f"CPU Usage: {psutil.cpu_percent()}%"

# Complex example

def wake_on_lan(from_, arg):
    """Use wake-on-lan to wake a computer"""
    # make sure the number is mine
    if from_ != "+12125551234":
        return
    computers = {"office": "ff.ff.ff.ff.ff.f1", "gaming": "ff.ff.ff.ff.ff.f2"}
    # Clean up arg in case I send a sloppy SMS
    arg = arg.strip().lower()
    if arg in computers:
        wakeonlan.send_magic_packet(computers[arg])
        return f'Computer "{arg}" is waking up.'


sc = SMSCommand("your_twilio_auth_token")
sc.assign("cpu", cpu_usage)
sc.assign("wol", wake_on_lan)

app = sc.wsgi()

Now if you send an SMS to the twilio number you selected earlier that says this (remember the command part is case-insensitive):

Cpu

you should get a reply like this

CPU Usage: 5.2%

or this:

Wol office

should reply:

Computer "office" is waking up.

ReceiveFax

Deployment:

Deploy this as you would any other Flask app. See Deployment Options for more information.

Example:

from twilio_webhooks import ReceiveFax

app = ReceiveFax("your_twilio_auth_token", '/path/to/save/pdf').wsgi()

Incoming faxes will now be saved to the path of your choice.

Notes:

  • Your twilio auth token can be found under "Dashboard / Settings / General". You must click it to view it.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

twilio-webhooks-0.1.0.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

twilio_webhooks-0.1.0-py3-none-any.whl (5.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page