call.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package asterisk
  2. import (
  3. "fmt"
  4. "net/http"
  5. "pbx-api-gin/api/model"
  6. "pbx-api-gin/internal/app/ami/action"
  7. "strings"
  8. "github.com/gin-gonic/gin"
  9. )
  10. // @tags Asterisk-Call
  11. // @Summary 挂断
  12. // @Description 挂断分机或通道数组
  13. // @Security ApiKeyAuth
  14. // @Accept json
  15. // @Produce json
  16. // @Param data body []string true "需要挂断的分机或通道数组"
  17. // @Success 200 {object} model.APIOK "请求成功"
  18. // @Router /ginapi/plugin-asterisk/call/hangup [post]
  19. func Hangup(extens string) {
  20. //var extensions []string
  21. extensions := strings.Split(extens, ";")
  22. for _, extension := range extensions {
  23. action.Hangup(extension)
  24. }
  25. }
  26. // @tags Asterisk-Call
  27. // @Summary 拨打电话
  28. // @Description 拨打指定的分机
  29. // @Security ApiKeyAuth
  30. // @Accept json
  31. // @Produce json
  32. // @Param data body model.DialVO true "需要拨打的分机信息"
  33. // @Success 200 {object} model.APIOK "请求成功"
  34. // @Router /ginapi/plugin-asterisk/call/dial [post]
  35. func Dial(ctx *gin.Context) {
  36. var info model.DialVO
  37. if err := ctx.ShouldBindJSON(&info); err != nil {
  38. ctx.JSON(http.StatusBadRequest, model.APIError{ErrorMessage: err.Error()})
  39. return
  40. }
  41. // 根据参数确定主叫, 再考虑用户登录的分机信息
  42. myExtension := info.CallerNumber
  43. if myExtension == "" {
  44. ID, _ := ctx.Get("ID")
  45. myExtension = "===================="
  46. if myExtension == "" {
  47. ctx.JSON(http.StatusBadRequest, model.APIError{ErrorMessage: fmt.Sprintf("not found user extension by ID %d", ID)})
  48. return
  49. }
  50. }
  51. dialplan := "default"
  52. if info.CallerID == "" {
  53. info.CallerID = info.Extension
  54. }
  55. // 喊话 主动挂断被叫
  56. action.Hangup(info.Extension)
  57. action.Dial(myExtension, info.Extension, dialplan, info.CallerID, "================", "Local")
  58. ctx.JSON(http.StatusOK, model.APIOK{Message: "ok", Data: info})
  59. }
  60. // @tags Asterisk-Call
  61. // @Summary 强拆()
  62. // @Description 将当前正在进行的通话保持,src 与 正在通话的 dst建立通话。
  63. // @Security ApiKeyAuth
  64. // @Accept json
  65. // @Produce json
  66. // @Param data body model.SrcDstInfo true "src, dst"
  67. // @Success 200 {object} model.APIOK "请求成功"
  68. // @Router /ginapi/plugin-asterisk/call/clear [post]
  69. func Clear(ctx *gin.Context) {
  70. var input model.SrcDstInfo
  71. if err := ctx.ShouldBindJSON(&input); err != nil {
  72. ctx.JSON(http.StatusBadRequest, model.APIError{ErrorMessage: err.Error()})
  73. return
  74. }
  75. action.Redirect(input.Src, input.Dst, "default")
  76. ctx.JSON(http.StatusOK, model.APIOK{Message: "ok", Data: input})
  77. }
  78. // @tags Asterisk-Call
  79. // @Summary 密语(悄悄话)
  80. // @Description 将 src 插入到 dst 的通话中,与 dst 密语
  81. // @Security ApiKeyAuth
  82. // @Accept json
  83. // @Produce json
  84. // @Param data body model.ExtensionDstInfo true "src, dst"
  85. // @Success 200 {object} model.APIOK "请求成功"
  86. // @Router /ginapi/plugin-asterisk/call/whisper [post]
  87. func extenWhisper(ctx *gin.Context) {
  88. var input model.ExtensionDstInfo
  89. if err := ctx.ShouldBindJSON(&input); err != nil {
  90. ctx.JSON(http.StatusBadRequest, model.APIError{ErrorMessage: err.Error()})
  91. return
  92. }
  93. action.ChanSpy(input.Extension, input.Dst, true, false)
  94. ctx.JSON(http.StatusOK, model.APIOK{Message: "ok", Data: input})
  95. }
  96. // @tags Asterisk-Call
  97. // @Summary 强插(三方通话)
  98. // @Description 将 src 插入 dst 的通话中
  99. // @Security ApiKeyAuth
  100. // @Accept json
  101. // @Produce json
  102. // @Param data body model.ExtensionDstInfo true "src, dst"
  103. // @Success 200 {object} model.APIOK "请求成功"
  104. // @Router /ginapi/plugin-asterisk/call/bargein [post]
  105. func extenBargeIn(ctx *gin.Context) {
  106. var input model.ExtensionDstInfo
  107. if err := ctx.ShouldBindJSON(&input); err != nil {
  108. ctx.JSON(http.StatusBadRequest, model.APIError{ErrorMessage: err.Error()})
  109. return
  110. }
  111. action.ChanSpy(input.Extension, input.Dst, false, true)
  112. ctx.JSON(http.StatusOK, model.APIOK{Message: "ok", Data: input})
  113. }
  114. // @tags Asterisk-Call
  115. // @Summary 监听
  116. // @Description 将当前正在进行的通话终止,并与分机建立通话。
  117. // @Security ApiKeyAuth
  118. // @Accept json
  119. // @Produce json
  120. // @Param data body model.ExtensionDstInfo true "src 为已进行的通话分机, dst 为未通话的分机"
  121. // @Success 200 {object} model.APIOK "请求成功"
  122. // @Router /ginapi/plugin-asterisk/call/spy [post]
  123. func extenSpy(ctx *gin.Context) {
  124. var input model.ExtensionDstInfo
  125. if err := ctx.ShouldBindJSON(&input); err != nil {
  126. ctx.JSON(http.StatusBadRequest, model.APIError{ErrorMessage: err.Error()})
  127. return
  128. }
  129. action.ChanSpy(input.Extension, input.Dst, false, false)
  130. ctx.JSON(http.StatusOK, model.APIOK{Message: "ok", Data: input})
  131. }
  132. // @tags Asterisk-Call
  133. // @Summary 转接
  134. // @Description 转接 src 到 dst
  135. // @Security ApiKeyAuth
  136. // @Accept json
  137. // @Produce json
  138. // @Param data body model.SrcDstInfo true "src, dst"
  139. // @Success 200 {object} model.APIOK "请求成功"
  140. // @Router /ginapi/plugin-asterisk/call/transfer [post]
  141. func transfer(ctx *gin.Context) {
  142. var input model.SrcDstInfo
  143. if err := ctx.ShouldBindJSON(&input); err != nil {
  144. ctx.JSON(http.StatusBadRequest, model.APIError{ErrorMessage: err.Error()})
  145. return
  146. }
  147. myExtension := ""
  148. if myExtension == "" {
  149. ID, _ := ctx.Get("ID")
  150. myExtension = "============================="
  151. if myExtension == "" {
  152. ctx.JSON(http.StatusBadRequest, model.APIError{ErrorMessage: fmt.Sprintf("not found user extension by ID %d", ID)})
  153. return
  154. }
  155. }
  156. //action.BlindTransfer(input.Src, input.Dst, mysql.GetDialPlanByExtension(myExtension))
  157. action.Redirect(input.Src, input.Dst, "default")
  158. ctx.JSON(http.StatusOK, model.APIOK{Message: "ok", Data: input})
  159. }
  160. // @tags Asterisk-Call
  161. // @Summary 转移
  162. // @Description 通话已进行, 将 Src 的通话转移到 Dst, 挂断后 Dst 与 Src 继续通话
  163. // @Security ApiKeyAuth
  164. // @Accept json
  165. // @Produce json
  166. // @Param data body model.ChannelDstInfo true "src, dst"
  167. // @Success 200 {object} model.APIOK "请求成功"
  168. // @Router /ginapi/plugin-asterisk/call/atxfer [post]
  169. func atxfer(ctx *gin.Context) {
  170. var input model.ChannelDstInfo
  171. if err := ctx.ShouldBindJSON(&input); err != nil {
  172. ctx.JSON(http.StatusBadRequest, model.APIError{ErrorMessage: err.Error()})
  173. return
  174. }
  175. //ID, _ := ctx.Get("ID")
  176. dialplan := "default"
  177. // admin 用户通过主叫获取
  178. action.Atxfer(input.Channel, input.Dst, dialplan)
  179. ctx.JSON(http.StatusOK, model.APIOK{Message: "ok", Data: input})
  180. }
  181. // @tags Asterisk-Call
  182. // @Summary 广播
  183. // @Description 广播
  184. // @Security ApiKeyAuth
  185. // @Accept json
  186. // @Produce json
  187. // @Param data body model.PageVO true "广播参数"
  188. // @Success 200 {object} model.APIOK "请求成功"
  189. // @Router /ginapi/plugin-asterisk/call/page [post]
  190. func page(ctx *gin.Context) {
  191. var input model.PageVO
  192. if err := ctx.ShouldBindJSON(&input); err != nil {
  193. ctx.JSON(http.StatusBadRequest, model.APIError{ErrorMessage: err.Error()})
  194. return
  195. }
  196. // 主动挂断被叫
  197. for _, exten := range input.Extensions {
  198. action.Hangup(exten)
  199. }
  200. action.Page(input.Src, input.Extensions, input.Duplex)
  201. ctx.JSON(http.StatusOK, model.APIOK{Message: "ok", Data: input})
  202. }