From 7a45e14c8cb9302d42430ef804babfbdfe822da4 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Wed, 9 Dec 2015 12:43:40 +0300 Subject: [PATCH] Add. mail templates for fax server responses. --- .../install/scripts/app/fax/app_languages.lua | 9 +++ .../app/fax/resources/scripts/queue/next.lua | 31 +++++--- .../app/fax/resources/scripts/queue/retry.lua | 73 ++++++++++++++----- .../app/fax/resources/scripts/queue/tasks.lua | 61 +++++++++++++++- .../templates/en/us/outbound/fail/body.tpl | 51 +++++++++++++ .../templates/en/us/outbound/fail/subject.tpl | 1 + .../templates/en/us/outbound/success/body.tpl | 51 +++++++++++++ .../en/us/outbound/success/subject.tpl | 1 + .../resources/functions/file_exists.lua | 6 +- 9 files changed, 249 insertions(+), 35 deletions(-) create mode 100644 resources/install/scripts/app/fax/app_languages.lua create mode 100644 resources/install/scripts/app/fax/resources/templates/en/us/outbound/fail/body.tpl create mode 100644 resources/install/scripts/app/fax/resources/templates/en/us/outbound/fail/subject.tpl create mode 100644 resources/install/scripts/app/fax/resources/templates/en/us/outbound/success/body.tpl create mode 100644 resources/install/scripts/app/fax/resources/templates/en/us/outbound/success/subject.tpl diff --git a/resources/install/scripts/app/fax/app_languages.lua b/resources/install/scripts/app/fax/app_languages.lua new file mode 100644 index 0000000000..7983512b77 --- /dev/null +++ b/resources/install/scripts/app/fax/app_languages.lua @@ -0,0 +1,9 @@ +text = text or {}; + +text['message-send_success'] = {} +text['message-send_success']['en-us'] = "We are happy to report the fax was sent successfully. It has been attached for your records." + +text['message-send_fail'] = {} +text['message-send_fail']['en-us'] = "We are sorry the fax failed to go through. It has been attached. Please check the number, and if it was correct you might consider emailing it instead." + +return text \ No newline at end of file diff --git a/resources/install/scripts/app/fax/resources/scripts/queue/next.lua b/resources/install/scripts/app/fax/resources/scripts/queue/next.lua index 4780867a1d..874171cca7 100644 --- a/resources/install/scripts/app/fax/resources/scripts/queue/next.lua +++ b/resources/install/scripts/app/fax/resources/scripts/queue/next.lua @@ -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 diff --git a/resources/install/scripts/app/fax/resources/scripts/queue/retry.lua b/resources/install/scripts/app/fax/resources/scripts/queue/retry.lua index 1d634ea725..3cc20dbf43 100644 --- a/resources/install/scripts/app/fax/resources/scripts/queue/retry.lua +++ b/resources/install/scripts/app/fax/resources/scripts/queue/retry.lua @@ -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 diff --git a/resources/install/scripts/app/fax/resources/scripts/queue/tasks.lua b/resources/install/scripts/app/fax/resources/scripts/queue/tasks.lua index e6ba83835f..33cd5c0f89 100644 --- a/resources/install/scripts/app/fax/resources/scripts/queue/tasks.lua +++ b/resources/install/scripts/app/fax/resources/scripts/queue/tasks.lua @@ -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; } diff --git a/resources/install/scripts/app/fax/resources/templates/en/us/outbound/fail/body.tpl b/resources/install/scripts/app/fax/resources/templates/en/us/outbound/fail/body.tpl new file mode 100644 index 0000000000..0a74af6208 --- /dev/null +++ b/resources/install/scripts/app/fax/resources/templates/en/us/outbound/fail/body.tpl @@ -0,0 +1,51 @@ + + + + + + + + +
+ Send fax fail +
+ + + + + + + + + + + + + + + + + +
+ To + + ${destination_number} +
+ Pages + + ${document_transferred_pages}/${document_total_pages} +
+ Message + + ${message} +
+ Error + + ${hangup_cause}/${fax_result_code}/${fax_result_text} +
+
+ \ No newline at end of file diff --git a/resources/install/scripts/app/fax/resources/templates/en/us/outbound/fail/subject.tpl b/resources/install/scripts/app/fax/resources/templates/en/us/outbound/fail/subject.tpl new file mode 100644 index 0000000000..0b4efa53ac --- /dev/null +++ b/resources/install/scripts/app/fax/resources/templates/en/us/outbound/fail/subject.tpl @@ -0,0 +1 @@ +Fax to: ${destination_number} FAILED \ No newline at end of file diff --git a/resources/install/scripts/app/fax/resources/templates/en/us/outbound/success/body.tpl b/resources/install/scripts/app/fax/resources/templates/en/us/outbound/success/body.tpl new file mode 100644 index 0000000000..073e4c6db6 --- /dev/null +++ b/resources/install/scripts/app/fax/resources/templates/en/us/outbound/success/body.tpl @@ -0,0 +1,51 @@ + + + + + + + + +
+ Send fax successfully +
+ + + + + + + + + + + + + + + + + +
+ To + + ${destination_number} +
+ Pages + + ${document_transferred_pages} +
+ Message + + ${message} +
+ Options + + ${fax_options} +
+
+ \ No newline at end of file diff --git a/resources/install/scripts/app/fax/resources/templates/en/us/outbound/success/subject.tpl b/resources/install/scripts/app/fax/resources/templates/en/us/outbound/success/subject.tpl new file mode 100644 index 0000000000..a1e835ddd1 --- /dev/null +++ b/resources/install/scripts/app/fax/resources/templates/en/us/outbound/success/subject.tpl @@ -0,0 +1 @@ +Fax to: ${destination_number} sent \ No newline at end of file diff --git a/resources/install/scripts/resources/functions/file_exists.lua b/resources/install/scripts/resources/functions/file_exists.lua index 76a31c5126..aece58bd5f 100644 --- a/resources/install/scripts/resources/functions/file_exists.lua +++ b/resources/install/scripts/resources/functions/file_exists.lua @@ -1,6 +1,8 @@ --check if a file exists function file_exists(name) - local f=io.open(name,"r") - if f~=nil then io.close(f) return true else return false end + local f = io.open(name, "r") + if not f then return end + f:close() + return name end \ No newline at end of file