-
Notifications
You must be signed in to change notification settings - Fork 0
/
record_message.lua
382 lines (355 loc) · 16.2 KB
/
record_message.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
-- Part of FusionPBX
-- Copyright (C) 2013 Mark J Crane <[email protected]>
-- All rights reserved.
--
-- Redistribution and use in source and binary forms, with or without
-- modification, are permitted provided that the following conditions are met:
--
-- 1. Redistributions of source code must retain the above copyright notice,
-- this list of conditions and the following disclaimer.
--
-- 2. Redistributions in binary form must reproduce the above copyright
-- notice, this list of conditions and the following disclaimer in the
-- documentation and/or other materials provided with the distribution.
--
-- THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
-- AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-- AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
-- OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-- POSSIBILITY OF SUCH DAMAGE.
--load libraries
local Database = require "resources.functions.database"
local Settings = require "resources.functions.lazy_settings"
local JSON = require "resources.functions.lunajson"
--define uuid function
local random = math.random;
local function gen_uuid()
local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
return string.gsub(template, '[xy]', function (c)
local v = (c == 'x') and random(0, 0xf) or random(8, 0xb);
return string.format('%x', v);
end)
end
--define escape function (prevents lua injection attacks)
local function esc(x)
return (x:gsub('%%', '%%%%')
:gsub('^%^', '%%^')
:gsub('%$$', '%%$')
:gsub('%(', '%%(')
:gsub('%)', '%%)')
:gsub('%.', '%%.')
:gsub('%[', '%%[')
:gsub('%]', '%%]')
:gsub('%*', '%%*')
:gsub('%+', '%%+')
:gsub('%-', '%%-')
:gsub('%?', '%%?'))
end
local function transcribe(file_path,settings,start_epoch)
--transcription variables
if (os.time() - start_epoch > 2) then
local transcribe_provider = settings:get('voicemail', 'transcribe_provider', 'text') or '';
transcribe_language = settings:get('voicemail', 'transcribe_language', 'text') or 'en-US';
if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] transcribe_provider: " .. transcribe_provider .. "\n");
freeswitch.consoleLog("notice", "[voicemail] transcribe_language: " .. transcribe_language .. "\n");
end
if (transcribe_provider == "azure") then
local api_key1 = settings:get('voicemail', 'azure_key1', 'text') or '';
local api_key2 = settings:get('voicemail', 'azure_key2', 'text') or '';
local azure_region = settings:get('voicemail', 'azure_server_region', 'text') or '';
if (api_key1 ~= '' and api_key2 ~= '' and azure_region ~= '') then
transcribe_cmd = "curl -X POST \"https://"..azure_region..".stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=" .. transcribe_language .. "&profanity=raw\" -H 'Ocp-Apim-Subscription-Key: "..api_key1.."' -H 'Content-type: audio/wav; codecs=audio/pcm; samplerate=16000;' -H 'Accept: application/json' --data-binary @"..file_path
local handle = io.popen(transcribe_cmd);
local transcribe_result = handle:read("*a");
handle:close();
if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] CMD: " .. transcribe_cmd .. "\n");
freeswitch.consoleLog("notice", "[voicemail] RESULT: " .. transcribe_result .. "\n");
end
--Trancribe request can fail
if (transcribe_result == '') then
freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: (null) \n");
return ''
end
local transcribe_json = JSON.decode(transcribe_result);
--Trancribe result can be nil
if (transcribe_json == nil) then
freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: results = (null) \n");
return ''
end
if (debug["info"]) then
if (transcribe_json["RecognitionStatus"] ~= 'Success') then
freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: (null) \n");
else
freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: " .. transcribe_json["DisplayText"] .. "\n");
end
end
transcription = transcribe_json["DisplayText"];
return transcription;
end
end
if (transcribe_provider == "custom") then
local transcription_server = settings:get('voicemail', 'transcription_server', 'text') or '';
local api_key = settings:get('voicemail', 'api_key', 'text') or '';
local json_enabled = settings:get('voicemail', 'json_enabled', 'boolean') or "false";
if (transcription_server ~= '') then
transcribe_cmd = "curl -X POST " .. transcription_server .. " -H 'Authorization: Bearer " .. api_key .. "' -F file=@"..file_path
local handle = io.popen(transcribe_cmd);
local transcribe_result = esc(handle:read("*a"));
handle:close();
if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] CMD: " .. transcribe_cmd .. "\n");
freeswitch.consoleLog("notice", "[voicemail] RESULT: " .. transcribe_result .. "\n");
end
--Trancribe request can fail
if (transcribe_result == '') then
freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: (null) \n");
return ''
end
if (json_enabled == "true") then
local transcribe_json = JSON.decode(transcribe_result);
if (transcribe_json["message"] == nil) then
freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: " .. transcribe_result .. "\n");
transcribe_result = '';
end
if (transcribe_json["error"] ~= nil) then
freeswitch.consoleLog("notice", "[voicemail] TRANSCRIPTION: " .. transcribe_result .. "\n");
transcribe_result = '';
end
transcribe_result = transcribe_json["message"];
end
return transcribe_result;
end
end
else
if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] message too short for transcription.\n");
end
end
return '';
end
--save the recording
function record_message()
local db = dbh or Database.new('system')
local settings = Settings.new(db, domain_name, domain_uuid)
local max_len_seconds = settings:get('voicemail', 'message_max_length', 'numeric') or 300;
transcribe_enabled = settings:get('voicemail', 'transcribe_enabled', 'boolean') or "false";
if (debug["info"]) then
freeswitch.consoleLog("notice", "[voicemail] transcribe_enabled: " .. transcribe_enabled .. "\n");
freeswitch.consoleLog("notice", "[voicemail] voicemail_transcription_enabled: " .. voicemail_transcription_enabled .. "\n");
end
--record your message at the tone press any key or stop talking to end the recording
if (skip_instructions == "true") then
--skip the instructions
else
if (dtmf_digits and string.len(dtmf_digits) == 0) then
dtmf_digits = macro(session, "record_message", 1, 100);
end
end
--voicemail ivr options
if (session:ready()) then
if (dtmf_digits == nil) then
dtmf_digits = session:getDigits(max_digits, "#", 1000);
else
dtmf_digits = dtmf_digits .. session:getDigits(max_digits, "#", 1000);
end
end
if (dtmf_digits) then
if (string.len(dtmf_digits) > 0) then
if (session:ready()) then
if (direct_dial["enabled"] == "true") then
if (string.len(dtmf_digits) < max_digits) then
dtmf_digits = dtmf_digits .. session:getDigits(direct_dial["max_digits"], "#", 3000);
end
end
end
if (session:ready()) then
freeswitch.consoleLog("notice", "[voicemail] dtmf_digits: " .. string.sub(dtmf_digits, 0, 1) .. "\n");
if (dtmf_digits == "*") then
if (remote_access == "true") then
--check the voicemail password
check_password(voicemail_id, password_tries);
--send to the main menu
timeouts = 0;
main_menu();
else
--remote access is false
freeswitch.consoleLog("notice", "[voicemail] remote access is disabled.\n");
session:hangup();
end
elseif (string.sub(dtmf_digits, 0, 1) == "*") then
--do not allow dialing numbers prefixed with *
session:hangup();
else
--get the voicemail options
local sql = [[SELECT * FROM v_voicemail_options WHERE voicemail_uuid = :voicemail_uuid ORDER BY voicemail_option_order asc ]];
local params = {voicemail_uuid = voicemail_uuid};
if (debug["sql"]) then
freeswitch.consoleLog("notice", "[voicemail] SQL: " .. sql .. "; params:" .. json.encode(params) .. "\n");
end
count = 0;
dbh:query(sql, params, function(row)
--check for matching options
if (tonumber(row.voicemail_option_digits) ~= nil) then
row.voicemail_option_digits = "^"..row.voicemail_option_digits.."$";
end
if (api:execute("regex", "m:~"..dtmf_digits.."~"..row.voicemail_option_digits) == "true") then
if (row.voicemail_option_action == "menu-exec-app") then
--get the action and data
pos = string.find(row.voicemail_option_param, " ", 0, true);
action = string.sub( row.voicemail_option_param, 0, pos-1);
data = string.sub( row.voicemail_option_param, pos+1);
--check if the option uses a regex
regex = string.find(row.voicemail_option_digits, "(", 0, true);
if (regex) then
--get the regex result
result = trim(api:execute("regex", "m:~"..digits.."~"..row.voicemail_option_digits.."~$1"));
if (debug["regex"]) then
freeswitch.consoleLog("notice", "[voicemail] regex m:~"..digits.."~"..row.voicemail_option_digits.."~$1\n");
freeswitch.consoleLog("notice", "[voicemail] result: "..result.."\n");
end
--replace the $1 and the domain name
data = data:gsub("$1", result);
data = data:gsub("${domain_name}", domain_name);
end --if regex
end --if menu-exex-app
end --if regex match
--execute
if (action) then
if (string.len(action) > 0) then
--send to the log
if (debug["action"]) then
freeswitch.consoleLog("notice", "[voicemail] action: " .. action .. " data: ".. data .. "\n");
end
--run the action
session:execute(action, data);
end
end
--clear the variables
action = "";
data = "";
--inrement the option count
count = count + 1;
end); --end results
--direct dial
if (session:ready()) then
if (direct_dial["enabled"] == "true" and count == 0) then
if (string.len(dtmf_digits) < max_digits) then
dtmf_digits = dtmf_digits .. session:getDigits(direct_dial["max_digits"], "#", 5000);
session:transfer(dtmf_digits.." XML "..context);
end
end
end
end
end
end
end
--play the beep
dtmf_digits = '';
result = macro(session, "record_beep", 1, 100);
--start epoch
start_epoch = os.time();
--save the recording
-- syntax is session:recordFile(file_name, max_len_secs, silence_threshold, silence_secs)
silence_seconds = 5;
if (storage_path == "http_cache") then
result = session:recordFile(storage_path.."/"..voicemail_id.."/msg_"..uuid.."."..vm_message_ext, max_len_seconds, record_silence_threshold, silence_seconds);
else
mkdir(voicemail_dir.."/"..voicemail_id);
if (vm_message_ext == "mp3") then
shout_exists = trim(api:execute("module_exists", "mod_shout"));
if (shout_exists == "true" and transcribe_enabled == "false") or (shout_exists == "true" and transcribe_enabled == "true" and voicemail_transcription_enabled == "false") then
freeswitch.consoleLog("notice", "using mod_shout for mp3 encoding\n");
--record in mp3 directly
result = session:recordFile(voicemail_dir.."/"..voicemail_id.."/msg_"..uuid..".mp3", max_len_seconds, record_silence_threshold, silence_seconds);
else
--create initial wav recording
result = session:recordFile(voicemail_dir.."/"..voicemail_id.."/msg_"..uuid..".wav", max_len_seconds, record_silence_threshold, silence_seconds);
if (transcribe_enabled == "true" and voicemail_transcription_enabled == "true") then
local transcribe_provider = settings:get('voicemail', 'transcribe_provider', 'text') or '';
if (transcribe_provider == 'microsoft') then
transcription_path = voicemail_dir.."/"..voicemail_id.."/trans_"..uuid..".wav";
resample = "sox "..voicemail_dir.."/"..voicemail_id.."/msg_"..uuid..".wav "..transcription_path.." rate 16000";
session:execute("system", resample);
transcription = transcribe(transcription_path,settings,start_epoch);
os.remove(transcription_path);
else
transcription = transcribe(voicemail_dir.."/"..voicemail_id.."/msg_"..uuid..".wav",settings,start_epoch);
end
end
--use lame to encode, if available
if (file_exists("/usr/bin/lame")) then
freeswitch.consoleLog("notice", "using lame for mp3 encoding\n");
--convert the wav to an mp3 (lame required)
resample = "/usr/bin/lame -b 32 --resample 8 -m s "..voicemail_dir.."/"..voicemail_id.."/msg_"..uuid..".wav "..voicemail_dir.."/"..voicemail_id.."/msg_"..uuid..".mp3";
session:execute("system", resample);
--delete the wav file, if mp3 exists
if (file_exists(voicemail_dir.."/"..voicemail_id.."/msg_"..uuid..".mp3")) then
os.remove(voicemail_dir.."/"..voicemail_id.."/msg_"..uuid..".wav");
else
vm_message_ext = "wav";
end
else
freeswitch.consoleLog("notice", "neither mod_shout or lame found, defaulting to wav\n");
vm_message_ext = "wav";
end
end
else
result = session:recordFile(voicemail_dir.."/"..voicemail_id.."/msg_"..uuid.."."..vm_message_ext, max_len_seconds, record_silence_threshold, silence_seconds);
if (transcribe_enabled == "true" and voicemail_transcription_enabled == "true") then
if (transcribe_provider == 'microsoft') then
transcription_path = voicemail_dir.."/"..voicemail_id.."/trans_"..uuid..".wav";
resample = "sox "..voicemail_dir.."/"..voicemail_id.."/msg_"..uuid..".wav "..transcription_path.." rate 16000";
session:execute("system", resample);
transcription = transcribe(transcription_path,settings,start_epoch);
os.remove(transcription_path);
else
transcription = transcribe(voicemail_dir.."/"..voicemail_id.."/msg_"..uuid..".wav",settings,start_epoch);
end
end
end
end
--stop epoch
stop_epoch = os.time();
--calculate the message length
message_length = stop_epoch - start_epoch;
message_length_formatted = format_seconds(message_length);
--if the recording is below the minimal length then re-record the message
if (message_length > 2) then
--continue
else
if (session:ready()) then
--your recording is below the minimal acceptable length, please try again
dtmf_digits = '';
macro(session, "too_small", 1, 100);
--record your message at the tone
timeouts = timeouts + 1;
if (timeouts < max_timeouts) then
record_message();
else
timeouts = 0;
record_menu("message", voicemail_dir.."/"..voicemail_id.."/msg_"..uuid.."."..vm_message_ext);
end
end
end
--instructions press 1 to listen to the recording, press 2 to save the recording, press 3 to re-record
if (session:ready()) then
if (skip_instructions == "true") then
--save the message
dtmf_digits = '';
macro(session, "message_saved", 1, 100, '');
macro(session, "goodbye", 1, 100, '');
--hangup the call
session:hangup();
else
timeouts = 0;
record_menu("message", voicemail_dir.."/"..voicemail_id.."/msg_"..uuid.."."..vm_message_ext);
end
end
end