call.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package action
  2. import (
  3. "fmt"
  4. "pbx-api-gin/pkg/lfshook"
  5. "pbx-api-gin/pkg/utils"
  6. "strings"
  7. "time"
  8. )
  9. // Hangup 挂断指定分机或通道
  10. func Hangup(channel string) {
  11. lfshook.NewLogger().Infof("hangup extensions/channel %s", channel)
  12. if !utils.IsChannel(channel) {
  13. channel = fmt.Sprintf(`/^(PJ)?SIP\/%s-.*$/`, channel)
  14. }
  15. action := map[string]string{
  16. "Action": "hangup",
  17. "Channel": channel,
  18. }
  19. lfshook.NewLogger().Infof("hangup action %+v", action)
  20. if _, _, err := AminInstance.Send(action); err != nil {
  21. lfshook.NewLogger().Errorf("Hangup %+v", err)
  22. }
  23. }
  24. // Dial 拨打号码
  25. func Dial(src, dst, dialrule, callerID, callerName string, callType string) {
  26. chanel := fmt.Sprintf("%s/%s", callType, src)
  27. action := map[string]string{
  28. "Action": "Originate",
  29. "Channel": chanel,
  30. "Exten": dst,
  31. "Context": dialrule,
  32. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  33. "Priority": "1",
  34. "async": "true",
  35. }
  36. lfshook.NewLogger().Infof("dial action %+v", action)
  37. res, _, err := AminInstance.Send(action)
  38. if err != nil {
  39. lfshook.NewLogger().Errorf("%+v", err)
  40. }
  41. lfshook.NewLogger().Info(res)
  42. }
  43. // ChanSpy
  44. func ChanSpy(src, dst string, whisper, bargein bool) {
  45. lfshook.NewLogger().Infof("chan spy src:%s dst:%s", src, dst)
  46. channel := fmt.Sprintf("%s/%s", utils.DialPrefix, dst)
  47. data := fmt.Sprintf("%s/%s,q,E", utils.DialPrefix, src)
  48. if whisper {
  49. data = fmt.Sprintf("%s,w", data)
  50. }
  51. if bargein {
  52. data = fmt.Sprintf("%s,B", data)
  53. }
  54. action := map[string]string{
  55. "Action": "Originate",
  56. "Channel": channel, // 不存在的通话
  57. "Application": "ChanSpy",
  58. "Data": data, // 存在的通话
  59. "CallerID": dst,
  60. "Async": "true",
  61. }
  62. lfshook.NewLogger().Infof("ChanSpy action %+v", action)
  63. _, _, err := AminInstance.Send(action)
  64. if err != nil {
  65. lfshook.NewLogger().Errorf("%+v", err)
  66. }
  67. }
  68. // Page
  69. func Page(src string, extensions []string, duplex bool) {
  70. channel := fmt.Sprintf("%s/%s", utils.DialPrefix, src)
  71. data := make([]string, 0)
  72. for _, exten := range extensions {
  73. data = append(data, fmt.Sprintf("%s/%s", utils.DialPrefix, exten))
  74. }
  75. appData := strings.Join(data, "&")
  76. if duplex {
  77. appData = fmt.Sprintf("%s,db(header-handler^addheader^1)", appData)
  78. } else {
  79. appData = fmt.Sprintf("%s,b(header-handler^addheader^1)", appData)
  80. }
  81. //timeout
  82. appData = fmt.Sprintf("%s,30", appData)
  83. action := map[string]string{
  84. "Action": "Originate",
  85. "Channel": channel,
  86. "Application": "page",
  87. "Data": appData,
  88. "CallerID": src,
  89. "CallerSrc": src,
  90. "Variable": "PJSIP_HEADER(add,answer-after)=0",
  91. "async": "true",
  92. }
  93. lfshook.NewLogger().Infof("page action %+v", action)
  94. res, _, err := AminInstance.Send(action)
  95. if err != nil {
  96. lfshook.NewLogger().Errorf("%+v", err)
  97. }
  98. lfshook.NewLogger().Infof("%+v", res)
  99. }
  100. // Play 转 AGI 播放
  101. func Play(name, UUID string, extensions []string, hangupAll bool, autoanswer string) {
  102. if hangupAll {
  103. lfshook.NewLogger().Infof("hangup all before play to %+v", extensions)
  104. for _, extension := range extensions {
  105. Hangup(extension)
  106. }
  107. time.Sleep(3 * time.Second)
  108. }
  109. channel := "Local/broadcast@broadcast"
  110. data := make([]string, 0)
  111. for _, exten := range extensions {
  112. data = append(data, fmt.Sprintf("%s/%s", utils.DialPrefix, exten))
  113. }
  114. appData := strings.Join(data, "&")
  115. //timeout
  116. /*if autoanswer == "yes" {
  117. appData = fmt.Sprintf("%s,%s,%d", appData, "b(header-handler^addheader^1)", configs.ConfigGlobal.AsteriskBroadcastTimeout)
  118. } else {
  119. appData = fmt.Sprintf("%s,,%d", appData, configs.ConfigGlobal.AsteriskBroadcastTimeout)
  120. }*/
  121. variable := fmt.Sprintf("task_UUID=%s", UUID)
  122. action := map[string]string{
  123. "Action": "Originate",
  124. "Channel": channel,
  125. "Application": "page",
  126. "Data": appData,
  127. "Variable": variable,
  128. //"CallerID": fmt.Sprintf("%s<%s>", configs.ConfigGlobal.AsteriskBroadcastName, configs.ConfigGlobal.AsteriskBroadcastID),
  129. "CallerID": fmt.Sprintf("%s<%s>", name, name),
  130. "CallSrc": name,
  131. "async": "true",
  132. }
  133. lfshook.NewLogger().Infof("play action %+v", action)
  134. _, _, err := AminInstance.Send(action)
  135. if err != nil {
  136. lfshook.NewLogger().Errorf("%+v", err)
  137. }
  138. }
  139. // Redirect 转接
  140. func Redirect(channel, dst, dialrule string) (err error) {
  141. callerID := "redirect"
  142. lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
  143. if !utils.IsChannel(channel) {
  144. callerID = channel
  145. if channel, err = GetChannelByExten(channel); err != nil {
  146. return err
  147. }
  148. }
  149. action := map[string]string{
  150. "Action": "Redirect",
  151. "Channel": channel,
  152. "Exten": dst,
  153. "Context": dialrule,
  154. "CallerID": callerID,
  155. "Priority": "1",
  156. "async": "true",
  157. }
  158. lfshook.NewLogger().Infof("redirect %+v", action)
  159. res, _, err := AminInstance.Send(action)
  160. if err != nil {
  161. lfshook.NewLogger().Error(err)
  162. }
  163. lfshook.NewLogger().Info(res)
  164. return err
  165. }
  166. // Redirect 转接
  167. func BlindTransfer(channel, dst, dialrule string) (err error) {
  168. lfshook.NewLogger().Infof("BlindTransfer src %s to dst %s", channel, dst)
  169. if !utils.IsChannel(channel) {
  170. if channel, err = GetExtenChan(channel); err != nil {
  171. return err
  172. }
  173. }
  174. action := map[string]string{
  175. "Action": "BlindTransfer",
  176. "Channel": channel,
  177. "Exten": dst,
  178. "Context": dialrule,
  179. }
  180. lfshook.NewLogger().Infof("BlindTransfer %+v", action)
  181. res, _, err := AminInstance.Send(action)
  182. if err != nil {
  183. lfshook.NewLogger().Error(err)
  184. }
  185. lfshook.NewLogger().Info(res)
  186. return err
  187. }
  188. // Atxfer
  189. func Atxfer(channel, dst, dialrule string) (err error) {
  190. // 获取通道
  191. if !utils.IsChannel(channel) {
  192. if channel, err = GetChannelByExten(channel); err != nil {
  193. return err
  194. }
  195. }
  196. action := map[string]string{
  197. "Action": "Atxfer",
  198. "Channel": channel,
  199. "Exten": dst,
  200. "Context": dialrule,
  201. }
  202. lfshook.NewLogger().Infof("atxfer action %+v", action)
  203. res, _, err := AminInstance.Send(action)
  204. if err != nil {
  205. lfshook.NewLogger().Errorf("%+v", err)
  206. return err
  207. }
  208. lfshook.NewLogger().Debugf("atxfer res %+v", res)
  209. return err
  210. }