Browse Source

修改挂断事件判定

dujunchen 4 days ago
parent
commit
1393751028
2 changed files with 89 additions and 32 deletions
  1. 89 32
      api/admin/zoho/push.go
  2. BIN
      deployments/crm-api

+ 89 - 32
api/admin/zoho/push.go

@@ -9,6 +9,7 @@ import (
 	"io/ioutil"
 	"net/http"
 	"strconv"
+	"strings"
 	"time"
 
 	"gopkg.in/ini.v1"
@@ -16,8 +17,8 @@ import (
 
 var TimestampList = make(map[string]string)
 var StartTimeList = make(map[string]time.Time) // 事件中 Timestamp 转 StartTime 有问题,重新记录时间
-// var CallTypeList = make(map[string]string)     // 临时记录 incoming 和 outgoing
-var DialStatusList = make(map[string]string) // 临时记录 DialStatus:ANSWER NOANSWER
+var LinkedidList = make(map[string]string)     // 临时记录 incoming 和 outgoing
+var DialStatusList = make(map[string]string)   // 临时记录 DialStatus:ANSWER NOANSWER
 //	func addOneToStringNumber(s string) string {
 //		num, err := strconv.Atoi(s)
 //		if err != nil {
@@ -123,6 +124,8 @@ func CallNotify(event map[string]string) {
 		fmt.Printf("TimestampParseFloat=%f\n", TimestampTmp002)
 
 		// * ===================================================================================== */
+		Channel := strings.Split(event["Channel"], "/")
+		fmt.Println("Channel[0] = ", Channel[0])
 		// Outgoing Call - Ringing
 		if event["Context"] == "macro-trunkdial-failover" {
 			lfshook.NewLogger().Info("Outgoing Call - Ringing")
@@ -140,7 +143,7 @@ func CallNotify(event map[string]string) {
 
 			// Incoming Call - Ringing
 			// if event["Context"] == "macro-stdexten" {
-		} else if event["Context"] == "macro-stdexten" || event["Context"] == "macro-stdexten-withoutvm" { // 开启语音留言:macro-stdexten  关闭语音留言:macro-stdexten-withoutvm
+		} else if (event["Context"] == "macro-stdexten" && Channel[0] != "Local") || (event["Context"] == "macro-stdexten-withoutvm" && Channel[0] != "Local") { // 开启语音留言:macro-stdexten  关闭语音留言:macro-stdexten-withoutvm
 			lfshook.NewLogger().Error("Incoming Call - Ringing")
 			fmt.Println("event[] = ", event)
 			fmt.Println("event = ", event["Event"])
@@ -160,10 +163,11 @@ func CallNotify(event map[string]string) {
 
 		// 呼叫结束事件
 	} else if event["Event"] == "DialEnd" {
+		Channel := strings.Split(event["Channel"], "/")
 		// 记录呼叫状态
 		DialStatusList[event["Linkedid"]] = event["DialStatus"]
 		fmt.Println("DialEnd[] = ", event)
-		fmt.Println("DialEnd = ", event["Event"])
+		fmt.Println("Channel[0] = ", Channel[0])
 		// Outgoing Call - Unattended
 		if event["Context"] == "macro-trunkdial-failover" && event["DialStatus"] == "CANCEL" {
 			lfshook.NewLogger().Info("Outgoing Call - CANCEL")
@@ -222,7 +226,7 @@ func CallNotify(event map[string]string) {
 
 			// Incoming Call - Missed
 			// if event["Context"] == "macro-stdexten" && event["DialStatus"] != "ANSWER" {
-		} else if (event["Context"] == "macro-stdexten" || event["Context"] == "macro-stdexten-withoutvm") && event["DialStatus"] != "ANSWER" { // 开启语音留言:macro-stdexten  关闭语音留言:macro-stdexten-withoutvm
+		} else if (event["Context"] == "macro-stdexten" && event["DialStatus"] != "ANSWER" && Channel[0] != "Local") || (event["Context"] == "macro-stdexten-withoutvm" && event["DialStatus"] != "ANSWER" && Channel[0] != "Local") { // 开启语音留言:macro-stdexten  关闭语音留言:macro-stdexten-withoutvm
 			lfshook.NewLogger().Error("Incoming Call - Missed")
 			fmt.Println("event[] = ", event)
 			fmt.Println("event = ", event["Event"])
@@ -243,6 +247,8 @@ func CallNotify(event map[string]string) {
 
 		// 呼叫已连接事件
 	} else if event["Event"] == "BridgeEnter" {
+		Channel := strings.Split(event["Channel"], "/")
+		fmt.Println("Channel[0] = ", Channel[0])
 		// Outgoing Call - Answered
 		if event["Context"] == "macro-trunkdial-failover" && event["Priority"] == "1" { // 注意这里选择 Priority:1 的,便于确认 CallerIDNum,ConnectedLineNum
 			// 记录开始时间
@@ -254,7 +260,7 @@ func CallNotify(event map[string]string) {
 			fmt.Println("Linkedid = ", event["Timestamp"])
 			TimestampList[event["Linkedid"]] = event["Timestamp"]
 			StartTimeList[event["Linkedid"]] = time.Now().Add(-8 * time.Hour)
-
+			LinkedidList["Linkedid"] = event["Linkedid"]
 			// https: //www.zohoapis.com/phonebridge/v3/callnotify?type=dialed&state=answered&id=10003&from=123456789&to=12300000001
 			// getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=dialed&state=answered&id=%s&from=%s&to=%s", ZOHO_URL, callId, event["CallerIDNum"], event["ConnectedLineNum"])
 			getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=dialed&state=answered&id=%s&from=%s&to=%s", ZOHO_URL, event["Linkedid"], event["ConnectedLineNum"], event["CallerIDNum"])
@@ -266,7 +272,7 @@ func CallNotify(event map[string]string) {
 
 			// Incoming Call - Answered
 			// if event["Context"] == "macro-stdexten" {
-		} else if event["Context"] == "macro-stdexten" || event["Context"] == "macro-stdexten-withoutvm" { // 开启语音留言:macro-stdexten  关闭语音留言:macro-stdexten-withoutvm
+		} else if (event["Context"] == "macro-stdexten" || event["Context"] == "macro-stdexten-withoutvm") && Channel[0] != "Local" { // 开启语音留言:macro-stdexten  关闭语音留言:macro-stdexten-withoutvm
 			// 记录开始时间
 			// TimestampList[event["Uniqueid"]] = event["Timestamp"] // Uniqueid 有时通话也不合适  NG  // 都改为 Linkedid
 			// StartTimeList[event["Uniqueid"]] = time.Now()         // Uniqueid 有时通话也不合适  NG  // 都改为 Linkedid
@@ -276,7 +282,7 @@ func CallNotify(event map[string]string) {
 			fmt.Println("Linkedid = ", event["Timestamp"])
 			TimestampList[event["Linkedid"]] = event["Timestamp"]             // 呼入时设置 Uniqueid 不是 Linkedid
 			StartTimeList[event["Linkedid"]] = time.Now().Add(-8 * time.Hour) // 呼入时设置 Uniqueid 不是 Linkedid
-
+			LinkedidList["Linkedid"] = event["Linkedid"]
 			// https://www.zohoapis.com/phonebridge/v3/callnotify?type=received&state=answered&id=10023&from=12300000001&to=123456789
 			getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=answered&id=%s&from=%s&to=%s", ZOHO_URL, event["Linkedid"], event["CallerIDNum"], event["ConnectedLineNum"])
 			// if zohoUser != "" {
@@ -319,8 +325,9 @@ func CallNotify(event map[string]string) {
 		// delete(TimestampList, event["Uniqueid"])
 		// * ===================================================================================== */
 		// 判断呼叫状态,为 ANSWER 时才推送,否则见以上推送 Outgoing Call - Unattended 或 Incoming Call - Missed
+		Channel := strings.Split(event["Channel"], "/")
 		fmt.Println("event[] = ", event)
-		fmt.Println("event = ", event["Event"])
+		fmt.Println("Channel[0] = ", Channel[0])
 		lfshook.NewLogger().Errorf("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa %+v", event["Context"])
 		fmt.Println("ChannelHangupRequest1 = ", DialStatusList[event["Linkedid"]])
 		fmt.Println("ChannelHangupRequest1 = ", event["DialStatus"])
@@ -331,20 +338,24 @@ func CallNotify(event map[string]string) {
 			lfshook.NewLogger().Errorf("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa %+v", event["Context"])
 			lfshook.NewLogger().Errorf("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb %+v", event["Linkedid"])
 			lfshook.NewLogger().Errorf("ccccccccccccccccccccccccccccccc %+v", event["DialStatus"])
+			durationTime := getDuration(event["Linkedid"], event["Timestamp"])
+			startTimeutc := StartTimeList[event["Linkedid"]].In(time.UTC) // 存储从string 改为 time.Time
+			startTime := startTimeutc.Format(time.RFC3339)
+			fmt.Println("startTime: ", startTime)
 			// Outgoing Call - Ended
-			if event["Context"] == "macro-trunkdial-failover" {
+			if event["Context"] == "macro-trunkdial-failover" && durationTime != -1 {
 				fmt.Println("Linkedid = ", event["Timestamp"])
 				// 获取时间
 				// durationTime := getDuration(event["Uniqueid"], event["Timestamp"])
-				durationTime := getDuration(event["Linkedid"], event["Timestamp"])
+				// durationTime := getDuration(event["Linkedid"], event["Timestamp"])
 				// /* ===================测试用 ============================================================
 				// startTime, _ := time.Parse("2006-01-02 15:04:05", TimestampList[event["Uniqueid"]]) // error   1734328213.774410  =>  0001-01-01 00:00:00 +0000 UTC
 				// startTime := StartTimeList[event["Uniqueid"]]
 				// startTime := StartTimeList[event["Uniqueid"]].Format("2006-01-02 15:04:09") // 存储从string 改为 time.Time
 				// startTime := StartTimeList[event["Linkedid"]].Format("2006-01-02 15:04:09") // 存储从string 改为 time.Time
-				startTimeutc := StartTimeList[event["Linkedid"]].In(time.UTC) // 存储从string 改为 time.Time
-				startTime := startTimeutc.Format(time.RFC3339)
-				fmt.Println("startTime: ", startTime)
+				// startTimeutc := StartTimeList[event["Linkedid"]].In(time.UTC) // 存储从string 改为 time.Time
+				// startTime := startTimeutc.Format(time.RFC3339)
+				// fmt.Println("startTime: ", startTime)
 				// durationTime002 := getDuration002(startTime)
 				// durationTime002 := time.Now().Sub(StartTimeList[event["Uniqueid"]]) // ok    durationTime002:  7.78408849s   // should use time.Since instead of time.Now().Sub (S1012)
 				// durationTime002 := time.Since(StartTimeList[event["Uniqueid"]]) // ok    durationTime002:  11.611233458s
@@ -353,20 +364,49 @@ func CallNotify(event map[string]string) {
 
 				// 删除记录时间
 				delete(StartTimeList, event["Linkedid"])
+				if event["Priority"] == "1" {
+					// https://www.zohoapis.com/phonebridge/v3/callnotify?type=dailed&state=ended&id=10030&from=123456789&to=12300000001&start_time=2024-12-04 11:32:15&duration=325
+					// getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=dailed&state=ended&id=%s&from=%s&to=%s&start_time=%s&duration=%d", ZOHO_URL, callId, event["CallerIDNum"], event["ConnectedLineNum"], startTime, durationTime)
+					getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=dailed&state=ended&id=%s&from=%s&to=%s&start_time=%s&duration=%d", ZOHO_URL, event["Linkedid"], event["ConnectedLineNum"], event["CallerIDNum"], startTime, durationTime)
+					// if zohoUser != "" {
+					// 	getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
+					// }
+					fmt.Println("getURL = ", getURL)
+					go httpclient.ZohoGet(getURL)
+				} else {
+					// https://www.zohoapis.com/phonebridge/v3/callnotify?type=dailed&state=ended&id=10030&from=123456789&to=12300000001&start_time=2024-12-04 11:32:15&duration=325
+					// getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=dailed&state=ended&id=%s&from=%s&to=%s&start_time=%s&duration=%d", ZOHO_URL, callId, event["CallerIDNum"], event["ConnectedLineNum"], startTime, durationTime)
+					getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=dailed&state=ended&id=%s&from=%s&to=%s&start_time=%s&duration=%d", ZOHO_URL, event["Linkedid"], event["CallerIDNum"], event["ConnectedLineNum"], startTime, durationTime)
+					// if zohoUser != "" {
+					// 	getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
+					// }
+					fmt.Println("getURL = ", getURL)
+					go httpclient.ZohoGet(getURL)
+				}
 
-				// https://www.zohoapis.com/phonebridge/v3/callnotify?type=dailed&state=ended&id=10030&from=123456789&to=12300000001&start_time=2024-12-04 11:32:15&duration=325
-				// getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=dailed&state=ended&id=%s&from=%s&to=%s&start_time=%s&duration=%d", ZOHO_URL, callId, event["CallerIDNum"], event["ConnectedLineNum"], startTime, durationTime)
-				getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=dailed&state=ended&id=%s&from=%s&to=%s&start_time=%s&duration=%d", ZOHO_URL, event["Linkedid"], event["ConnectedLineNum"], event["CallerIDNum"], startTime, durationTime)
-				// if zohoUser != "" {
-				// 	getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
-				// }
-				fmt.Println("getURL = ", getURL)
-				go httpclient.ZohoGet(getURL)
 				// Incoming Call - Ended
 				// 	// if event["Context"] == "macro-stdexten" { // Context:DialPlan1
-			} else { // 开启语音留言:macro-stdexten  关闭语音留言:macro-stdexten-withoutvm
+				// } else if Channel[0] != "Local" && durationTime != -1 { // 开启语音留言:macro-stdexten  关闭语音留言:macro-stdexten-withoutvm
+			} else if (event["Context"] == "macro-stdexten" || event["Context"] == "macro-stdexten-withoutvm") && Channel[0] != "Local" && durationTime != -1 {
 				// if event["Context"] != "macro-trunkdial-failover" {
 				fmt.Println("Linkedid = ", event["Timestamp"])
+				if event["Priority"] == "1" {
+					getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=ended&id=%s&from=%s&to=%s&start_time=%s&duration=%d", ZOHO_URL, event["Linkedid"], event["ConnectedLineNum"], event["CallerIDNum"], startTime, durationTime)
+					// getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=ended&id=%s&from=%s&to=%s&start_time=%s&duration=%d", ZOHO_URL, callId, event["ConnectedLineNum"], event["CallerIDName"], startTime, durationTime)
+					// if zohoUser != "" {
+					// 	getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
+					// }
+					fmt.Println("getURL = ", getURL)
+					go httpclient.ZohoGet(getURL)
+				} else {
+					getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=ended&id=%s&from=%s&to=%s&start_time=%s&duration=%d", ZOHO_URL, event["Linkedid"], event["CallerIDNum"], event["ConnectedLineNum"], startTime, durationTime)
+					// getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=ended&id=%s&from=%s&to=%s&start_time=%s&duration=%d", ZOHO_URL, callId, event["ConnectedLineNum"], event["CallerIDName"], startTime, durationTime)
+					// if zohoUser != "" {
+					// 	getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
+					// }
+					fmt.Println("getURL = ", getURL)
+					go httpclient.ZohoGet(getURL)
+				}
 				// 还需优化判断,开启语音留言时,主叫挂断才是 macro-stdexten , 被叫挂断是 DialPlan1
 				//              关闭语音留言时,主叫挂断才是 macro-stdexten-withoutvm , 被叫挂断是 DialPlan1
 				// 如果 Outgoing Call 只会是 macro-trunkdial-failover 的话,那么和以下判断换一下,else 不判断,都为 Incoming Call 处理
@@ -374,20 +414,37 @@ func CallNotify(event map[string]string) {
 				// 获取时间
 				// durationTime := getDuration(event["Uniqueid"], event["Timestamp"])
 				// startTime := StartTimeList[event["Uniqueid"]].Format("2006-01-02 15:04:09") // 存储从string 改为 time.Time
-				durationTime := getDuration(event["Linkedid"], event["Timestamp"])
-				startTimeutc := StartTimeList[event["Linkedid"]].In(time.UTC) // 存储从string 改为 time.Time
-				startTime := startTimeutc.Format(time.RFC3339)
-				fmt.Println("startTime: ", startTime)
+				// durationTime := getDuration(event["Linkedid"], event["Timestamp"])
+				// startTimeutc := StartTimeList[event["Linkedid"]].In(time.UTC) // 存储从string 改为 time.Time
+				// startTime := startTimeutc.Format(time.RFC3339)
+				// fmt.Println("startTime: ", startTime)
 
 				// https://www.zohoapis.com/phonebridge/v3/callnotify?type=received&state=ended&id=100&from=12300000001&to=123456789&start_time=2024-12-04 10:32:10&duration=223
-				getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=ended&id=%s&from=%s&to=%s&start_time=%s&duration=%d", ZOHO_URL, event["Linkedid"], event["CallerIDNum"], event["ConnectedLineNum"], startTime, durationTime)
+
+			}
+		}
+	} else if event["Event"] == "Cdr" {
+		if event["Disposition"] == "ANSWERED" {
+			startTimeutc := StartTimeList[LinkedidList["Linkedid"]].In(time.UTC) // 存储从string 改为 time.Time
+			startTime := startTimeutc.Format(time.RFC3339)
+			CallSrcNum := strings.Split(strings.Split(event["CallSrcNum"], "<")[1], ">")[0]
+			if event["CallType"] == "outgoing" {
+				getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=dailed&state=ended&id=%s&from=%s&to=%s&start_time=%s&duration=%s", ZOHO_URL, LinkedidList["Linkedid"], CallSrcNum, event["CallDestNum"], startTime, event["Duration"])
+				// if zohoUser != "" {
+				// 	getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
+				// }
+				fmt.Println("getURL = ", getURL)
+				go httpclient.ZohoGet(getURL)
+				delete(LinkedidList, "Linkedid")
+			} else if event["CallType"] == "incoming" {
+				getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=ended&id=%s&from=%s&to=%s&start_time=%s&duration=%s", ZOHO_URL, LinkedidList["Linkedid"], CallSrcNum, event["CallDestNum"], startTime, event["Duration"])
 				// getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=ended&id=%s&from=%s&to=%s&start_time=%s&duration=%d", ZOHO_URL, callId, event["ConnectedLineNum"], event["CallerIDName"], startTime, durationTime)
 				// if zohoUser != "" {
 				// 	getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
 				// }
 				fmt.Println("getURL = ", getURL)
 				go httpclient.ZohoGet(getURL)
-
+				delete(LinkedidList, "Linkedid")
 			}
 		}
 	}
@@ -407,20 +464,20 @@ func getDuration(linkedid string, timeStamp string) int { // 都改为 Linkedid
 	fmt.Println("startTimeStamp Str: ", TimestampList[linkedid])
 	if TimestampList[linkedid] == "" || timeStamp == "" {
 		fmt.Printf("linkedid 或 timeStamp 为空\n")
-		return 9999
+		return -1
 	}
 
 	startTimeStamp, err := strconv.ParseFloat(TimestampList[linkedid], 64)
 	if err != nil {
 		fmt.Printf("startTimeStamp转换出错: %v\n", err)
-		return 9999
+		return -1
 	}
 	fmt.Printf("startTimeStamp=%f\n", startTimeStamp)
 
 	endTimeStamp, err := strconv.ParseFloat(timeStamp, 64)
 	if err != nil {
 		fmt.Printf("endTimeStamp转换出错: %v\n", err)
-		return 9999
+		return -1
 	}
 	fmt.Printf("endTimeStamp=%f\n", endTimeStamp)
 

BIN
deployments/crm-api