User:Birgit bachler/Freeswitch/recordplay.py
< User:Birgit bachler | Freeswitch
Revision as of 18:41, 1 May 2011 by Birgit bachler (talk | contribs)
import os
from freeswitch import *
import sqlite3
sql = sqlite3.connect("/home/merglind/var/hello.db")
c = sql.cursor()
# HANGUP HOOK
#
# session is a session object
# what is "hangup" or "transfer"
# if you pass an extra arg to setInputCallback then append 'arg' to get that value
#
# WARNING: known bugs with hangup hooks, use with extreme caution
def hangup_hook(session, what):
consoleLog("info","hangup hook for %s!!\n\n" % what)
return
# INPUT CALLBACK
#
# session is a session object
# what is "dtmf" or "event"
# obj is a dtmf object or an event object depending on the 'what' var.
# if you pass an extra arg to setInputCallback then append 'arg' to get that value
# def input_callback(session, what, obj, arg):
def input_callback(session, what, obj):
if (what == "dtmf"):
consoleLog("info", what + " " + obj.digit + "\n")
if (obj.digit == "#"):
return "break"
else:
consoleLog("info", what + " " + obj.serialize() + "\n")
return "pause"
# APPLICATION
#
# default name for apps is "handler" it can be overridden with <modname>::<function>
# session is a session object
# args is all the args passed after the module name
def handler(session, args):
# session.answer()
session.setHangupHook(hangup_hook)
session.setInputCallback(input_callback)
# session.execute("playback", session.getVariable("hold_music"))
digits = session.getVariable("digits")
caller = session.getVariable("caller_id_number")
caller_name = session.getVariable("caller_id_name")
c.execute('INSERT INTO calls (caller, start_time) values (%s, DATETIME(\"NOW\"))' % caller)
c.execute("SELECT id FROM calls ORDER BY id DESC LIMIT 1")
call_id = c.fetchone()[0]
sql.commit()
c.execute("SELECT nr FROM extensions WHERE nr=%s" % digits)
consoleLog("info", "caller %s (%s) entered: %s \n" % (caller, caller_name, digits))
if len(c.fetchall()):
# if we are here, caller called a valid extension
consoleLog("info", "UPDATE calls SET extensions=%s WHERE id=%d \n" % (digits, call_id))
c.execute("UPDATE calls SET extension=%s WHERE id=%d" % (digits, call_id))
sql.commit()
# check how many messages are recorded under this extension
c.execute("SELECT recording FROM calls WHERE extension=%s" % digits)
msg_ids = []
for record in c.fetchall():
msg_id = record[0]
if msg_id == None:
continue
msg_ids.append(msg_id)
if len(msg_ids):
# we have messages
for msg_id in msg_ids:
#get filename
c.execute("SELECT filename FROM recordings WHERE id=%d" % msg_id)
filename = str(c.fetchone()[0])
consoleLog("info", "streaming %s" % filename)
# play the message here...
session.streamFile(filename)
# record a message
# session:recordFile(file_name, max_len, silence_threshold, silence_secs)
rec_filename = "%s-%d.wav" % (digits, len(msg_ids))
consoleLog("info", "rec_filename = %s-%d.wav \n" % (digits, len(msg_ids)))
c.execute('INSERT INTO recordings (filename, start_time) values (\"%s\", DATETIME(\"NOW\"))' % rec_filename)
c.execute("SELECT id FROM recordings ORDER BY id DESC LIMIT 1")
rec_id = c.fetchone()[0]
c.execute("UPDATE calls SET recording=%d WHERE id=%d" % (rec_id, call_id))
sql.commit()
# record file here...
session.recordFile(rec_filename, 30000, 10, 10)
c.execute('UPDATE recordings SET end_time=DATETIME(\"NOW\") WHERE id=%d' % rec_id)
sql.commit()
c.execute('UPDATE calls SET end_time=DATETIME(\"NOW\") WHERE id=%d' % call_id)
sql.commit()
# close the cursor and the DB-file
c.close()
sql.close()