Python script for asterisk voicemail function

Python script for asterisk voicemail function

Closed - This job posting has been filled and work has been completed.

Job Description

This script, monitors a mailbox and if there is a new message, it makes a call to a pager number and enters a code (here the code is 301 which will appear on the pager). We want to make these changes:
After three minutes, the script checks the mailbox again and if the message is still there, it will make another call to pager 1 and pager 2 (both with pause the then the code 301)
After another three minutes the script checks the mailbox again and if the message is still there, it will make a call to pager 1 and pager 2 and pager 3.
Every three minutes the script checks and if the message is still there will page the three pagers until the message is removed from the mailbox.

During this situation if a new message is left in the same mailbox, the script should follow the above process from scratch: “and if there is a new message, it makes a call to a pager number and enters a code”




#!/bin/env python
import sys, os, logging, telnetlib

#Access to AMI on asterisk
MANAGER_USER = "admin"
MANAGER_PORT = "5038"
MANAGER_HOST = "127.0.0.1"
MANAGER_PASS = "amp111"

MONITOR_EXT = '301'
DST = '17037020373'

PAUSE_1 = '3'
PAGER_EXT = '301'
PAUSE_2 = '4'

#Call legs
LEG_1_CONTEXT = 'vm_notify_leg1'
LEG_2_CONTEXT = 'vm_notify_leg2'

#Log file
LOG_FILE = '/tmp/vm.log'
LOG_APP = 'dialer'

# Additional functions
def log(message):
try:
logger = logging.getLogger(LOG_APP)
hdlr = logging.FileHandler(LOG_FILE)
formatter = logging.Formatter('%(asctime)s: %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.WARNING)
logger.error(message)
except:
pass

EXT = sys.argv[2]

if sys.argv[2] == MONITOR_EXT:
log("VM for our %(EXT)s" %vars())

try:
t = telnetlib.Telnet()
t.open(MANAGER_HOST,MANAGER_PORT)
except Exception, e:
log("Error: %(e)s" %vars())
sys.exit()

EOL = '\r\n'
LOGIN = ['Action: login','Username: %s' % MANAGER_USER,'Secret: %s' % MANAGER_PASS, 'Events: on', '\r\n']

try:
t.write(EOL.join(LOGIN))
except Exception, e:
log("Error: %(e)s" %vars())
sys.exit()

DIALOUT = ['Action: Originate', 'Channel: Local/s@%(LEG_1_CONTEXT)s/n' %vars() ,
'Context: %(LEG_2_CONTEXT)s' %vars(), 'Extension: s' , 'Priority: 1',
'Variable: DST=%s' %DST,
'Variable: PAUSE_1=%s' %PAUSE_1,
'Variable: PAGER_EXT=%s' %PAGER_EXT,
'Variable: PAUSE_2=%s' %PAUSE_2,
'\r\n' ]
try:
log("Dialing ...")
t.write(EOL.join(DIALOUT))
except Exception,e:
log("Connection try out for AMI. Cause: %(e)s" %vars())


else:
pass

---
Skills: linux, import