call.go 7.4 KB

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