瀏覽代碼

Ability to pass argv to outbound fix some issues with twilio (#18)

* Ability to pass argv to outbound fix some issues with twilio

Add ability to pass argv to outbound so that it can be called the same
as inbound or just pass a full message object to it. Fix some issues
with twilio and add more debug options.

* fix number typo
Chris Black 9 年之前
父節點
當前提交
dfae514fac
共有 5 個文件被更改,包括 77 次插入34 次删除
  1. 7 0
      sms/app_defaults.php
  2. 2 2
      sms/hook/sms_hook_twilio.php
  3. 60 17
      sms/resources/install/scripts/app/sms/index.lua
  4. 7 14
      sms/sms_hook_common.php
  5. 1 1
      sms/sms_mdr.php

+ 7 - 0
sms/app_defaults.php

@@ -37,6 +37,13 @@ if ($domains_processed == 1) {
 	$array[$x]['default_setting_category'] = 'sms';
 	$array[$x]['default_setting_subcategory'] = 'carriers';
 	$array[$x]['default_setting_name'] = 'array';
+	$array[$x]['default_setting_value'] = 'teli';
+	$array[$x]['default_setting_enabled'] = 'true';
+	$array[$x]['default_setting_description'] = '';
+	$x++;
+	$array[$x]['default_setting_category'] = 'sms';
+	$array[$x]['default_setting_subcategory'] = 'carriers';
+	$array[$x]['default_setting_name'] = 'array';
 	$array[$x]['default_setting_value'] = 'twilio';
 	$array[$x]['default_setting_enabled'] = 'true';
 	$array[$x]['default_setting_description'] = '';

+ 2 - 2
sms/hook/sms_hook_twilio.php

@@ -7,8 +7,8 @@ require_once "../sms_hook_common.php";
 
 if (check_acl()) {
 	if  ($_SERVER['REQUEST_METHOD'] == 'POST') {
-			error_log('REQUEST: ' .  print_r($_REQUEST, true));
-		route_and_send_sms($_REQUEST['From'], $_REQUEST['To'], $_REQUEST['Body']);
+		//error_log('REQUEST: ' .  print_r($_REQUEST, true));
+		route_and_send_sms($_REQUEST['From'], str_replace("+","",$_REQUEST['To']), $_REQUEST['Body']);
 	} else {
 	  die("no");
 	}

+ 60 - 17
sms/resources/install/scripts/app/sms/index.lua

@@ -62,6 +62,7 @@
 		extension = string.match(to,'%d+');
 
 		if (debug["info"]) then
+			freeswitch.consoleLog("notice", "[sms] DIRECTION: " .. direction .. "\n");
 			freeswitch.consoleLog("notice", "[sms] TO: " .. to .. "\n");
 			freeswitch.consoleLog("notice", "[sms] Extension: " .. extension .. "\n");
 			freeswitch.consoleLog("notice", "[sms] FROM: " .. from .. "\n");
@@ -87,15 +88,38 @@
 		end
 		event:fire();
 		to = extension;
-
 	elseif direction == "outbound" then
-		to = message:getHeader("to_user");
-		domain_name = message:getHeader("from_host");
-		from = message:getHeader("from_user");
-		body = message:getBody();
+		if (argv[3] ~= nil) then
+			to_user = argv[3];
+			to = string.match(to_user,'%d+');
+		else 
+			to = message:getHeader("to_user");
+		end
+		if (argv[3] ~= nil) then
+			domain_name = string.match(to_user,'%@+(.+)');
+		else
+			domain_name = message:getHeader("from_host");
+		end
+		if (argv[4] ~= nil) then
+			from = argv[4];
+			extension = string.match(from,'%d+');
+			if extension:len() > 7 then
+				outbound_caller_id_number = extension;
+			end 
+		else
+			from = message:getHeader("from_user");
+		end
+		if (argv[5] ~= nil) then
+			body = argv[5];
+		else
+			body = message:getBody();
+		end
 
 		if (debug["info"]) then
-			freeswitch.consoleLog("info", message:serialize());
+			if (message ~= nil) then
+				freeswitch.consoleLog("info", message:serialize());
+			end
+			freeswitch.consoleLog("notice", "[sms] DIRECTION: " .. direction .. "\n");
 			freeswitch.consoleLog("notice", "[sms] TO: " .. to .. "\n");
 			freeswitch.consoleLog("notice", "[sms] FROM: " .. from .. "\n");
 			freeswitch.consoleLog("notice", "[sms] BODY: " .. body .. "\n");
@@ -106,7 +130,7 @@
 			--get the domain_uuid using the domain name required for multi-tenant
 				if (domain_name ~= nil) then
 					sql = "SELECT domain_uuid FROM v_domains ";
-					sql = sql .. "WHERE domain_name = '" .. domain_name .. "' ";
+					sql = sql .. "WHERE domain_name = '" .. domain_name .. "' and domain_enabled = 'true' ";
 					if (debug["sql"]) then
 						freeswitch.consoleLog("notice", "[voicemail] SQL: " .. sql .. "\n");
 					end
@@ -122,7 +146,10 @@
 					sql = "SELECT outbound_caller_id_number, extension_uuid, carrier FROM v_extensions ";
 					sql = sql .. ", v_sms_destinations ";
 					sql = sql .. "WHERE outbound_caller_id_number = destination and  ";
-					sql = sql .. "v_extensions.domain_uuid = '" .. domain_uuid .. "' and extension = '" .. from .."' ";
+					sql = sql .. "v_extensions.domain_uuid = '" .. domain_uuid .. "' and extension = '" .. from .."' and ";
+					sql = sql .. "v_sms_destinations.enabled = 'true' and ";
+					sql = sql .. "v_extensions.enabled = 'true'";
+
 					if (debug["sql"]) then
 						freeswitch.consoleLog("notice", "[sms] SQL: " .. sql .. "\n");
 					end
@@ -132,6 +159,21 @@
 						carrier = rows["carrier"];
 					end);
 				end
+		elseif (outbound_caller_id_number ~= nil) then
+			--get the outbound_caller_id_number using the domain_uuid and the extension number
+				if (domain_uuid ~= nil) then
+					sql = "SELECT carrier FROM  ";
+					sql = sql .. " v_sms_destinations ";
+					sql = sql .. "WHERE destination = '" .. from .. "' and ";
+					sql = sql .. "v_sms_destinations.domain_uuid = '" .. domain_uuid .. "' and ";
+					sql = sql .. "enabled = 'true'";
+					if (debug["sql"]) then
+						freeswitch.consoleLog("notice", "[sms] SQL: " .. sql .. "\n");
+					end
+					status = dbh:query(sql, function(rows)
+						carrier = rows["carrier"];
+					end);
+				end
 		end
 		
 		sql = "SELECT default_setting_value FROM v_default_settings ";
@@ -165,7 +207,7 @@
 			cmd = "curl -u ".. access_key ..":" .. secret_key .. " -H \"Content-Type: application/json\" -X POST -d '{\"to\":\"" .. to .. "\",\"from\":\"" .. outbound_caller_id_number .."\",\"body\":\"" .. body .. "\"}' " .. api_url;
 		elseif (carrier == "twilio") then
 			cmd ="curl -X POST '" .. api_url .."' --data-urlencode 'To=+" .. to .."' --data-urlencode 'From=+" .. outbound_caller_id_number .. "' --data-urlencode 'Body=" .. body .. "' -u ".. access_key ..":" .. secret_key .. " --insecure";
-		end		
+		end
 		if (debug["info"]) then
 			freeswitch.consoleLog("notice", "[sms] CMD: " .. cmd .. "\n");
 		end
@@ -206,11 +248,12 @@
 	end
 
 
-
-	sql = "insert into v_sms_messages";
-   	sql = sql .. "(sms_message_uuid,extension_uuid,domain_uuid,start_stamp,from_number,to_number,message,direction,response,carrier)";
-   	sql = sql .. " values ('" .. uuid() .. "','" .. extension_uuid .. "','" .. domain_uuid .."',now(),'" .. from .. "','" .. to .. "','" .. body .. "','" .. direction .. "','','" .. carrier .."')";
-   	if (debug["sql"]) then
-		freeswitch.consoleLog("notice", "[sms] "..sql.."\n");
-	end
-	dbh:query(sql);
+	if (extension_uuid ~= nil) then
+		sql = "insert into v_sms_messages";
+		sql = sql .. "(sms_message_uuid,extension_uuid,domain_uuid,start_stamp,from_number,to_number,message,direction,response,carrier)";
+		sql = sql .. " values ('" .. uuid() .. "','" .. extension_uuid .. "','" .. domain_uuid .."',now(),'" .. from .. "','" .. to .. "','" .. body .. "','" .. direction .. "','','" .. carrier .."')";
+		if (debug["sql"]) then
+			freeswitch.consoleLog("notice", "[sms] "..sql.."\n");
+		end
+		dbh:query(sql);
+	end

+ 7 - 14
sms/sms_hook_common.php

@@ -34,23 +34,10 @@ include "root.php";
 
 //luarun /var/www/fusionpbx/app/sms/sms.lua TO FROM 'BODY'
 
-$debug = true;
+$debug = false;
 
 require_once "resources/require.php";
 
-/*if ($_SERVER['REQUEST_METHOD'] == 'POST') {
-	if  ($_SERVER['CONTENT_TYPE'] == 'application/json') {
-		$data = json_decode(file_get_contents("php://input"));
-	} else {
-		error_log('REQUEST: ' .  print_r($_REQUEST, true));
-		$data = (object) ['body' => $_REQUEST['Body'],
-			'to' => str_replace("+", "", $_REQUEST['To']),
-			'from' => intval(str_replace("+", "", $_REQUEST['From']))
-			];
-	}
-}
-*/
-
 function route_and_send_sms($from, $to, $body) {
 	global $db, $debug, $domain_uuid, $domain_name;
 	if ($debug) {
@@ -70,6 +57,7 @@ function route_and_send_sms($from, $to, $body) {
 				if ($debug) {
 					error_log("TO: " . print_r($to,true));
 					error_log("FROM: " . print_r($from,true));
+					error_log("BODY: " . print_r($body,true));
 				}
 
 				$sql = "select domain_name, ";
@@ -82,10 +70,15 @@ function route_and_send_sms($from, $to, $body) {
 				$sql .= "and v_destinations.domain_uuid = v_domains.domain_uuid";
 				$sql .= " and destination_number like :to and dialplan_detail_type = 'transfer'";
 
+				if ($debug) {
+					error_log("SQL: " . print_r($sql,true));
+				}
+
 				$prep_statement = $db->prepare(check_sql($sql));
 				$prep_statement->execute(array(':to' => $to));
 				$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
 				if (count($result) == 0) {
+					error_log("Cannot find a destination: " . print_r($result,true));
 					die("Invalid Destination");
 				}
 				foreach ($result as &$row) {

+ 1 - 1
sms/sms_mdr.php

@@ -51,7 +51,7 @@ else {
 	require_once "resources/paging.php";
 
 	$rows_per_page = ($_SESSION['domain']['paging']['numeric'] != '') ? $_SESSION['domain']['paging']['numeric'] : 50;
-	$sql = "select domain_name, extension, sms_message_uuid,start_stamp,from_nubmer,to_number,message,direction from v_sms_messages, v_domains, v_extensions where v_sms_messages.domain_uuid = v_domains.domain_uuid and v_sms_messages.extension_uuid = v_extensions.extension_uuid and v_domains.domain_uuid = :domain_uuid order by start_stamp DESC";
+	$sql = "select domain_name, extension, sms_message_uuid,start_stamp,from_number,to_number,message,direction from v_sms_messages, v_domains, v_extensions where v_sms_messages.domain_uuid = v_domains.domain_uuid and v_sms_messages.extension_uuid = v_extensions.extension_uuid and v_domains.domain_uuid = :domain_uuid order by start_stamp DESC";
 	error_log("SQL: " . print_r($sql,true));
 	$prep_statement = $db->prepare(check_sql($sql));
 	$prep_statement->execute(array(':domain_uuid' => $domain_uuid));