Add. mail templates for fax server responses.

This commit is contained in:
Alexey Melnichuk
2015-12-09 12:43:40 +03:00
parent bc1fcebad3
commit 7a45e14c8c
9 changed files with 249 additions and 35 deletions

View File

@@ -1,6 +1,7 @@
require "resources.functions.config"
require "resources.functions.sleep"
require "resources.functions.file_exists"
local log = require "resources.functions.log".next_fax_task
local Tasks = require "app.fax.resources.scripts.queue.tasks"
local Esl = require "resources.functions.esl"
@@ -13,17 +14,27 @@ local FAX_OPTIONS = {
"fax_use_ecm=false,fax_enable_t38=false,fax_enable_t38_request=false,fax_disable_v17=false";
}
local function task_send_mail(task)
local function task_send_mail(task, err)
local number_dialed = task.uri:match("/([^/]-)%s*$")
Tasks.send_mail_task(task, {
"Fax to: " .. number_dialed .. " FAILED",
table.concat{
"We are sorry the fax failed to go through. ",
"It has been attached. Please check the number " .. number_dialed .. ", ",
"and if it was correct you might consider emailing it instead.",
}}
)
local Text = require "resources.functions.text"
local text = Text.new("app.fax.app_languages")
local env = {
destination_number = number_dialed:match("^([^@]*)");
hangup_cause = err;
message = text['message-send_fail'];
}
local body = Tasks.build_template(task, 'outbound/fail/body', env)
local subject = Tasks.build_template(task, 'outbound/fail/subject', env)
if not subject then
log.warning("Can not find template for email")
subject = "Fax to: " .. number_dialed .. " FAILED"
end
Tasks.send_mail_task(task, {subject, body}, nil, file_exists(task.fax_file))
end
local function next_task()
@@ -56,7 +67,7 @@ local function next_task()
Tasks.wait_task(task, false, info)
if task.status ~= 0 then
Tasks.remove_task(task)
task_send_mail(task)
task_send_mail(task, tostring(info))
end
log.noticef('Can not originate to `%s` cause: %s: %s ', task.uri, tostring(status), tostring(info))
else

View File

@@ -1,8 +1,7 @@
-- include libraries
require "resources.functions.config";
require "resources.functions.explode";
require "resources.functions.split";
require "resources.functions.count";
require "resources.functions.file_exists";
local log = require "resources.functions.log".fax_retry
local Database = require "resources.functions.database"
@@ -28,6 +27,10 @@
local dbh = Database.new('system')
-- Global environment
default_language = env:getHeader("default_language")
default_dialect = env:getHeader("default_dialect")
-- Channel/FusionPBX variables
local uuid = env:getHeader("uuid")
local fax_queue_task_session = env:getHeader('fax_queue_task_session')
@@ -312,18 +315,32 @@
--Success
log.infof("RETRY STATS SUCCESS: GATEWAY[%s]", fax_options);
Tasks.remove_task(task)
local Text = require "resources.functions.text"
local text = Text.new("app.fax.app_languages")
local env = {
fax_options = fax_options;
destination_number = number_dialed:match("^([^@]*)");
document_transferred_pages = fax_document_transferred_pages;
document_total_pages = fax_document_total_pages;
message = text['message-send_success'];
}
local body = Tasks.build_template(task, 'outbound/success/body', env)
local subject = Tasks.build_template(task, 'outbound/success/subject', env)
if not subject then
log.warning("Can not find template for email")
subject = "Fax to: " .. number_dialed .. " SENT"
end
Tasks.send_mail_task(task, {subject, body}, uuid, file_exists(fax_file))
if keep_local == "false" then
os.remove(fax_file);
end
Tasks.remove_task(task)
Tasks.send_mail_task(task, {
"Fax to: " .. number_dialed .. " SENT",
table.concat{
"We are happy to report the fax was sent successfully.",
"It has been attached for your records.",
}}, uuid
)
end
if fax_success ~= "1" then
@@ -345,14 +362,32 @@
Tasks.wait_task(task, answered, hangup_cause_q850)
if task.status ~= 0 then
Tasks.remove_task(task)
Tasks.send_mail_task(task, {
"Fax to: " .. number_dialed .. " FAILED",
table.concat{
"We are sorry the fax failed to go through. ",
"It has been attached. Please check the number "..number_dialed..", ",
"and if it was correct you might consider emailing it instead.",
}}, uuid
)
local Text = require "resources.functions.text"
local text = Text.new("app.fax.app_languages")
local env = {
fax_options = fax_options;
destination_number = number_dialed:match("^([^@]*)");
document_transferred_pages = fax_document_transferred_pages;
document_total_pages = fax_document_total_pages;
hangup_cause = hangup_cause;
hangup_cause_q850 = hangup_cause_q850;
fax_result_code = fax_result_code;
fax_result_text = fax_result_text;
message = text['message-send_fail'];
}
local body = Tasks.build_template(task, 'outbound/fail/body', env)
local subject = Tasks.build_template(task, 'outbound/fail/subject', env)
if not subject then
log.warning("Can not find template for email")
subject = "Fax to: " .. number_dialed .. " FAILED"
end
Tasks.send_mail_task(task, {subject, body}, uuid, file_exists(fax_file))
end
end
end

View File

@@ -203,8 +203,6 @@ local function wait_task(task, answered, q850)
task.uuid
)
print(sql)
local ok, err = db:query( sql )
if not ok then return nil, err end
@@ -245,7 +243,7 @@ local function cleanup_tasks()
db:query(remove_finished_tasks_sql)
end
local function send_mail_task(task, message, call_uuid)
local function send_mail_task(task, message, call_uuid, file)
if not task.reply_address or #task.reply_address == 0 then
return
end
@@ -257,7 +255,61 @@ local function send_mail_task(task, message, call_uuid)
["X-FusionPBX-Email-Type"] = 'email2fax';
}
return send_mail(mail_x_headers, task.reply_address, message)
return send_mail(mail_x_headers, task.reply_address, message, file)
end
local function read_file(name, mode)
local f, err, code = io.open(name, mode or 'rb')
if not f then return nil, err, code end
local data = f:read("*all")
f:close()
return data
end
local function read_template(app, name, lang)
local default_language_path = 'en/us'
local full_path_tpl = scripts_dir .. '/app/' .. app .. '/resources/templates/{lang}/' .. name .. '.tpl'
local path
if lang then
path = file_exists((full_path_tpl:gsub('{lang}', lang)))
end
if (not path) and (lang ~= default_language_path) then
path = file_exists((full_path_tpl:gsub('{lang}', default_language_path)))
end
if path then
return read_file(path)
end
end
local function build_template(task, templ, env)
local lang
if default_language and default_dialect then
lang = (default_language .. '/' .. default_dialect):lower()
else
local settings = Settings.new(dbh or 'system', task.domain_name, task.domain_uuid)
lang = settings:get('domain', 'language', 'code')
if lang then lang = lang:gsub('%-', '/'):lower() end
end
local body = read_template('fax', templ, lang)
body = body:gsub("[^\\](${.-})", function(name)
name = name:sub(3, -2)
if type(env) == 'table' then
return env[name] or ''
end
if type(env) == 'function' then
return env(name) or ''
end
end)
return body
end
return {
@@ -274,4 +326,5 @@ return {
release_task = release_task;
cleanup_tasks = cleanup_tasks;
send_mail_task = send_mail_task;
build_template = build_template;
}