zoho.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package ami
  2. // import (
  3. // "fmt"
  4. // "pms-api-go/pkg/configs"
  5. // "pms-api-go/pkg/httpclient"
  6. // "pms-api-go/pkg/lfshook"
  7. // "strconv"
  8. // "strings"
  9. // "time"
  10. // "gopkg.in/ini.v1"
  11. // )
  12. // var TimestampList = make(map[string]string)
  13. // var StartTimeList = make(map[string]time.Time) // 事件中 Timestamp 转 StartTime 有问题,重新记录时间
  14. // var CallTypeList = make(map[string]string) // 临时记录 incoming 和 outgoing ,为incoming时: HangupRequest 中如果CallerIDNum为被叫号码,则未接听
  15. // // var DialStatusList = make(map[string]string) // 临时记录 DialStatus:ANSWER NOANSWER
  16. // func callNotify(event map[string]string) {
  17. // fmt.Println("=========================================== ")
  18. // fmt.Println("event[] = ", event)
  19. // fmt.Println("event = ", event["Event"])
  20. // fmt.Println("AMIPushUrl = ", configs.PushConfigValue.AMIPushUrl)
  21. // fmt.Println("Channel = ", event["Channel"])
  22. // var callDest string
  23. // if event["Channel"] != "" {
  24. // callDest = strings.Split(strings.Split(event["Channel"], "/")[1], "-")[0]
  25. // fmt.Println("callDest = ", callDest)
  26. // }
  27. // // var callId string
  28. // var callId = "10000021" // 测试用,每次测试 +1
  29. // // event["Exten"] = "123456789" // 测试用
  30. // // fmt.Println("Exten = ", event["Exten"])
  31. // // 读取vtiger配置文件
  32. // confPath := "/etc/asterisk/pms_api.conf"
  33. // cfg, err := ini.Load(confPath)
  34. // if err != nil {
  35. // lfshook.NewLogger().Error(err)
  36. // return
  37. // }
  38. // ZOHO_URL := cfg.Section("general").Key("zohoUrl").String()
  39. // if ZOHO_URL == "" {
  40. // lfshook.NewLogger().Error("/etc/asterisk/pms_api.conf not set zohoUrl")
  41. // return
  42. // }
  43. // // var getURL string
  44. // fmt.Println("Context = ", event["Context"])
  45. // // 呼叫发起事件
  46. // // if event["Event"] == "DialBegin" && event["Context"] == "macro-stdexten" {
  47. // // if event["Event"] == "DialBegin" && strings.Compare(event["Context"], "macro-stdexten") == 0 {
  48. // if event["Event"] == "DialBegin" {
  49. // /* ===================先放这里测试用 ============================================================
  50. // // 记录开始时间
  51. // TimestampList[event["Uniqueid"]] = event["Timestamp"]
  52. // // StartTimeList[event["Uniqueid"]] = time.Now().Format("2006-01-02 15:04:09")
  53. // StartTimeList[event["Uniqueid"]] = time.Now()
  54. // /* ===================先放这里测试用 ============================================================
  55. // fmt.Printf("Uniqueid=%s\n", event["Uniqueid"])
  56. // fmt.Printf("Timestamp=%s\n", event["Timestamp"])
  57. // TimestampTmp, err := strconv.Atoi(event["Timestamp"]) // 不要
  58. // if err != nil {
  59. // fmt.Printf("转换出错: %v\n", err)
  60. // return
  61. // }
  62. // fmt.Printf("TimestampAtoi=%d\n", TimestampTmp)
  63. // TimestampTmp002, err := strconv.ParseFloat(event["Timestamp"], 64) // OK
  64. // if err != nil {
  65. // fmt.Printf("转换出错: %v\n", err)
  66. // return
  67. // }
  68. // fmt.Printf("TimestampParseFloat=%f\n", TimestampTmp002)
  69. // // * ===================================================================================== */
  70. // // Incoming Call - Ringing
  71. // if event["Context"] == "macro-stdexten" {
  72. // // if strings.Compare(event["Context"], "macro-stdexten") == 0 { // OK
  73. // // https://www.zohoapis.com/phonebridge/v3/callnotify?type=received&state=ringing&id=10033&from=12300000001&to=123456789
  74. // // getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=ringing&id=%s&from=%s&to=%s", ZOHO_URL, callId, event["Exten"], callDest) // 取得from to 可能不准
  75. // getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=ringing&id=%s&from=%s&to=%s", ZOHO_URL, callId, event["DestConnectedLineNum"], event["DestCallerIDNum"])
  76. // fmt.Println("getURL = ", getURL)
  77. // go httpclient.ZohoGet(getURL)
  78. // // Outgoing Call - Ringing
  79. // } else if event["Context"] == "macro-trunkdial-failover" {
  80. // // https://www.zohoapis.com/phonebridge/v3/callnotify?type=dialed&state=ringing&id=10031&from=123456789&to=12300000001
  81. // getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=dialed&state=ringing&id=%s&from=%s&to=%s", ZOHO_URL, callId, event["DestConnectedLineNum"], event["DestCallerIDNum"])
  82. // fmt.Println("getURL = ", getURL)
  83. // go httpclient.ZohoGet(getURL)
  84. // }
  85. // // 呼叫已连接事件
  86. // } else if event["Event"] == "BridgeEnter" {
  87. // // Incoming Call - Answered
  88. // if event["Context"] == "macro-stdexten" {
  89. // // 记录开始时间
  90. // // TimestampList[event["Uniqueid"]] = event["Timestamp"] // Uniqueid 有时通话也不合适 NG // 都改为 Linkedid
  91. // // StartTimeList[event["Uniqueid"]] = time.Now() // Uniqueid 有时通话也不合适 NG // 都改为 Linkedid
  92. // TimestampList[event["Linkedid"]] = event["Timestamp"] // 呼入时设置 Uniqueid 不是 Linkedid
  93. // StartTimeList[event["Linkedid"]] = time.Now() // 呼入时设置 Uniqueid 不是 Linkedid
  94. // // https://www.zohoapis.com/phonebridge/v3/callnotify?type=received&state=answered&id=10023&from=12300000001&to=123456789
  95. // getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=answered&id=%s&from=%s&to=%s", ZOHO_URL, callId, event["CallerIDNum"], event["ConnectedLineNum"])
  96. // fmt.Println("getURL = ", getURL)
  97. // go httpclient.ZohoGet(getURL)
  98. // // Outgoing Call - Answered
  99. // } else if event["Context"] == "macro-trunkdial-failover" && event["Priority"] == "1" { // 注意这里选择 Priority:1 的,便于确认 CallerIDNum,ConnectedLineNum
  100. // // 记录开始时间
  101. // // TimestampList[event["Uniqueid"]] = event["Timestamp"] // error 需要把 Uniqueid 改为 Linkedid
  102. // // StartTimeList[event["Uniqueid"]] = time.Now() // error 需要把 Uniqueid 改为 Linkedid
  103. // TimestampList[event["Linkedid"]] = event["Timestamp"]
  104. // StartTimeList[event["Linkedid"]] = time.Now()
  105. // // https: //www.zohoapis.com/phonebridge/v3/callnotify?type=dialed&state=answered&id=10003&from=123456789&to=12300000001
  106. // // getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=dialed&state=answered&id=%s&from=%s&to=%s", ZOHO_URL, callId, event["CallerIDNum"], event["ConnectedLineNum"])
  107. // getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=dialed&state=answered&id=%s&from=%s&to=%s", ZOHO_URL, callId, event["ConnectedLineNum"], event["CallerIDNum"])
  108. // fmt.Println("getURL = ", getURL)
  109. // go httpclient.ZohoGet(getURL)
  110. // }
  111. // // 呼叫挂断事件
  112. // } else if event["Event"] == "HangupRequest" {
  113. // /* ===================先放这里测试用 ============================================================
  114. // // 记录结束时间
  115. // // TimestampList[event["Uniqueid"]] = event["Timestamp"]
  116. // // startTimeStamp := int64(TimestampList[event["Uniqueid"]])
  117. // fmt.Println("startTimeStamp Str: ", TimestampList[event["Uniqueid"]])
  118. // // keyTmp := event["Uniqueid"]
  119. // // fmt.Println("keyTmp: ", keyTmp)
  120. // // fmt.Println("startTimeStamp Str: ", TimestampList[keyTmp])
  121. // startTimeStamp, err := strconv.ParseFloat(TimestampList[event["Uniqueid"]], 64) // OK
  122. // if err != nil {
  123. // fmt.Printf("startTimeStamp转换出错: %v\n", err)
  124. // return
  125. // }
  126. // fmt.Printf("startTimeStamp=%f\n", startTimeStamp)
  127. // endTimeStamp, err := strconv.ParseFloat(event["Timestamp"], 64) // OK
  128. // if err != nil {
  129. // fmt.Printf("endTimeStamp转换出错: %v\n", err)
  130. // return
  131. // }
  132. // fmt.Printf("endTimeStamp=%f\n", endTimeStamp)
  133. // durationTime := time.Duration(endTimeStamp-startTimeStamp) * time.Second
  134. // fmt.Println("durationTime=", durationTime)
  135. // // 删除记录时间
  136. // // delete(TimestampList, event["Uniqueid"])
  137. // // * ===================================================================================== */
  138. // // Incoming Call - Ended
  139. // // if event["Context"] == "macro-stdexten" { // Context:DialPlan1
  140. // if event["Context"] != "macro-trunkdial-failover" {
  141. // // 获取时间
  142. // // durationTime := getDuration(event["Uniqueid"], event["Timestamp"])
  143. // // startTime := StartTimeList[event["Uniqueid"]].Format("2006-01-02 15:04:09") // 存储从string 改为 time.Time
  144. // durationTime := getDuration(event["Linkedid"], event["Timestamp"])
  145. // startTime := StartTimeList[event["Linkedid"]].Format("2006-01-02 15:04:09") // 存储从string 改为 time.Time
  146. // fmt.Println("startTime: ", startTime)
  147. // // 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
  148. // // 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["CallerIDNum"], event["ConnectedLineNum"], startTime, durationTime)
  149. // 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)
  150. // fmt.Println("getURL = ", getURL)
  151. // go httpclient.ZohoGet(getURL)
  152. // // Outgoing Call - Ended
  153. // } else if event["Context"] == "macro-trunkdial-failover" {
  154. // // 获取时间
  155. // // durationTime := getDuration(event["Uniqueid"], event["Timestamp"])
  156. // durationTime := getDuration(event["Linkedid"], event["Timestamp"])
  157. // // /* ===================测试用 ============================================================
  158. // // startTime, _ := time.Parse("2006-01-02 15:04:05", TimestampList[event["Uniqueid"]]) // error 1734328213.774410 => 0001-01-01 00:00:00 +0000 UTC
  159. // // startTime := StartTimeList[event["Uniqueid"]]
  160. // // startTime := StartTimeList[event["Uniqueid"]].Format("2006-01-02 15:04:09") // 存储从string 改为 time.Time
  161. // startTime := StartTimeList[event["Linkedid"]].Format("2006-01-02 15:04:09") // 存储从string 改为 time.Time
  162. // fmt.Println("startTime: ", startTime)
  163. // // durationTime002 := getDuration002(startTime)
  164. // // durationTime002 := time.Now().Sub(StartTimeList[event["Uniqueid"]]) // ok durationTime002: 7.78408849s // should use time.Since instead of time.Now().Sub (S1012)
  165. // // durationTime002 := time.Since(StartTimeList[event["Uniqueid"]]) // ok durationTime002: 11.611233458s
  166. // // fmt.Println("durationTime002: ", durationTime002)
  167. // // * ===================================================================================== */
  168. // // 删除记录时间
  169. // delete(StartTimeList, event["Linkedid"])
  170. // // 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
  171. // 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)
  172. // // 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["ConnectedLineNum"], event["CallerIDNum"], startTime, durationTime)
  173. // fmt.Println("getURL = ", getURL)
  174. // go httpclient.ZohoGet(getURL)
  175. // }
  176. // }
  177. // // fmt.Println("getURL = ", getURL)
  178. // // 测试验证下上面有没有删除,以防TimestampList越来越大
  179. // for k, v := range TimestampList {
  180. // fmt.Printf("===Linkedid:%s Timestamp:%s===\n", k, v)
  181. // }
  182. // // 按编号搜索API 暂时用的以下接口
  183. // // group.GET("/vtiger/lookup", contactsInfo) // 按编号搜索
  184. // }
  185. // // 使用事件中event["Timestamp"]的值计算时间间隔
  186. // // func getDuration(uniqueid string, timeStamp string) int {
  187. // func getDuration(linkedid string, timeStamp string) int { // 都改为 Linkedid
  188. // fmt.Println("startTimeStamp Str: ", TimestampList[linkedid])
  189. // if TimestampList[linkedid] == "" || timeStamp == "" {
  190. // fmt.Printf("linkedid 或 timeStamp 为空\n")
  191. // return 9999
  192. // }
  193. // startTimeStamp, err := strconv.ParseFloat(TimestampList[linkedid], 64)
  194. // if err != nil {
  195. // fmt.Printf("startTimeStamp转换出错: %v\n", err)
  196. // return 9999
  197. // }
  198. // fmt.Printf("startTimeStamp=%f\n", startTimeStamp)
  199. // endTimeStamp, err := strconv.ParseFloat(timeStamp, 64)
  200. // if err != nil {
  201. // fmt.Printf("endTimeStamp转换出错: %v\n", err)
  202. // return 9999
  203. // }
  204. // fmt.Printf("endTimeStamp=%f\n", endTimeStamp)
  205. // durationTime := time.Duration(endTimeStamp-startTimeStamp) * time.Second
  206. // // fmt.Println("durationTime=", durationTime) // durationTime= 35s
  207. // // fmt.Printf("durationTime=%d\n", durationTime) // durationTime=35000000000
  208. // // fmt.Printf("durationTime=%d\n", durationTime.Seconds()) // durationTime=%!d(float64=35)
  209. // // fmt.Printf("durationTime=%f\n", durationTime.Seconds()) // durationTime=35.000000
  210. // fmt.Printf("durationTime=%d\n", int(durationTime.Seconds())) // durationTime=35
  211. // // durationTime001 := time.Duration(endTimeStamp - startTimeStamp)
  212. // // fmt.Println("durationTime001=", durationTime001) // durationTime001= 35ns
  213. // // durationTime003 := time.Duration(endTimeStamp - startTimeStamp).Seconds()
  214. // // fmt.Println("durationTime003=", durationTime003) // durationTime003= 3.5e-08
  215. // // 删除记录时间
  216. // delete(TimestampList, linkedid)
  217. // // return durationTime
  218. // return int(durationTime.Seconds())
  219. // }
  220. // // // 程序中取时间计算间隔多少秒
  221. // // func getDuration002(startTime string) time.Duration {
  222. // // // durationTime := endTime.Sub(startTime)
  223. // // startTime002, err := time.Parse("2006-01-02 15:04:05", startTime)
  224. // // if err != nil {
  225. // // fmt.Println("Error parsing time:", err)
  226. // // return 0
  227. // // }
  228. // // durationTime := time.Now().Sub(startTime002)
  229. // // return durationTime
  230. // // }