call.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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@default", "Local", 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. "Variable": fmt.Sprintf("CAB=%s", callType),
  35. "async": "true",
  36. }
  37. lfshook.NewLogger().Infof("dial action %+v", action)
  38. res, _, err := AminInstance.Send(action)
  39. if err != nil {
  40. lfshook.NewLogger().Errorf("%+v", err)
  41. }
  42. lfshook.NewLogger().Info(res)
  43. }
  44. // 获取分机状态
  45. func ExtenStatus(exten string) (Status string) {
  46. action := map[string]string{
  47. "Action": "ExtensionState",
  48. "Exten": exten,
  49. "Context": "default",
  50. }
  51. lfshook.NewLogger().Infof("ExtensionState action %+v", action)
  52. res, _, err := AminInstance.Send(action)
  53. if err != nil {
  54. lfshook.NewLogger().Errorf("%+v", err)
  55. return ""
  56. }
  57. lfshook.NewLogger().Info(res)
  58. return res["StatusText"]
  59. }
  60. // PACU ChanSpy
  61. func ChanSpy(src, dst string, whisper, bargein bool) {
  62. lfshook.NewLogger().Infof("chan spy src:%s dst:%s", src, dst)
  63. //channel := fmt.Sprintf("%s/%s", utils.DialPrefix, dst)
  64. channel := fmt.Sprintf("Local/%s@aio-rule", dst)
  65. data := fmt.Sprintf("%s/%s,qBE", utils.DialPrefix, src)
  66. /*
  67. if whisper {
  68. data = fmt.Sprintf("%s,w", data)
  69. }
  70. if bargein {
  71. data = fmt.Sprintf("%s,B", data)
  72. }
  73. */
  74. action := map[string]string{
  75. "Action": "Originate",
  76. "Channel": channel, // 不存在的通话
  77. "Application": "ChanSpy",
  78. "Data": data, // 存在的通话
  79. "CallerID": dst,
  80. "Async": "true",
  81. }
  82. lfshook.NewLogger().Infof("PACU ChanSpy action %+v", action)
  83. _, _, err := AminInstance.Send(action)
  84. if err != nil {
  85. lfshook.NewLogger().Errorf("%+v", err)
  86. }
  87. }
  88. // Page
  89. func Page(src string, extensions []string, duplex bool) {
  90. channel := fmt.Sprintf("%s/%s", utils.DialPrefix, src)
  91. data := make([]string, 0)
  92. for _, exten := range extensions {
  93. data = append(data, fmt.Sprintf("%s/%s", utils.DialPrefix, exten))
  94. }
  95. appData := strings.Join(data, "&")
  96. if duplex {
  97. appData = fmt.Sprintf("%s,db(header-handler^addheader^1)", appData)
  98. } else {
  99. appData = fmt.Sprintf("%s,b(header-handler^addheader^1)", appData)
  100. }
  101. //timeout
  102. appData = fmt.Sprintf("%s,30", appData)
  103. action := map[string]string{
  104. "Action": "Originate",
  105. "Channel": channel,
  106. "Application": "page",
  107. "Data": appData,
  108. "CallerID": src,
  109. "CallerSrc": src,
  110. "Variable": "PJSIP_HEADER(add,answer-after)=0",
  111. "async": "true",
  112. }
  113. lfshook.NewLogger().Infof("page action %+v", action)
  114. res, _, err := AminInstance.Send(action)
  115. if err != nil {
  116. lfshook.NewLogger().Errorf("%+v", err)
  117. }
  118. lfshook.NewLogger().Infof("%+v", res)
  119. }
  120. // Play 转 AGI 播放
  121. func Play(name, UUID string, extensions []string, hangupAll bool, autoanswer string) {
  122. if hangupAll {
  123. lfshook.NewLogger().Infof("hangup all before play to %+v", extensions)
  124. for _, extension := range extensions {
  125. Hangup(extension)
  126. }
  127. time.Sleep(3 * time.Second)
  128. }
  129. channel := "Local/broadcast@broadcast"
  130. data := make([]string, 0)
  131. for _, exten := range extensions {
  132. data = append(data, fmt.Sprintf("%s/%s", utils.DialPrefix, exten))
  133. }
  134. appData := strings.Join(data, "&")
  135. //timeout
  136. /*if autoanswer == "yes" {
  137. appData = fmt.Sprintf("%s,%s,%d", appData, "b(header-handler^addheader^1)", configs.ConfigGlobal.AsteriskBroadcastTimeout)
  138. } else {
  139. appData = fmt.Sprintf("%s,,%d", appData, configs.ConfigGlobal.AsteriskBroadcastTimeout)
  140. }*/
  141. variable := fmt.Sprintf("task_UUID=%s", UUID)
  142. action := map[string]string{
  143. "Action": "Originate",
  144. "Channel": channel,
  145. "Application": "page",
  146. "Data": appData,
  147. "Variable": variable,
  148. //"CallerID": fmt.Sprintf("%s<%s>", configs.ConfigGlobal.AsteriskBroadcastName, configs.ConfigGlobal.AsteriskBroadcastID),
  149. "CallerID": fmt.Sprintf("%s<%s>", name, name),
  150. "CallSrc": name,
  151. "async": "true",
  152. }
  153. lfshook.NewLogger().Infof("play action %+v", action)
  154. _, _, err := AminInstance.Send(action)
  155. if err != nil {
  156. lfshook.NewLogger().Errorf("%+v", err)
  157. }
  158. }
  159. // Redirect 转接
  160. func Redirect(channel, dst, dialrule string) (err error) {
  161. callerID := "redirect"
  162. lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
  163. if !utils.IsChannel(channel) {
  164. callerID = channel
  165. if channel, err = GetChannelByExten(channel); err != nil {
  166. return err
  167. }
  168. }
  169. action := map[string]string{
  170. "Action": "Redirect",
  171. "Channel": channel,
  172. "Exten": dst,
  173. "Context": dialrule,
  174. "CallerID": callerID,
  175. "Priority": "1",
  176. "async": "true",
  177. }
  178. lfshook.NewLogger().Infof("redirect %+v", action)
  179. res, _, err := AminInstance.Send(action)
  180. if err != nil {
  181. lfshook.NewLogger().Error(err)
  182. }
  183. lfshook.NewLogger().Info(res)
  184. return err
  185. }
  186. // Redirect 转接
  187. func RedirectInQueue(channel, dst, dialrule, callerID string) (err error) {
  188. //callerID := "redirect"
  189. lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
  190. if !utils.IsChannel(channel) {
  191. //callerID = channel
  192. if channel, err = GetChannelByExtenNotBridged(channel); err != nil {
  193. return err
  194. }
  195. }
  196. action := map[string]string{
  197. "Action": "Redirect",
  198. "Channel": channel,
  199. "Exten": dst,
  200. "Context": dialrule,
  201. "CallerID": callerID,
  202. "Priority": "1",
  203. "async": "true",
  204. }
  205. lfshook.NewLogger().Infof("redirect %+v", action)
  206. res, _, err := AminInstance.Send(action)
  207. if err != nil {
  208. lfshook.NewLogger().Error(err)
  209. }
  210. lfshook.NewLogger().Info(res)
  211. return err
  212. }
  213. // Redirect 转接
  214. func BlindTransfer(channel, dst, dialrule string) (err error) {
  215. lfshook.NewLogger().Infof("BlindTransfer src %s to dst %s", channel, dst)
  216. if !utils.IsChannel(channel) {
  217. if channel, err = GetExtenChan(channel); err != nil {
  218. return err
  219. }
  220. }
  221. action := map[string]string{
  222. "Action": "BlindTransfer",
  223. "Channel": channel,
  224. "Exten": dst,
  225. "Context": dialrule,
  226. }
  227. lfshook.NewLogger().Infof("BlindTransfer %+v", action)
  228. res, _, err := AminInstance.Send(action)
  229. if err != nil {
  230. lfshook.NewLogger().Error(err)
  231. }
  232. lfshook.NewLogger().Info(res)
  233. return err
  234. }
  235. // Atxfer
  236. func Atxfer(channel, dst, dialrule string) (err error) {
  237. // 获取通道
  238. if !utils.IsChannel(channel) {
  239. if channel, err = GetChannelByExten(channel); err != nil {
  240. return err
  241. }
  242. }
  243. action := map[string]string{
  244. "Action": "Atxfer",
  245. "Channel": channel,
  246. "Exten": dst,
  247. "Context": dialrule,
  248. }
  249. lfshook.NewLogger().Infof("atxfer action %+v", action)
  250. res, _, err := AminInstance.Send(action)
  251. if err != nil {
  252. lfshook.NewLogger().Errorf("%+v", err)
  253. return err
  254. }
  255. lfshook.NewLogger().Debugf("atxfer res %+v", res)
  256. return err
  257. }