info.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package vtiger
  2. import (
  3. "crm-api/api"
  4. "crm-api/pkg/lfshook"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "github.com/gin-gonic/gin"
  9. "gopkg.in/ini.v1"
  10. )
  11. // const VTIGER_URL = "https://zycoo1.od2.vtiger.com"
  12. // // Params 队列参数
  13. // type CallInitiatedParams struct {
  14. // From string `form:"from"`
  15. // To string `form:"to"`
  16. // Event string `form:"event"`
  17. // CallId string `form:"call_id"`
  18. // Direction string `form:"direction"`
  19. // }
  20. // @tags PBX-vtiger
  21. // @Summary 查询contact
  22. // @Description 查询contact
  23. // @Security ApiKeyAuth
  24. // @Accept json
  25. // @Produce json
  26. // @Router /api/vtiger/lookup [get]
  27. func contactsInfo(ctx *gin.Context) {
  28. fmt.Printf("contactsInfo ............\n")
  29. // 获取 vtigerUrl
  30. // confPath := "/etc/asterisk/vtiger_api.conf"
  31. confPath := "/etc/asterisk/pms_api.conf"
  32. cfg, err := ini.Load(confPath)
  33. if err != nil {
  34. lfshook.NewLogger().Error(err)
  35. return
  36. }
  37. VtigerUrl := cfg.Section("general").Key("vtigerUrl").String()
  38. BasicAuthUser := cfg.Section("general").Key("vtigerBasicAuthUser").String()
  39. BasicAuthPWD := cfg.Section("general").Key("vtigerBasicAuthPWD").String()
  40. if VtigerUrl == "" || BasicAuthUser == "" || BasicAuthPWD == "" {
  41. lfshook.NewLogger().Error("/etc/asterisk/pms_api.conf not set vtigerUrl or vtigerBasicAuthUser or vtigerBasicAuthPWD")
  42. return
  43. }
  44. // 获取 phone_number
  45. var reqVO CheckExistReqVO
  46. if err := ctx.ShouldBind(&reqVO); err != nil {
  47. api.Error(ctx, http.StatusBadRequest, err.Error())
  48. return
  49. }
  50. // 创建HTTP客户端
  51. client := &http.Client{}
  52. // 创建请求
  53. fmt.Printf("phone_number = %s\n", reqVO.PhoneNumber)
  54. // getURL := fmt.Sprintf("%s/modules/PhoneCalls/callbacks/Search.php?phone_number=%s", VTIGER_URL, reqVO.PhoneNumber)
  55. // getURL := fmt.Sprintf("%s/restapi/v1/vtiger/default/lookup?type=phone&value=%s", VTIGER_URL, reqVO.PhoneNumber)
  56. getURL := fmt.Sprintf("%s/restapi/v1/vtiger/default/lookup?type=phone&value=%s", VtigerUrl, reqVO.PhoneNumber)
  57. fmt.Printf("getURL = %s\n", getURL)
  58. req, err := http.NewRequest("GET", getURL, nil)
  59. if err != nil {
  60. fmt.Println("创建请求时发生错误:", err)
  61. return
  62. }
  63. // req.SetBasicAuth("juncheng.du@zycoo.com", "8DJ3O28MCZ4sPAk5")
  64. req.SetBasicAuth(BasicAuthUser, BasicAuthPWD)
  65. // 发送请求
  66. resp, err := client.Do(req)
  67. if err != nil {
  68. fmt.Println("发送请求时发生错误:", err)
  69. return
  70. }
  71. defer resp.Body.Close()
  72. // 读取请求后的响应
  73. data, err := ioutil.ReadAll(resp.Body)
  74. if err != nil {
  75. fmt.Println("读取请求后的响应时发生错误:", err)
  76. return
  77. }
  78. // 打印请求后的响应
  79. fmt.Printf("data = %+v\n", string(data))
  80. api.Success(ctx, string(data))
  81. }
  82. /* ===移到ami中了====================================================================================================
  83. // @tags PBX-vtiger
  84. // @Summary 呼叫发起事件
  85. // @Description 呼叫发起事件
  86. // @Security ApiKeyAuth
  87. // @Accept json
  88. // @Produce json
  89. // @Router /api/vtiger/call-initiated [get]
  90. func callInitiated(ctx *gin.Context) {
  91. fmt.Printf("callInitiated ............\n")
  92. var callInitiated CallInitiatedParams
  93. if err := ctx.ShouldBind(&callInitiated); err != nil {
  94. api.Error(ctx, http.StatusBadRequest, err.Error())
  95. return
  96. }
  97. fmt.Printf("From = %s\n", callInitiated.From)
  98. fmt.Printf("To = %s\n", callInitiated.To)
  99. fmt.Printf("Event = %s\n", callInitiated.Event)
  100. fmt.Printf("CallId = %s\n", callInitiated.CallId)
  101. fmt.Printf("Direction = %s\n", callInitiated.Direction)
  102. apiKeyValue := "174781790673da476e25cf"
  103. // 创建HTTP客户端
  104. client := &http.Client{}
  105. // 创建请求
  106. // https://zycoo1.od2.vtiger.com/modules/PhoneCalls/callbacks/Generic.php?from=10000002&to=12300001&event=call_initiated&call_id=12345678&direction=inbound
  107. getURL := fmt.Sprintf("%s/modules/PhoneCalls/callbacks/Generic.php?from=%s&to=%s&event=%s&call_id=%s&direction=%s", VTIGER_URL, callInitiated.From, callInitiated.To, callInitiated.Event, callInitiated.CallId, callInitiated.Direction)
  108. fmt.Printf("getURL = %s\n", getURL)
  109. req, err := http.NewRequest("GET", getURL, nil)
  110. if err != nil {
  111. fmt.Println("创建请求时发生错误:", err)
  112. return
  113. }
  114. req.Header.Set("X-VTIGER-SECRET", apiKeyValue)
  115. // 发送请求
  116. resp, err := client.Do(req)
  117. if err != nil {
  118. fmt.Println("发送请求时发生错误:", err)
  119. return
  120. }
  121. defer resp.Body.Close()
  122. // 读取请求后的响应
  123. data, err := ioutil.ReadAll(resp.Body)
  124. if err != nil {
  125. fmt.Println("读取请求后的响应后的响应时发生错误:", err)
  126. return
  127. }
  128. // 打印请求后的响应
  129. fmt.Printf("data = %+v\n", string(data))
  130. api.Success(ctx, string(data))
  131. }
  132. // ======================================================================================================= */