Skip to content

Instantly share code, notes, and snippets.

@electroniceel
Last active January 7, 2020 18:41
Show Gist options
  • Save electroniceel/5498bc608fbbb7de65ac8485de1a91c7 to your computer and use it in GitHub Desktop.
Save electroniceel/5498bc608fbbb7de65ac8485de1a91c7 to your computer and use it in GitHub Desktop.
asterisk cti dial with callfiles
from flask import *
import re
import json
import os
import tempfile
import shutil
import stat
script_dir = os.path.dirname(os.path.realpath(__file__))
app = Flask(__name__)
CALLFILE_TMPDIR = "/var/spool/asterisk/callfile-tmp"
CALLFILE_ACTIONDIR = "/var/spool/asterisk/outgoing/"
@app.route('/dial', methods = ['GET'])
def dial():
# sanitize input first
number = request.args.get('number', type = str)
number = number.replace("/", "")
number = number.replace("-", "")
number = number.replace(".", "")
number = number.replace(" ", "")
number = number.replace("(", "")
number = number.replace(")", "")
if re.fullmatch(r"^\+?[0-9]{2,}$", number) is None:
return render_template("result_error.html",errmsg='Illegal number')
extension = request.args.get('extension', type = str)
callfile_data = render_template("callfile", extension=extension, number=number)
with tempfile.NamedTemporaryFile(dir=CALLFILE_TMPDIR,prefix="cti_call.",delete=False) as fp:
fp.write(callfile_data.encode("UTF-8"))
fp.close()
shutil.chown(fp.name,group="asterisk")
os.chmod(fp.name,stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH)
os.rename(fp.name, os.path.join(CALLFILE_ACTIONDIR,os.path.basename(fp.name)))
return render_template("success_close.html")
Channel: Channel: PJSIP/{{ extension }}
Callerid: "CTI-Call" <{{ number }}@localhost>
Extension: {{ number }}
Context: from-internal
WaitTime: 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment