Skip to main content

Email2callback

About

A Python script that triggers a call from an e-mail message.

Click here to expand Table of Contents

Steps

These instructions assume you are using postfix on a debian based system. Please adapt to your own system appropriately.

To use the following system, send an email to callback@FQDN, with either the subject of "callback NUMERIC" or somewhere in the body with "callback NUMERIC". This will then dial the number NUMERIC based on the dialplan of the email2callback user, for example "callback 1800123123".

1. Install postfix, sudo, python, and have postfix accept mail to local unix users.

2. Make sure 'freepy' (found in the scripts/socket directory) is setup correctly, which may mean installation of the SMC python library as per the README/INSTALL of freepy.

3. Create an extension, with username of email2callback, and choose an appropriate password for the user. Please set the password within the python script below.

4. Add the following to /etc/sudoers, which will allow the nobody user (which postfix uses for spawning scripts from /etc/aliases) to execute the script owned by freeswitch:

nobody	ALL=NOPASSWD: /opt/freeswitch/scripts/socket/email2callback.py

5. Add the following to /etc/aliases, and then run newaliases:

callback: | /usr/local/bin/email2callback.sh

6. Create the file /usr/local/bin/email2callback.sh:

/usr/local/bin/email2callback.sh

#!/bin/sh
sudo /opt/freeswitch/scripts/socket/email2callback.py

7. Create the file /opt/freeswitch/scripts/socket/email2callback.py:

/opt/freeswitch/scripts/socket/email2callback.py

#!/usr/bin/python
# script to parse email for phonenumber in subject or body, and call back with extension
# Replace DOMAIN with the value of $${domain}
# Replace EXTENSION with the value of the application (&rxfax()) or extension (5000)
# Replace PASSWORD with the password for the email2callback user extension

import sys
import os
import email.Parser
import types
import re
import syslog

sys.path.append('/opt/freeswitch/scripts/socket')
sys.path.append('/opt/freeswitch/scripts/socket/freepy')

from fshelper import *

def initiateCallback():
outgoing_number="unknown"

# first try to parse the incoming email
message = email.message_from_file(sys.stdin)

#
# process the message here
#
syslog.syslog('Received mail from "%s", subject: "%s"' % (message.get('From'), message.get('Subject') ))

subject = message.get('Subject')
m = re.match('callback (\d*)', subject)
if m:
outgoing_number = m.group(1)

msg = message.get_payload()
m = re.match('callback (\d*)', msg)
if m:
outgoing_number = m.group(1)

syslog.syslog('Making outgoing call to "%s"' % outgoing_number)

fshelper = FsHelper(host="127.0.0.1",
passwd="ClueCon",
port=8021)

def worked(result):
print "Originate succeeded: %s" % result
reactor.stop()

def failed(failure):
print "failed: %s" % failure
reactor.stop()


dest_ext_app = "EXTENSION"
party2dial="[sip_auth_username=email2callback,sip_auth_password=PASSWORD]sofia/external/%s@DOMAIN" % outgoing_number
d2 = fshelper.originate(party2dial=party2dial,
dest_ext_app=dest_ext_app,
bgapi=True)

d2.addCallback(worked)
d2.addErrback(failed)
reactor.run()


if __name__=="__main__":
initiateCallback()