push.go 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. package zoho
  2. import (
  3. "crm-api/internal/app/mysql"
  4. "crm-api/pkg/httpclient"
  5. "crm-api/pkg/lfshook"
  6. "encoding/json"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "slices"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "gopkg.in/ini.v1"
  15. )
  16. var TimestampList = make(map[string]string)
  17. var StartTimeList = make(map[string]time.Time) // 事件中 Timestamp 转 StartTime 有问题,重新记录时间
  18. var LinkedidList = make(map[string]string) // 临时记录 incoming 和 outgoing
  19. var DialStatusList = make(map[string]string) // 临时记录 DialStatus:ANSWER NOANSWER
  20. var CallTypeList = make(map[string]string) //通过Linkedid临时记录拨打类型呼入还是呼出
  21. var TimetimesList = make(map[string]int)
  22. var AttendedTransferList = make(map[string]string)
  23. var ZohoExtenList = make([]string, 0)
  24. var n int
  25. func CallClicktodialerror(event map[string]string) {
  26. confPath := "/etc/asterisk/crm_api.conf"
  27. cfg, err := ini.Load(confPath)
  28. if err != nil {
  29. lfshook.NewLogger().Error(err)
  30. return
  31. }
  32. ZOHO_URL := cfg.Section("general").Key("zohoUrl").String()
  33. if ZOHO_URL == "" {
  34. lfshook.NewLogger().Error("/etc/asterisk/crm_api.conf not set zohoUrl")
  35. return
  36. }
  37. Channel := strings.Split(event["Channel"], "/")
  38. // fmt.Println("Channel[0] = ", Channel[0])
  39. if Channel[0] == "Local" {
  40. if event["Event"] == "SoftHangupRequest" {
  41. if (event["Context"] == "macro-stdexten" || event["Context"] == "macro-stdexten-withoutvm") && len(event["Exten"]) > 3 {
  42. ExtenStatus := strings.Split(event["Exten"], "-")
  43. fmt.Println(len(event["Exten"]))
  44. if ExtenStatus[1] == "NOANSWER" && slices.Contains(ZohoExtenList, event["ConnectedLineNum"]) {
  45. getURL := fmt.Sprintf("%s/phonebridge/v3/clicktodialerror?code=noanswer&from=%s&to=%s", ZOHO_URL, event["ConnectedLineNum"], event["CallerIDNum"])
  46. // if zohoUser != "" {
  47. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  48. // }
  49. fmt.Println("getURL = ", getURL)
  50. go httpclient.ZohoGet(getURL)
  51. return
  52. } else if ExtenStatus[1] == "BUSY" {
  53. ChannelStatus1 := strings.Split(event["Channel"], "@")
  54. ChannelStatus2 := strings.Split(ChannelStatus1[0], "/")
  55. if slices.Contains(ZohoExtenList, ChannelStatus2[1]) {
  56. getURL := fmt.Sprintf("%s/phonebridge/v3/clicktodialerror?code=notavailble&from=%s&to=%s&message=agentunavailable", ZOHO_URL, ChannelStatus2[1], event["CallerIDNum"])
  57. // if zohoUser != "" {
  58. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  59. // }
  60. fmt.Println("getURL = ", getURL)
  61. go httpclient.ZohoGet(getURL)
  62. return
  63. }
  64. } else if ExtenStatus[1] == "CHANUNAVAIL" && slices.Contains(ZohoExtenList, event["ConnectedLineNum"]) {
  65. getURL := fmt.Sprintf("%s/phonebridge/v3/clicktodialerror?code=rejected&from=%s&to=%s", ZOHO_URL, event["ConnectedLineNum"], event["CallerIDNum"])
  66. // if zohoUser != "" {
  67. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  68. // }
  69. fmt.Println("getURL = ", getURL)
  70. go httpclient.ZohoGet(getURL)
  71. return
  72. }
  73. }
  74. } else if event["Event"] == "DialEnd" {
  75. if (event["Context"] == "macro-stdexten" || event["Context"] == "macro-stdexten-withoutvm") && event["DialStatus"] == "CANCEL" && slices.Contains(ZohoExtenList, event["ConnectedLineNum"]) {
  76. getURL := fmt.Sprintf("%s/phonebridge/v3/clicktodialerror?code=noanswer&from=%s&to=%s", ZOHO_URL, event["ConnectedLineNum"], event["CallerIDNum"])
  77. // if zohoUser != "" {
  78. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  79. // }
  80. fmt.Println("getURL = ", getURL)
  81. go httpclient.ZohoGet(getURL)
  82. return
  83. }
  84. }
  85. }
  86. CallNotify(event)
  87. }
  88. func CallNotify(event map[string]string) {
  89. confPath := "/etc/asterisk/crm_api.conf"
  90. cfg, err := ini.Load(confPath)
  91. if err != nil {
  92. lfshook.NewLogger().Error(err)
  93. return
  94. }
  95. ZOHO_URL := cfg.Section("general").Key("zohoUrl").String()
  96. if ZOHO_URL == "" {
  97. lfshook.NewLogger().Error("/etc/asterisk/crm_api.conf not set zohoUrl")
  98. return
  99. }
  100. // var getURL string
  101. // fmt.Println("Context = ", event["Context"])
  102. // 呼叫发起事件
  103. // if event["Event"] == "DialBegin" && event["Context"] == "macro-stdexten" {
  104. // if event["Event"] == "DialBegin" && strings.Compare(event["Context"], "macro-stdexten") == 0 {
  105. if event["Event"] == "DialBegin" {
  106. lfshook.NewLogger().Error(event)
  107. // if TimetimesList[event["Linkedid"]] != 0 {
  108. // TimetimesList[event["Linkedid"]] = TimetimesList[event["Linkedid"]] + 1
  109. // } else {
  110. // TimetimesList[event["Linkedid"]] = 1
  111. // }
  112. Channel := strings.Split(event["Channel"], "/")
  113. fmt.Println("Channel[0] = ", Channel[0])
  114. // Outgoing Call - Ringing
  115. if event["Context"] == "macro-trunkdial-failover" && slices.Contains(ZohoExtenList, event["CallerIDNum"]) {
  116. lfshook.NewLogger().Info("Outgoing Call - Ringing")
  117. fmt.Println("event[] = ", event)
  118. fmt.Println("event = ", event["Event"])
  119. fmt.Println("Linkedid = ", event["Timestamp"])
  120. StartTimeList[event["Linkedid"]] = time.Now().Add(-8 * time.Hour)
  121. // https://www.zohoapis.com/phonebridge/v3/callnotify?type=dialed&state=ringing&id=10031&from=123456789&to=12300000001
  122. getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=dialed&state=ringing&id=%s&from=%s&to=%s", ZOHO_URL, event["Linkedid"], event["CallerIDNum"], event["DestCallerIDNum"])
  123. // if zohoUser != "" {
  124. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  125. // }
  126. fmt.Println("getURL = ", getURL)
  127. go httpclient.ZohoGet(getURL)
  128. // Incoming Call - Ringing
  129. // if event["Context"] == "macro-stdexten" {
  130. } else if (event["Context"] == "macro-stdexten" && Channel[0] != "Local" && slices.Contains(ZohoExtenList, event["DestCallerIDNum"])) || (event["Context"] == "macro-stdexten-withoutvm" && Channel[0] != "Local" && slices.Contains(ZohoExtenList, event["DestCallerIDNum"])) { // 开启语音留言:macro-stdexten 关闭语音留言:macro-stdexten-withoutvm
  131. lfshook.NewLogger().Error("Incoming Call - Ringing")
  132. fmt.Println("event[] = ", event)
  133. fmt.Println("event = ", event["Event"])
  134. StartTimeList[event["Linkedid"]] = time.Now().Add(-8 * time.Hour)
  135. fmt.Println("Linkedid = ", event["Timestamp"])
  136. // if strings.Compare(event["Context"], "macro-stdexten") == 0 { // OK
  137. // https://www.zohoapis.com/phonebridge/v3/callnotify?type=received&state=ringing&id=10033&from=12300000001&to=123456789
  138. // 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 可能不准
  139. getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=ringing&id=%s&from=%s&to=%s", ZOHO_URL, event["Linkedid"], event["CallerIDNum"], event["DestCallerIDNum"])
  140. // if zohoUser != "" {
  141. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  142. // }
  143. fmt.Println("getURL = ", getURL)
  144. go httpclient.ZohoGet(getURL)
  145. } else if (strings.HasPrefix(event["Context"], "department") || strings.HasSuffix(event["Context"], "queue")) && slices.Contains(ZohoExtenList, event["DestCallerIDNum"]) {
  146. TimetimesList[event["Linkedid"]] = TimetimesList[event["Linkedid"]] + 1
  147. StartTimeList[event["Linkedid"]] = time.Now().Add(-8 * time.Hour)
  148. getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=ringing&id=%s&from=%s&to=%s", ZOHO_URL, event["Linkedid"], event["CallerIDNum"], event["DestCallerIDNum"])
  149. // if zohoUser != "" {
  150. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  151. // }
  152. fmt.Println("getURL = ", getURL)
  153. go httpclient.ZohoGet(getURL)
  154. }
  155. // 呼叫结束事件
  156. } else if event["Event"] == "DialEnd" {
  157. Channel := strings.Split(event["Channel"], "/")
  158. // 记录呼叫状态
  159. DialStatusList[event["Linkedid"]] = event["DialStatus"]
  160. fmt.Println("DialEnd[] = ", event)
  161. fmt.Println("Channel[0] = ", Channel[0])
  162. // Outgoing Call - Unattended
  163. if event["Context"] == "macro-trunkdial-failover" && event["DialStatus"] == "CANCEL" && slices.Contains(ZohoExtenList, event["CallerIDNum"]) {
  164. lfshook.NewLogger().Info("Outgoing Call - CANCEL")
  165. fmt.Println("event[] = ", event)
  166. fmt.Println("event = ", event["Event"])
  167. fmt.Println("Linkedid = ", event["Timestamp"])
  168. // startTime := StartTimeList[event["Linkedid"]].Format("2006-01-02 15:04:09")
  169. startTimeutc := StartTimeList[event["Linkedid"]].In(time.UTC) // 存储从string 改为 time.Time
  170. startTime := startTimeutc.Format(time.RFC3339)
  171. fmt.Println("startTime: ", startTime)
  172. // https://www.zohoapis.com/phonebridge/v3/callnotify?type=dialed&state=busy&id=10025&from=123456789&to=12300000001&start_time=2024-12-04 15:09:10
  173. getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=dialed&state=busy&id=%s&from=%s&to=%s&start_time=%s", ZOHO_URL, event["Linkedid"], event["CallerIDNum"], event["DestCallerIDNum"], startTime)
  174. // if zohoUser != "" {
  175. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  176. // }
  177. fmt.Println("getURL = ", getURL)
  178. go httpclient.ZohoGet(getURL)
  179. delete(StartTimeList, event["Linkedid"])
  180. // Incoming Call - Missed
  181. // if event["Context"] == "macro-stdexten" && event["DialStatus"] != "ANSWER" {
  182. } else if event["Context"] == "macro-trunkdial-failover" && event["DialStatus"] == "NOANSWER" && slices.Contains(ZohoExtenList, event["CallerIDNum"]) {
  183. lfshook.NewLogger().Info("Outgoing Call - NOANSWER")
  184. fmt.Println("event[] = ", event)
  185. fmt.Println("event = ", event["Event"])
  186. fmt.Println("Linkedid = ", event["Timestamp"])
  187. // startTime := StartTimeList[event["Linkedid"]].Format("2006-01-02 15:04:09")
  188. startTimeutc := StartTimeList[event["Linkedid"]].In(time.UTC) // 存储从string 改为 time.Time
  189. startTime := startTimeutc.Format(time.RFC3339)
  190. fmt.Println("startTime: ", startTime)
  191. // https://www.zohoapis.com/phonebridge/v3/callnotify?type=dialed&state=busy&id=10025&from=123456789&to=12300000001&start_time=2024-12-04 15:09:10
  192. getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=dialed&state=noanswer&id=%s&from=%s&to=%s&start_time=%s", ZOHO_URL, event["Linkedid"], event["CallerIDNum"], event["DestCallerIDNum"], startTime)
  193. // if zohoUser != "" {
  194. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  195. // }
  196. fmt.Println("getURL = ", getURL)
  197. go httpclient.ZohoGet(getURL)
  198. delete(StartTimeList, event["Linkedid"])
  199. // Incoming Call - Missed
  200. // if event["Context"] == "macro-stdexten" && event["DialStatus"] != "ANSWER" {
  201. } else if event["Context"] == "macro-trunkdial-failover" && event["DialStatus"] == "BUSY" && slices.Contains(ZohoExtenList, event["CallerIDNum"]) {
  202. lfshook.NewLogger().Info("Outgoing Call - BUSY")
  203. fmt.Println("event[] = ", event)
  204. fmt.Println("event = ", event["Event"])
  205. fmt.Println("Linkedid = ", event["Timestamp"])
  206. // startTime := StartTimeList[event["Linkedid"]].Format("2006-01-02 15:04:09")
  207. startTimeutc := StartTimeList[event["Linkedid"]].In(time.UTC) // 存储从string 改为 time.Time
  208. startTime := startTimeutc.Format(time.RFC3339)
  209. fmt.Println("startTime: ", startTime)
  210. // https://www.zohoapis.com/phonebridge/v3/callnotify?type=dialed&state=busy&id=10025&from=123456789&to=12300000001&start_time=2024-12-04 15:09:10
  211. getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=dialed&state=rejected&id=%s&from=%s&to=%s&start_time=%s", ZOHO_URL, event["Linkedid"], event["CallerIDNum"], event["DestCallerIDNum"], startTime)
  212. // if zohoUser != "" {
  213. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  214. // }
  215. fmt.Println("getURL = ", getURL)
  216. go httpclient.ZohoGet(getURL)
  217. delete(StartTimeList, event["Linkedid"])
  218. // Incoming Call - Missed
  219. // if event["Context"] == "macro-stdexten" && event["DialStatus"] != "ANSWER" {
  220. } else if (event["Context"] == "macro-stdexten" && event["DialStatus"] != "ANSWER" && Channel[0] != "Local" && slices.Contains(ZohoExtenList, event["DestCallerIDNum"])) || (event["Context"] == "macro-stdexten-withoutvm" && event["DialStatus"] != "ANSWER" && Channel[0] != "Local" && slices.Contains(ZohoExtenList, event["DestCallerIDNum"])) { // 开启语音留言:macro-stdexten 关闭语音留言:macro-stdexten-withoutvm
  221. lfshook.NewLogger().Error("Incoming Call - Missed")
  222. fmt.Println("event[] = ", event)
  223. fmt.Println("event = ", event["Event"])
  224. // startTime := StartTimeList[event["Linkedid"]].Format("2006-01-02 15:04:09")
  225. startTimeutc := StartTimeList[event["Linkedid"]].In(time.UTC) // 存储从string 改为 time.Time
  226. startTime := startTimeutc.Format(time.RFC3339)
  227. fmt.Println("startTime: ", startTime)
  228. fmt.Println("Linkedid = ", event["Timestamp"])
  229. // https://www.zohoapis.com/phonebridge/v3/callnotify?type=received&state=missed&id=10005&from=12300000001&to=123456789&start_time=2024-12-04 15:09:10
  230. getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=missed&id=%s&from=%s&to=%s&start_time=%s", ZOHO_URL, event["Linkedid"], event["CallerIDNum"], event["DestCallerIDNum"], startTime)
  231. // if zohoUser != "" {
  232. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  233. // }
  234. fmt.Println("getURL = ", getURL)
  235. go httpclient.ZohoGet(getURL)
  236. delete(StartTimeList, event["Linkedid"])
  237. } else if (strings.HasPrefix(event["Context"], "department") || strings.HasSuffix(event["Context"], "queue")) && event["DialStatus"] != "ANSWER" && TimetimesList[event["Linkedid"]] < 2 && TimetimesList[event["Linkedid"]] > 0 && slices.Contains(ZohoExtenList, event["DestCallerIDNum"]) {
  238. startTimeutc := StartTimeList[event["Linkedid"]].In(time.UTC) // 存储从string 改为 time.Time
  239. startTime := startTimeutc.Format(time.RFC3339)
  240. getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=missed&id=%s&from=%s&to=%s&start_time=%s", ZOHO_URL, event["Linkedid"], event["CallerIDNum"], event["DestCallerIDNum"], startTime)
  241. // if zohoUser != "" {
  242. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  243. // }
  244. fmt.Println("getURL = ", getURL)
  245. go httpclient.ZohoGet(getURL)
  246. delete(TimetimesList, event["Linkedid"])
  247. } else if (strings.HasPrefix(event["Context"], "department") || strings.HasSuffix(event["Context"], "queue")) && event["DialStatus"] != "ANSWER" && TimetimesList[event["Linkedid"]] >= 2 && slices.Contains(ZohoExtenList, event["DestCallerIDNum"]) {
  248. n = n + 1
  249. if TimetimesList[event["Linkedid"]] == n {
  250. lfshook.NewLogger().Error("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh")
  251. startTimeutc := StartTimeList[event["Linkedid"]].In(time.UTC) // 存储从string 改为 time.Time
  252. startTime := startTimeutc.Format(time.RFC3339)
  253. getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=missed&id=%s&from=%s&to=%s&start_time=%s", ZOHO_URL, event["Linkedid"], event["CallerIDNum"], event["DestCallerIDNum"], startTime)
  254. // if zohoUser != "" {
  255. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  256. // }
  257. fmt.Println("getURL = ", getURL)
  258. go httpclient.ZohoGet(getURL)
  259. delete(TimetimesList, event["Linkedid"])
  260. n = 0
  261. }
  262. } else if event["DialStatus"] == "ANSWER" {
  263. delete(TimetimesList, event["Linkedid"])
  264. }
  265. // 呼叫已连接事件
  266. } else if event["Event"] == "BridgeEnter" {
  267. Channel := strings.Split(event["Channel"], "/")
  268. fmt.Println("Channel[0] = ", Channel[0])
  269. // Outgoing Call - Answered
  270. if event["Context"] == "macro-trunkdial-failover" && event["Priority"] == "1" && slices.Contains(ZohoExtenList, event["ConnectedLineNum"]) { // 注意这里选择 Priority:1 的,便于确认 CallerIDNum,ConnectedLineNum
  271. // 记录开始时间
  272. // TimestampList[event["Uniqueid"]] = event["Timestamp"] // error 需要把 Uniqueid 改为 Linkedid
  273. // StartTimeList[event["Uniqueid"]] = time.Now() // error 需要把 Uniqueid 改为 Linkedid
  274. lfshook.NewLogger().Info("Outgoing Call - Answered")
  275. fmt.Println("event[] = ", event)
  276. fmt.Println("event = ", event["Event"])
  277. fmt.Println("Linkedid = ", event["Timestamp"])
  278. TimestampList[event["Linkedid"]] = event["Timestamp"]
  279. StartTimeList[event["Linkedid"]] = time.Now().Add(-8 * time.Hour)
  280. LinkedidList["Linkedid"] = event["Linkedid"]
  281. CallTypeList[event["Linkedid"]] = "outgoing"
  282. // https: //www.zohoapis.com/phonebridge/v3/callnotify?type=dialed&state=answered&id=10003&from=123456789&to=12300000001
  283. // getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=dialed&state=answered&id=%s&from=%s&to=%s", ZOHO_URL, callId, event["CallerIDNum"], event["ConnectedLineNum"])
  284. 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"])
  285. // if zohoUser != "" {
  286. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  287. // }
  288. fmt.Println("getURL = ", getURL)
  289. go httpclient.ZohoGet(getURL)
  290. // Incoming Call - Answered
  291. // if event["Context"] == "macro-stdexten" {
  292. } else if (event["Context"] == "macro-stdexten" || event["Context"] == "macro-stdexten-withoutvm") && Channel[0] != "Local" && slices.Contains(ZohoExtenList, event["ConnectedLineNum"]) && event["SwapUniqueid"] == "" { // 开启语音留言:macro-stdexten 关闭语音留言:macro-stdexten-withoutvm
  293. // 记录开始时间
  294. // TimestampList[event["Uniqueid"]] = event["Timestamp"] // Uniqueid 有时通话也不合适 NG // 都改为 Linkedid
  295. // StartTimeList[event["Uniqueid"]] = time.Now() // Uniqueid 有时通话也不合适 NG // 都改为 Linkedid
  296. lfshook.NewLogger().Error("Incoming Call - Answered")
  297. fmt.Println("event[] = ", event)
  298. fmt.Println("event = ", event["Event"])
  299. fmt.Println("Linkedid = ", event["Timestamp"])
  300. TimestampList[event["Linkedid"]] = event["Timestamp"] // 呼入时设置 Uniqueid 不是 Linkedid
  301. StartTimeList[event["Linkedid"]] = time.Now().Add(-8 * time.Hour) // 呼入时设置 Uniqueid 不是 Linkedid
  302. LinkedidList["Linkedid"] = event["Linkedid"]
  303. CallTypeList[event["Linkedid"]] = "incoming"
  304. // https://www.zohoapis.com/phonebridge/v3/callnotify?type=received&state=answered&id=10023&from=12300000001&to=123456789
  305. 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"])
  306. // if zohoUser != "" {
  307. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  308. // }
  309. fmt.Println("getURL = ", getURL)
  310. go httpclient.ZohoGet(getURL)
  311. } else if (strings.HasPrefix(event["Context"], "department") && event["Priority"] == "1" && event["SwapUniqueid"] == "") || (strings.HasSuffix(event["Context"], "queue") && event["SwapUniqueid"] == "") {
  312. TimestampList[event["Linkedid"]] = event["Timestamp"] // 呼入时设置 Uniqueid 不是 Linkedid
  313. StartTimeList[event["Linkedid"]] = time.Now().Add(-8 * time.Hour) // 呼入时设置 Uniqueid 不是 Linkedid
  314. LinkedidList["Linkedid"] = event["Linkedid"]
  315. CallTypeList[event["Linkedid"]] = "incoming"
  316. if event["Priority"] == "1" && slices.Contains(ZohoExtenList, event["CallerIDNum"]) {
  317. getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=answered&id=%s&from=%s&to=%s", ZOHO_URL, event["Linkedid"], event["ConnectedLineNum"], event["CallerIDNum"])
  318. // if zohoUser != "" {
  319. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  320. // }
  321. fmt.Println("getURL = ", getURL)
  322. go httpclient.ZohoGet(getURL)
  323. } else if slices.Contains(ZohoExtenList, event["ConnectedLineNum"]) {
  324. 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"])
  325. // if zohoUser != "" {
  326. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  327. // }
  328. fmt.Println("getURL = ", getURL)
  329. go httpclient.ZohoGet(getURL)
  330. }
  331. delete(TimetimesList, event["Linkedid"])
  332. n = 0
  333. }
  334. // 呼叫挂断事件
  335. } else if event["Event"] == "HangupRequest" {
  336. // * ===================================================================================== */
  337. // 判断呼叫状态,为 ANSWER 时才推送,否则见以上推送 Outgoing Call - Unattended 或 Incoming Call - Missed
  338. Channel := strings.Split(event["Channel"], "/")
  339. fmt.Println("event[] = ", event)
  340. fmt.Println("Channel[0] = ", Channel[0])
  341. // lfshook.NewLogger().Errorf("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa %+v", event["Context"])
  342. fmt.Println("ChannelHangupRequest1 = ", DialStatusList[event["Linkedid"]])
  343. fmt.Println("ChannelHangupRequest1 = ", event["DialStatus"])
  344. // if DialStatusList[event["Linkedid"]] == "ANSWER" {//暂时不使用dialend的呼叫状态
  345. lfshook.NewLogger().Errorf("ChannelHangupRequest = %+v", event["Uniqueid"])
  346. fmt.Println("event[] = ", event)
  347. fmt.Println("event = ", event["Event"])
  348. // Outgoing Call - Ended
  349. if event["Context"] == "macro-trunkdial-failover" && CallTypeList[event["Linkedid"]] == "outgoing" {
  350. startTimeutc := StartTimeList[event["Linkedid"]].In(time.UTC) // 存储从string 改为 time.Time
  351. startTime := startTimeutc.Format(time.RFC3339)
  352. fmt.Println("startTime: ", startTime)
  353. fmt.Println("Linkedid = ", event["Timestamp"])
  354. durationTime := getDuration(event["Linkedid"], event["Timestamp"])
  355. lfshook.NewLogger().Errorf("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaCallTypeList %+v", CallTypeList)
  356. if durationTime != -1 {
  357. if event["Priority"] == "1" && slices.Contains(ZohoExtenList, event["ConnectedLineNum"]) {
  358. // 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
  359. // 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)
  360. 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)
  361. // if zohoUser != "" {
  362. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  363. // }
  364. fmt.Println("getURL = ", getURL)
  365. go httpclient.ZohoGet(getURL)
  366. } else if slices.Contains(ZohoExtenList, event["CallerIDNum"]) {
  367. // 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
  368. // 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)
  369. 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)
  370. // if zohoUser != "" {
  371. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  372. // }
  373. fmt.Println("getURL = ", getURL)
  374. go httpclient.ZohoGet(getURL)
  375. }
  376. }
  377. // 删除记录时间
  378. delete(StartTimeList, event["Linkedid"])
  379. delete(CallTypeList, event["Linkedid"])
  380. // Incoming Call - Ended
  381. // // if event["Context"] == "macro-stdexten" { // Context:DialPlan1
  382. // } else if Channel[0] != "Local" && durationTime != -1 { // 开启语音留言:macro-stdexten 关闭语音留言:macro-stdexten-withoutvm
  383. } else if Channel[0] != "Local" && CallTypeList[event["Linkedid"]] == "incoming" {
  384. // if event["Context"] != "macro-trunkdial-failover" {
  385. startTimeutc := StartTimeList[event["Linkedid"]].In(time.UTC) // 存储从string 改为 time.Time
  386. startTime := startTimeutc.Format(time.RFC3339)
  387. fmt.Println("startTime: ", startTime)
  388. fmt.Println("Linkedid = ", event["Timestamp"])
  389. durationTime := getDuration(event["Linkedid"], event["Timestamp"])
  390. if durationTime != -1 {
  391. if event["Priority"] == "1" && slices.Contains(ZohoExtenList, event["CallerIDNum"]) {
  392. 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)
  393. // 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)
  394. // if zohoUser != "" {
  395. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  396. // }
  397. fmt.Println("getURL = ", getURL)
  398. go httpclient.ZohoGet(getURL)
  399. } else if slices.Contains(ZohoExtenList, event["ConnectedLineNum"]) {
  400. 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)
  401. // 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)
  402. // if zohoUser != "" {
  403. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  404. // }
  405. fmt.Println("getURL = ", getURL)
  406. go httpclient.ZohoGet(getURL)
  407. }
  408. }
  409. delete(StartTimeList, event["Linkedid"])
  410. delete(CallTypeList, event["Linkedid"])
  411. // 还需优化判断,开启语音留言时,主叫挂断才是 macro-stdexten , 被叫挂断是 DialPlan1
  412. // 关闭语音留言时,主叫挂断才是 macro-stdexten-withoutvm , 被叫挂断是 DialPlan1
  413. // 如果 Outgoing Call 只会是 macro-trunkdial-failover 的话,那么和以下判断换一下,else 不判断,都为 Incoming Call 处理
  414. } else if AttendedTransferList[event["Linkedid"]] != "" {
  415. startTimeutc := StartTimeList[AttendedTransferList[event["Linkedid"]]].In(time.UTC) // 存储从string 改为 time.Time
  416. startTime := startTimeutc.Format(time.RFC3339)
  417. fmt.Println("startTime: ", startTime)
  418. durationTime := getDuration(AttendedTransferList[event["Linkedid"]], event["Timestamp"])
  419. if event["Priority"] == "1" && slices.Contains(ZohoExtenList, event["CallerIDNum"]) {
  420. getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=ended&id=%s&from=%s&to=%s&start_time=%s&duration=%d", ZOHO_URL, AttendedTransferList[event["Linkedid"]], event["ConnectedLineNum"], event["CallerIDNum"], startTime, durationTime)
  421. // 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)
  422. // if zohoUser != "" {
  423. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  424. // }
  425. fmt.Println("getURL = ", getURL)
  426. go httpclient.ZohoGet(getURL)
  427. } else if slices.Contains(ZohoExtenList, event["ConnectedLineNum"]) {
  428. getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=ended&id=%s&from=%s&to=%s&start_time=%s&duration=%d", ZOHO_URL, AttendedTransferList[event["Linkedid"]], event["CallerIDNum"], event["ConnectedLineNum"], startTime, durationTime)
  429. // 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)
  430. // if zohoUser != "" {
  431. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  432. // }
  433. fmt.Println("getURL = ", getURL)
  434. go httpclient.ZohoGet(getURL)
  435. }
  436. delete(StartTimeList, AttendedTransferList[event["Linkedid"]])
  437. delete(CallTypeList, AttendedTransferList[event["Linkedid"]])
  438. delete(AttendedTransferList, event["Linkedid"])
  439. }
  440. // }
  441. } else if event["Event"] == "AttendedTransfer" && slices.Contains(ZohoExtenList, event["TransfereeConnectedLineNum"]) { //热转接
  442. if event["Result"] == "Success" {
  443. durationTime := getDuration(event["OrigTransfererLinkedid"], event["Timestamp"])
  444. startTimeutc := StartTimeList[event["OrigTransfererLinkedid"]].In(time.UTC) // 存储从string 改为 time.Time
  445. startTime := startTimeutc.Format(time.RFC3339)
  446. fmt.Println("startTime: ", startTime)
  447. AttendedTransferList[event["OrigTransfererLinkedid"]] = event["SecondTransfererLinkedid"]
  448. 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["OrigTransfererLinkedid"], event["TransfereeCallerIDNum"], event["TransfereeConnectedLineNum"], startTime, durationTime)
  449. // if zohoUser != "" {
  450. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  451. // }
  452. fmt.Println("getURL = ", getURL)
  453. go httpclient.ZohoGet(getURL)
  454. delete(StartTimeList, event["OrigTransfererLinkedid"])
  455. delete(CallTypeList, event["OrigTransfererLinkedid"])
  456. }
  457. } else if event["Event"] == "SoftHangupRequest" {
  458. Channel := strings.Split(event["Channel"], "/")
  459. if (event["Context"] == "macro-stdexten" || event["Context"] == "macro-stdexten-withoutvm") && Channel[0] != "Local" && len(event["Exten"]) > 3 {
  460. startTimebj := time.Now().Add(-8 * time.Hour)
  461. startTimeutc := startTimebj.In(time.UTC) // 存储从string 改为 time.Time
  462. startTime := startTimeutc.Format(time.RFC3339)
  463. if event["ConnectedLineNum"] == "<unknown>" {
  464. ConnectedLineNum := strings.Split(strings.Split(event["ConnectedLineNum"], "<")[1], ">")[0]
  465. getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=missed&id=%s&from=%s&to=%s&start_time=%s", ZOHO_URL, event["Linkedid"], event["CallerIDNum"], ConnectedLineNum, startTime)
  466. // if zohoUser != "" {
  467. // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  468. // }
  469. fmt.Println("getURL = ", getURL)
  470. go httpclient.ZohoGet(getURL)
  471. }
  472. }
  473. }
  474. // else if event["Event"] == "SoftHangupRequest" {
  475. // Channel := strings.Split(event["Channel"], "/")
  476. // if event["Exten"] == "s-CHANUNAVAIL" && Channel[0] != "Local" {
  477. // startTimebj := time.Now().Add(-8 * time.Hour)
  478. // startTimeutc := startTimebj.In(time.UTC) // 存储从string 改为 time.Time
  479. // startTime := startTimeutc.Format(time.RFC3339)
  480. // ConnectedLineNum := strings.Split(strings.Split(event["ConnectedLineNum"], "<")[1], ">")[0]
  481. // getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=missed&id=%s&from=%s&to=%s&start_time=%s", ZOHO_URL, event["Linkedid"], event["CallerIDNum"], ConnectedLineNum, startTime)
  482. // // if zohoUser != "" {
  483. // // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  484. // // }
  485. // fmt.Println("getURL = ", getURL)
  486. // go httpclient.ZohoGet(getURL)
  487. // }
  488. // }
  489. // else if event["Event"] == "Cdr" {
  490. // if event["Disposition"] == "ANSWERED" && event["CallType"] == "incoming" && strings.HasPrefix(event["DestinationContext"], "department") {
  491. // durationTime := getDuration(LinkedidList["Linkedid"], event["Timestamp"])
  492. // startTimeutc := StartTimeList[LinkedidList["Linkedid"]].In(time.UTC) // 存储从string 改为 time.Time
  493. // startTime := startTimeutc.Format(time.RFC3339)
  494. // 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["Source"], event["Destination"], startTime, durationTime)
  495. // // 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)
  496. // // if zohoUser != "" {
  497. // // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  498. // // }
  499. // fmt.Println("getURL = ", getURL)
  500. // go httpclient.ZohoGet(getURL)
  501. // delete(LinkedidList, "Linkedid")
  502. // }
  503. // }
  504. // else if event["Event"] == "Cdr" {
  505. // if event["Disposition"] == "ANSWERED" {
  506. // durationTime := getDuration(LinkedidList["Linkedid"], event["Timestamp"])
  507. // startTimeutc := StartTimeList[LinkedidList["Linkedid"]].In(time.UTC) // 存储从string 改为 time.Time
  508. // startTime := startTimeutc.Format(time.RFC3339)
  509. // CallSrcNum := strings.Split(strings.Split(event["CallSrcNum"], "<")[1], ">")[0]
  510. // if event["CallType"] == "outgoing" {
  511. // getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=dailed&state=ended&id=%s&from=%s&to=%s&start_time=%s&duration=%d", ZOHO_URL, LinkedidList["Linkedid"], CallSrcNum, event["CallDestNum"], startTime, durationTime)
  512. // // if zohoUser != "" {
  513. // // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  514. // // }
  515. // fmt.Println("getURL = ", getURL)
  516. // go httpclient.ZohoGet(getURL)
  517. // delete(LinkedidList, "Linkedid")
  518. // } else if event["CallType"] == "incoming" {
  519. // getURL := fmt.Sprintf("%s/phonebridge/v3/callnotify?type=received&state=ended&id=%s&from=%s&to=%s&start_time=%s&duration=%d", ZOHO_URL, LinkedidList["Linkedid"], CallSrcNum, event["CallDestNum"], startTime, durationTime)
  520. // // 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)
  521. // // if zohoUser != "" {
  522. // // getURL = fmt.Sprintf("%s&zohouser=%s", getURL, zohoUser)
  523. // // }
  524. // fmt.Println("getURL = ", getURL)
  525. // go httpclient.ZohoGet(getURL)
  526. // delete(LinkedidList, "Linkedid")
  527. // }
  528. // }
  529. // }
  530. // fmt.Println("getURL = ", getURL)
  531. // 测试验证下上面有没有删除,以防TimestampList越来越大
  532. // for k, v := range TimestampList {
  533. // fmt.Printf("===Linkedid:%s Timestamp:%s===\n", k, v)
  534. // }
  535. }
  536. // 使用事件中event["Timestamp"]的值计算时间间隔
  537. // func getDuration(uniqueid string, timeStamp string) int {
  538. func getDuration(linkedid string, timeStamp string) int { // 都改为 Linkedid
  539. fmt.Println("startTimeStamp Str: ", TimestampList[linkedid])
  540. if TimestampList[linkedid] == "" || timeStamp == "" {
  541. fmt.Printf("linkedid 或 timeStamp 为空\n")
  542. return -1
  543. }
  544. startTimeStamp, err := strconv.ParseFloat(TimestampList[linkedid], 64)
  545. if err != nil {
  546. fmt.Printf("startTimeStamp转换出错: %v\n", err)
  547. return -1
  548. }
  549. fmt.Printf("startTimeStamp=%f\n", startTimeStamp)
  550. endTimeStamp, err := strconv.ParseFloat(timeStamp, 64)
  551. if err != nil {
  552. fmt.Printf("endTimeStamp转换出错: %v\n", err)
  553. return -1
  554. }
  555. fmt.Printf("endTimeStamp=%f\n", endTimeStamp)
  556. durationTime := time.Duration(endTimeStamp-startTimeStamp) * time.Second
  557. // fmt.Println("durationTime=", durationTime) // durationTime= 35s
  558. // fmt.Printf("durationTime=%d\n", durationTime) // durationTime=35000000000
  559. // fmt.Printf("durationTime=%d\n", durationTime.Seconds()) // durationTime=%!d(float64=35)
  560. // fmt.Printf("durationTime=%f\n", durationTime.Seconds()) // durationTime=35.000000
  561. fmt.Printf("durationTime=%d\n", int(durationTime.Seconds())) // durationTime=35
  562. // durationTime001 := time.Duration(endTimeStamp - startTimeStamp)
  563. // fmt.Println("durationTime001=", durationTime001) // durationTime001= 35ns
  564. // durationTime003 := time.Duration(endTimeStamp - startTimeStamp).Seconds()
  565. // fmt.Println("durationTime003=", durationTime003) // durationTime003= 3.5e-08
  566. // 删除记录时间
  567. delete(TimestampList, linkedid)
  568. // return durationTime
  569. return int(durationTime.Seconds())
  570. }
  571. // @tags PBX-zoho
  572. // @Summary 刷新token
  573. // @Description 刷新token
  574. // @Security ZohoToken
  575. // @Accept json
  576. // @Produce json
  577. // @Router /api/zoho/refresh-token [post]
  578. func RefreshToken() {
  579. fmt.Printf("RefreshToken ............\n")
  580. // 获取配置文件信息
  581. // confPath := "/etc/asterisk/vtiger_api.conf"
  582. confPath := "/etc/asterisk/crm_api.conf"
  583. cfg, err := ini.Load(confPath)
  584. if err != nil {
  585. lfshook.NewLogger().Error(err)
  586. return
  587. }
  588. ZohoAuthUrl := cfg.Section("general").Key("zohoAuthUrl").String()
  589. ZohoRefreshToken := cfg.Section("general").Key("zohoRefreshToken").String()
  590. ZohoClientId := cfg.Section("general").Key("zohoClientId").String()
  591. ZohoClientSecret := cfg.Section("general").Key("zohoClientSecret").String()
  592. if ZohoAuthUrl == "" || ZohoRefreshToken == "" || ZohoClientId == "" || ZohoClientSecret == "" {
  593. lfshook.NewLogger().Error("/etc/asterisk/crm_api.conf not set zohoAuthUrl or zohoRefreshToken or zohoClientId or zohoClientSecret")
  594. return
  595. }
  596. // 创建请求
  597. // https://accounts.zoho.com/oauth/v2/token?refresh_token=1004.86c8c0e3db7bfe9133598825bef28eb9.17a82a3bf3e675c504f478c1b0b5c456
  598. // &client_id=1004.LWJCJZD5O9DB6SZLL5YJEWHT7LH0BV&client_secret=fc3aef43dc58af8a49d3ed597710924200b03f74d0&grant_type=refresh_token
  599. getURL := fmt.Sprintf("%s/oauth/v2/token?refresh_token=%s&client_id=%s&client_secret=%s&grant_type=refresh_token", ZohoAuthUrl, ZohoRefreshToken, ZohoClientId, ZohoClientSecret)
  600. fmt.Printf("getURL = %s\n", getURL)
  601. // data := httpRequest(ctx, "POST", getURL)
  602. // /* =====================================================================================
  603. req, err := http.NewRequest("POST", getURL, nil)
  604. if err != nil {
  605. fmt.Println("创建请求时发生错误:", err)
  606. return
  607. }
  608. // 创建HTTP客户端
  609. client := &http.Client{}
  610. // req.Header.Set("Authorization", "Bearer "+accessToken) // 刷新token时不需要
  611. // 发送请求
  612. resp, err := client.Do(req)
  613. if err != nil {
  614. fmt.Println("发送请求时发生错误:", err)
  615. return
  616. }
  617. defer resp.Body.Close()
  618. // 读取请求后的响应
  619. data, err := ioutil.ReadAll(resp.Body)
  620. if err != nil {
  621. // 读取数据错误
  622. lfshook.NewLogger().Warn("ioutil ReadAll failed :", err.Error())
  623. // api.Error(ctx, http.StatusInternalServerError, err.Error())
  624. return
  625. }
  626. lfshook.NewLogger().Infof("data %+v", string(data))
  627. // * ===================================================================================== */
  628. refreshTokenData := RefreshTokenResp{}
  629. err = json.Unmarshal([]byte(data), &refreshTokenData)
  630. if err != nil {
  631. // 转换数据错误
  632. lfshook.NewLogger().Warn("json unmarshal failed :", err.Error())
  633. // api.Error(ctx, http.StatusInternalServerError, err.Error())
  634. return
  635. }
  636. // 将获取的 access_token 写入文件 crm_api.conf
  637. cfg.Section("general").Key("zohoAccessToken").SetValue(refreshTokenData.AccessToken)
  638. err = cfg.SaveTo(confPath)
  639. if err != nil {
  640. lfshook.NewLogger().Error(err)
  641. // api.Error(ctx, http.StatusInternalServerError, err.Error())
  642. return
  643. }
  644. // 打印请求后的响应
  645. fmt.Printf("data = %+v\n", string(data))
  646. // api.Success(ctx, string(data))
  647. // api.Success(ctx, refreshTokenData)
  648. // return
  649. }
  650. // 每隔55分钟刷新token
  651. func RefreshTokenTicker() {
  652. fmt.Printf("RefreshTokenTicker ............\n")
  653. ticker := time.NewTicker(55 * time.Minute)
  654. defer ticker.Stop()
  655. done := make(chan bool)
  656. // go func() { // 注释掉OK
  657. for {
  658. select {
  659. case <-done:
  660. return
  661. case t := <-ticker.C:
  662. fmt.Println("Tick at", t)
  663. RefreshToken()
  664. }
  665. }
  666. // }()
  667. }
  668. // 每隔3秒更新zoho用户分机集合
  669. func ZohoUserExtenList() {
  670. // 构建参数
  671. // lfshook.NewLogger().Error("来了")
  672. list := make([]string, 0)
  673. session := mysql.DBOrmInstance.Prepare()
  674. // 查询数据
  675. var data []TabZohouser
  676. _, err := session.FindAndCount(&data)
  677. if err != nil {
  678. lfshook.NewLogger().Error(err)
  679. return
  680. }
  681. for _, v := range data {
  682. list = append(list, v.Exten)
  683. }
  684. ZohoExtenList = list
  685. // lfshook.NewLogger().Error(ZohoExtenList)
  686. }
  687. func RefreshZohoUserExtenListTicker() {
  688. fmt.Printf("RefreshTokenTicker ............\n")
  689. ticker := time.NewTicker(3 * time.Second)
  690. defer ticker.Stop()
  691. done := make(chan bool)
  692. // go func() { // 注释掉OK
  693. for {
  694. select {
  695. case <-done:
  696. return
  697. case <-ticker.C:
  698. // fmt.Println("Tick at", t)
  699. ZohoUserExtenList()
  700. }
  701. }
  702. // }()
  703. }