-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclick2dial.py
146 lines (114 loc) · 4.33 KB
/
click2dial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
"""
SYNOPSIS
TODO click2dial.py [-h,--help] [-v,--verbose] [--version]
DESCRIPTION
Proivde a URL to iniate calls to a desired number, and internal extension.
Usage: http://asterisk-pbx:port?did=number2dial&i_exten=1234&key=licensekey&m=json&crm=ns
Only the did and i_exten are required at this point.
This version will based on the usage of asterisk .call files, to start the process. I hope
to possibly migrate to using AMI options with asterisks built in http support, but maybe
that is not what is best. For now this is dirty quick and should be effective.
EXAMPLES
click2dial.py --destination=5555551212 --internal=1000
--destination=5551212 The number you wish to dial.
--internal=1000 Your extension number.
--user=asterisk The user Asterisk PBX is running under.
--accountcode=TESTING The accountcode for asterisk CDR.
EXIT STATUS
TODO: List exit codes
AUTHOR
Ben <ben@voice1 dot me>
LICENSE
This script is for internal usage only.
VERSION
$Id$
"""
import sys
import os
import traceback
import optparse
import time
import re
#from pexpect import run, spawn
import logging
logname = 'click2dial'
logger = logging.getLogger(logname)
hdlr = logging.FileHandler('/var/log/%s.log' % (logname) )
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.WARNING)
logger.setLevel(logging.DEBUG)
logger.info('Starting logger')
# We need our magical pycall library to create our call files all nice
# and python like. :)
try:
logger.info('Attempting to load pycall library.')
import pycall
except Exception, e:
logger.warn('Unable to load pycall library. Please visit http://pycall.org/.')
logger.warn('System unable to proceed.')
sys.exit(1)
def mkCall():
"""Wrapper around mkCallFile, and mkCallAMI functions to use what ever is best"""
mkCallFile()
def mkCallFile(number2dial=None, extension=None, **kwargs):
"""Creates call file for us"""
# Removes all non alpha chars from the number2dial
channel_vars = {'num2dial':number2dial,'accountcode':'TESTING'}
call = pycall.Call('SIP/flowroute/%s' %(extension.strip()),
variables=channel_vars,
account='TESTING',
callerid='Testing <%s000000>' %(extension))
# We want to send this call to an existing context in asterisk
context = pycall.Context('click2dial', 's', '1')
# Build the call file in memory
cfile = pycall.CallFile(call, context, user='asterisk')
# Send the call file to the spool
cfile.spool()
return cfile.contents
def main ():
global options, args
# TODO: Do something more interesting here...
print options
print args
if __name__ == '__main__':
try:
start_time = time.time()
parser = optparse.OptionParser(formatter=optparse.TitledHelpFormatter(),
usage=globals()['__doc__'],
version='0.1')
parser.add_option ('-v',
'--verbose',
action='store_true',
default=False,
help='verbose output')
parser.add_option ('-d',
'--destination',
action='store_true',
default=False,
help='Called party telephone number.')
parser.add_option ('-e',
'--exten',
action='store_true',
default=False,
help='Internal extension.')
(options, args) = parser.parse_args()
#if len(args) < 1:
# parser.error ('missing argument')
if options.verbose: print time.asctime()
main()
if options.verbose: print time.asctime()
if options.verbose: print 'TOTAL TIME IN MINUTES:',
if options.verbose: print (time.time() - start_time) / 60.0
sys.exit(0)
except KeyboardInterrupt, e: # Ctrl-C
raise e
except SystemExit, e: # sys.exit()
raise e
except Exception, e:
print 'ERROR, UNEXPECTED EXCEPTION'
print str(e)
traceback.print_exc()
os._exit(1)