Simple conference.lua

From FreeSWITCH Wiki
Revision as of 21:41, 15 December 2008 by Mcollins (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

DB schema

conferencing=> \d+ conferences
                                  Table "public.conferences"
   Column   |  Type   |                        Modifiers                         | Description
------------+---------+----------------------------------------------------------+-------------
 id         | integer | not null default nextval('conferences_id_seq'::regclass) |
 conf_num   | integer | not null                                                 |
 conf_pin   | integer |                                                          |
 conf_owner | text    |                                                          |
 conf_email | text    |                                                          |
CREATE TABLE conferences(
   id SERIAL NOT NULL, 
   conf_num INT NOT NULL, 
   conf_pin INT, 
   conf_owner TEXT, 
   conf_email TEXT);

Lua script for conferencing

require "luasql.postgres"

attempt = 1
max_attempts = 3

function session_hangup_hook(status)
  freeswitch.consoleLog("NOTICE", "Session hangup: " .. status .. "\n")
  db_connection:close()
  error()
end

function get_conference_num(min, max, attempts, timeout)
  local conference_num
  freeswitch.consoleLog("NOTICE", "Awaiting caller to enter a conference number phrase:conference_num\n")
  conference_num = session:playAndGetDigits(min, max, attempts, timeout, '#', 'phrase:conference_num', '', '\\d+')
  return(conference_num)
end

function get_conference_pin(min, max, attempts, timeout, pin_number)
  local pin_attempt = 1
  local pin_max_attempt = 3

  while pin_attempt <= pin_max_attempt do
    conference_pin = session:playAndGetDigits(min, max, attempts, timeout, '#', 'phrase:conference_pin', '', '\\d+')
    if tonumber(conference_pin) == tonumber(pin_number) then
      return true
    else
      session:execute("phrase", "conference_bad_pin")
    end

    pin_attempt = pin_attempt + 1
  end

  return false
end

env = assert(luasql.postgres())
db_connection = assert(env:connect("dbname=conferencing user=conf password=conf"))

session:answer();
session:setHangupHook("session_hangup_hook")

if session:ready() then
  freeswitch.consoleLog("NOTICE", string.format("Caller has called conferencing server, Playing welcome message phrase:conference_welcome\n"))
  session:execute("phrase", "conference_welcome")
end

while attempt <= max_attempts do
  conf_num = get_conference_num(1, 4, 3, 4000)

  db_cursor = assert(db_connection:execute(string.format("select conf_pin from conferences where conf_num = %d", tonumber(conf_num))))
  row = db_cursor:fetch({}, "a")

  --[[ do conference authentication ]]--
  if row == nil then

    --[[ if the conference number does not exist, playback message saying it is
         and invalid conference number ]]--
    session:execute("phrase", "conference_bad_num")

  elseif row["conf_pin"] == nil or row["conf_pin"] == "" then
    freeswitch.consoleLog("NOTICE", string.format("Conference %d has no PIN, Sending caller into conference\n", tonumber(conf_num)))

    --[[ join the conference ]]--
    session:execute("conference", string.format("%s@default", conf_num))
  else
    freeswitch.consoleLog("NOTICE", string.format("Conference %d has a PIN %d, Authenticating user\n", tonumber(conf_num), tonumber(row["conf_pin"])))

    --[[ get the conference pin number ]]--
    if ((get_conference_pin(1, 4, 3, 4000, row["conf_pin"])) == true) then
      freeswitch.consoleLog("NOTICE", string.format("Conference %d correct PIN entered, Sending caller into conference\n", tonumber(conf_num)))
      --[[ join the conference, if the correct pin was entered ]]--
      session:execute("conference", string.format("%s@default", conf_num))
    else
      freeswitch.consoleLog("NOTICE", string.format("Conference %d invalid PIN entered, Looping again\n", tonumber(conf_num)))
    end
  end

  attempt = attempt + 1
end

session:execute("phrase", "conference_too_many_failures")

session:hangup()

See Also: Mod_lua