call.go 7.4 KB

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