User:Birgit bachler/Freeswitch/recordplay.py: Difference between revisions
(Created page with "<source lang="python"> import os from freeswitch import * import sqlite3 sql = sqlite3.connect("/home/merglind/var/hello.db") c = sql.cursor() # HANGUP HOOK # # session is a se...") |
No edit summary |
||
Line 59: | Line 59: | ||
if len(c.fetchall()): | if len(c.fetchall()): | ||
# if we are here, caller called a valid extension | # if we are here, caller called a valid extension | ||
consoleLog("info", "UPDATE calls SET extensions=%s WHERE id=%d \n" % (digits, call_id)) | 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)) | c.execute("UPDATE calls SET extension=%s WHERE id=%d" % (digits, call_id)) | ||
sql.commit() | sql.commit() | ||
Line 79: | Line 80: | ||
c.execute("SELECT filename FROM recordings WHERE id=%d" % msg_id) | c.execute("SELECT filename FROM recordings WHERE id=%d" % msg_id) | ||
filename = c.fetchone()[0] | filename = str(c.fetchone()[0]) | ||
# play the message here... | consoleLog("info", "streaming %s" % filename) | ||
# play the message here... | |||
session.streamFile(filename) | |||
# record a message | # record a message | ||
# session:recordFile(file_name, max_len, silence_threshold, silence_secs) | # session:recordFile(file_name, max_len, silence_threshold, silence_secs) | ||
rec_filename = "%s-%d.wav" % (digits, len(msg_ids)) | rec_filename = "%s-%d.wav" % (digits, len(msg_ids)) | ||
consoleLog("info", " | 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('INSERT INTO recordings (filename, start_time) values (\"%s\", DATETIME(\"NOW\"))' % rec_filename) | ||
c.execute("SELECT id FROM recordings ORDER BY id DESC LIMIT 1") | c.execute("SELECT id FROM recordings ORDER BY id DESC LIMIT 1") | ||
rec_id = c.fetchone()[0] | rec_id = c.fetchone()[0] | ||
Line 104: | Line 106: | ||
c.close() | c.close() | ||
sql.close() | sql.close() | ||
</source> | </source> |
Latest revision as of 18:41, 1 May 2011
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()