tSIP softphone: Lua script examples

Random Lua scripts. See "script" button reference for general description and list of available functions.

Quick introduction to Lua: http://tylerneylon.com/a/learn-lua/.

on_call_disconnected_play.lua ("on call state" script)

-- "on call state": beep when call ends if (only if) call was established
call_state = GetCallState()
call_incoming = IsCallIncoming()

if call_state == 6 then -- CALL_STATE_ESTABLISHED
	SetVariable("CallEstablished", "does-not-matter-what-i-enter-here")	
elseif call_state == 0 then -- CALL_STATE_CLOSED
	local dummy, isset = GetVariable("CallEstablished")
	ClearVariable("CallEstablished")
	if (isset ~= 0) then
		-- Beep only if call was established previously
		Beep(392, 350)
		Sleep(50)
		Beep(392, 350)
		Sleep(50)
		Beep(392, 350)
		Sleep(50)
		Beep(311.1, 250)
		Sleep(50)
		Beep(466.2, 250)
		Sleep(50)
		Beep(392, 350)
		Sleep(50)
		Beep(311.1, 250)
		Sleep(50)
		Beep(466, 250)
		Sleep(50)
		Beep(392, 700)
	end	
end
  

Count number of times script was executed ("button" script)

-- This script counts number of times it was executed
local count, var_isset = GetVariable("runcount")
if (var_isset == 0) then
	count = 0
else
	count = tonumber(count)
end

count = count + 1
SetVariable("runcount", count)
print(string.format("Script executed %d time(s)\n", count))  
  

Send DTMFs automatically to e.g. enter conference room ("button" script)

-- user config
number = "sip:192.168.1.85:5065"	-- or regular phone number
dtmf = "1234"
-- end of user config

Call(number)
for i=1, 20, 1
do
	if (i == 20) then
		print("Timed out waiting for confirmed state\n")
		break;
	end

	Sleep(300)
	call_state = GetCallState()
	if call_state == 6 then
		-- CALL_STATE_ESTABLISHED
		Sleep(2000)
		SendDtmf("1234")
		break
	elseif call_state == 0 then
		-- CALL_STATE_CLOSED
		print("End of call\n")
		break;
	end
end
print("End of script\n")  
  

Change tray icon depending on call state ("on call state")

callState = GetCallState()
if callState == 0 then
	SetTrayIcon("icon_closed.bmp")
elseif callState == 1 then
	SetTrayIcon("icon_incoming.bmp")
elseif callState == 2 then
	SetTrayIcon("icon_outgoing.bmp")
elseif callState == 3 then
	SetTrayIcon("icon_trying.bmp")
elseif callState == 4 then
	SetTrayIcon("icon_ringing.bmp")
elseif callState == 5 then
	SetTrayIcon("icon_progress.bmp")
elseif callState == 6 then
	SetTrayIcon("icon_established.bmp")
end

Lua modules / including another script

-- scriptTest.lua from scripts directory
local M = {} -- public interface
local x = 1	 -- private

function M.testFunction()
	print(string.format("Test function called, x = %d\n", x))
end

return M
-- end of scriptTest.lua


-- main.lua
local scriptTest = require("scripts.scriptTest")
print("Trying to call testFunction...\n")
scriptTest.testFunction()
-- end of main.lua

Send record file by mail - to be combined with mailbox

call_state = GetCallState()

if call_state == 0 then -- CALL_STATE_CLOSED
	local fileName = GetRecordFile()
	if fileName ~= "" then
		-- check file size, skip small (probably empty - only announcement) files
		local file = io.open(fileName, "r")
		local fileSize = file:seek("end")
		file:close()
		if (fileSize < 8 * 2 * 8000) then	-- 8 seconds * 2B/sample * 8kS/s
			print(string.format("Skipping file %s (size = %d)\n", fileName, fileSize))
		else
			print(string.format("Sending mail with file %s (size = %d)\n", fileName, fileSize))
			-- TODO: replace with real command line mail client
			local params = "send " .. fileName
			ShellExecute("open", "some_sendmail.exe", params, nil, 1)
		end
	end
end

Set call codec name as button caption ("on call state" event script)

-- configuration
local buttonId = 0	-- button to display codec name on
-- end of configuration

local callState = GetCallState()

if callState == 6 then -- ESTABLISHED
	local codecName = GetCallCodecName();
	local caption = string.format("    %s", codecName)
	SetButtonCaption(buttonId, caption)
elseif callState == 0 then -- CLOSED
	SetButtonCaption(buttonId, "    ----")
end

Back to howto list


 "Cookie monsters": 5933070    Parse time: 0.000 s