You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
4.0 KiB

  1. -- Copyright (C) 2016 Trever L. Adams
  2. -- based on code and ideas by Craig Gowing <craig.gowing@credativ.co.uk>
  3. -- & Alexis de Lattre <alexis.delattre@akretion.com>
  4. -- License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  5. -- This module assumes calls start at creation not answer.
  6. -- It works on incoming and outgoing calls.
  7. -- See code for the line to uncomment to change this.
  8. -- Add the following to the appropriate places in your dialplan
  9. -- Outbound (default dialplan, extension to extension, and anything out via gateway or via FXO:
  10. -- <action application="set" data="api_hangup_hook=lua freeswitch_logcall.lua"/>
  11. -- Inbound (public dialplan):
  12. -- <action application="export" data="odoo_type="inbound"/>
  13. -- <action application="export" data="nolocal:api_hangup_hook=lua freeswitch_logcall.lua"/>
  14. require("xmlrpc.http")
  15. function GetFileName(url)
  16. if url == nil then
  17. return ""
  18. end
  19. return url:match("^.+/(.+)$")
  20. end
  21. function getTransferTarget(app_data)
  22. if app_data == nil then
  23. return ""
  24. end
  25. app_data = app_data:gsub("-.?leg%s*", "")
  26. app_data = app_data:sub(1, string.find(app_data, " "), -2)
  27. return app_data
  28. end
  29. -- Change these to meet your installation
  30. local port=8069;
  31. local server="localhost";
  32. options_database = "odoo-test"
  33. options_userid = 1
  34. options_password = "testtesttest"
  35. -- Best not change anything below
  36. local protocol="https"
  37. server_string = protocol .. "://" .. server .. ":" .. port .. "/xmlrpc/2/object"
  38. function odoo_report_call(odoo_type, odoo_src, odoo_dst, odoo_duration, odoo_start, odoo_filename, odoo_uniqueid, odoo_description)
  39. local ok, res = xmlrpc.http.call(server_string, 'execute', options_database, options_userid, options_password,
  40. 'phone.common', 'log_call_and_recording', odoo_type, odoo_src, odoo_dst, odoo_duration, odoo_start,
  41. odoo_filename, odoo_uniqueid, odoo_description)
  42. assert(ok, string.format("XML-RPC call failed on client: %s", tostring(res)))
  43. end
  44. odoo_hangupcause = env:getHeader("variable_hangup_cause")
  45. if odoo_hangupcause == "LOSE_RACE" then
  46. return
  47. end
  48. odoo_type = env:getHeader("odoo_type")
  49. if odoo_type == 'inbound' then
  50. odoo_type = 'incoming'
  51. else
  52. odoo_type = 'outgoing'
  53. end
  54. if env:getHeader("Caller-RDNIS") then
  55. redirected = true
  56. end
  57. name = env:getHeader("origination_caller_id_name")
  58. if name == "Odoo Connector" then
  59. odoo_type = 'outgoing'
  60. end
  61. odoo_connector = env:getHeader("odoo_connector")
  62. if odoo_connector then
  63. odoo_src = env:getHeader("dialed_user")
  64. else
  65. odoo_src = env:getHeader("Caller-Caller-ID-Number")
  66. end
  67. odoo_dst = env:getHeader("Caller-Destination-Number")
  68. odoo_dst2 = env:getHeader("Other-Leg-Destination-Number")
  69. if odoo_dst2 then
  70. if string.len(odoo_dst) < string.len(odoo_dst2) then
  71. odoo_dst = odoo_dst2
  72. end
  73. end
  74. if redirected and odoo_type == "outgoing" and odoo_connector == nil then
  75. odoo_src, odoo_dst = odoo_dst, odoo_src
  76. end
  77. odoo_filename = env:getHeader("variable_rec_file")
  78. odoo_uniqueid = env:getHeader("variable_uuid")
  79. if odoo_filename == nil then
  80. odoo_filename = ""
  81. else
  82. odoo_filename = GetFileName(odoo_filename)
  83. end
  84. if odoo_hangupcause == nil then
  85. odoo_hangupcause = ""
  86. end
  87. app_name = env:getHeader("variable_last_app")
  88. app_data = getTransferTarget(env:getHeader("variable_last_arg"))
  89. if app_name == "transfer" then
  90. odoo_hangupcause = "[" .. odoo_hangupcause .. " / TRANSFERRED TO: " .. app_data .. "]"
  91. else
  92. odoo_hangupcause = "[" .. odoo_hangupcause .. "]"
  93. end
  94. if redirected == true then
  95. odoo_start = tonumber(env:getHeader("bridge_epoch"))
  96. else
  97. odoo_start = tonumber(env:getHeader("start_epoch"))
  98. -- odoo_start = tonumber(env:getHeader("answer_epoch"))
  99. end
  100. local end_epoch = tonumber(env:getHeader("end_epoch"))
  101. if odoo_start ~= nil and end_epoch ~= nil then
  102. odoo_duration = tonumber(end_epoch) - tonumber(odoo_start)
  103. end
  104. odoo_report_call(odoo_type, odoo_src, odoo_dst, tostring(odoo_duration), tostring(odoo_start), odoo_filename, odoo_uniqueid, odoo_hangupcause)