call.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package action
  2. import (
  3. "errors"
  4. "fmt"
  5. "pbx-api-gin/internal/app/stc/priority"
  6. "pbx-api-gin/pkg/lfshook"
  7. "pbx-api-gin/pkg/utils"
  8. "time"
  9. )
  10. // Hangup 挂断指定分机或通道
  11. func Hangup(channel string) {
  12. lfshook.NewLogger().Infof("Hangup extensions/channel :%s", channel)
  13. if !utils.IsChannel(channel) {
  14. channel = fmt.Sprintf(`/^(PJ)?SIP\/%s-.*$/`, channel)
  15. }
  16. action := map[string]string{
  17. "Action": "hangup",
  18. "Channel": channel,
  19. }
  20. //lfshook.NewLogger().Infof("hangup action :%+v", action)
  21. if _, _, err := AminInstance.Send(action); err != nil {
  22. lfshook.NewLogger().Errorf("Hangup %+v", err)
  23. }
  24. }
  25. // Hangup task
  26. func HangupTask() {
  27. _, taskInfo, ok := priority.RegistryTask.HighestPriorityRunningTask()
  28. if ok {
  29. lfshook.NewLogger().Infof("HangupTask Check TaskName:%s ", taskInfo.RunType)
  30. ConfbridgeKick(taskInfo.ConfbridgeID, "all")
  31. Hangup(taskInfo.RunChannel)
  32. time.Sleep(100 * time.Millisecond)
  33. }
  34. }
  35. // Hangup all calls
  36. func HangupAll() {
  37. utils.ExecCmdAsync("/usr/sbin/asterisk", "-rx", "hangup request all")
  38. }
  39. // 获取分机状态
  40. func ExtenStatus(exten string) (Status string) {
  41. action := map[string]string{
  42. "Action": "ExtensionState",
  43. "Exten": exten,
  44. "Context": "default",
  45. }
  46. res, _, err := AminInstance.Send(action)
  47. if err != nil {
  48. lfshook.NewLogger().Errorf("%+v", err)
  49. return ""
  50. }
  51. //lfshook.NewLogger().Infof("================ExtensionState:res %+v", res)
  52. return res["StatusText"]
  53. }
  54. func ConfbridgeKick(confnum, channel string) (res map[string]string, err error) {
  55. action := map[string]string{
  56. "Action": "ConfbridgeKick",
  57. "Conference": confnum,
  58. "Channel": channel,
  59. }
  60. res, _, err = AminInstance.Send(action)
  61. if err != nil {
  62. return nil, err
  63. }
  64. lfshook.NewLogger().Infof("ConfbridgeKick res:%+v", res)
  65. if res["Response"] != "Success" {
  66. return nil, errors.New(res["Message"])
  67. }
  68. return res, nil
  69. }
  70. func ConfbridgeList(confnum string) (chans []string, err error) {
  71. action := map[string]string{
  72. "Action": "ConfbridgeList",
  73. "Conference": confnum,
  74. }
  75. res, events, err := AminInstance.Send(action)
  76. if err != nil {
  77. return nil, err
  78. }
  79. lfshook.NewLogger().Infof("ConfbridgeList res:%+v", res)
  80. if res["Response"] == "Success" {
  81. for _, event := range events {
  82. if event.Data["Event"] == "ConfbridgeList" {
  83. chans = append(chans, event.Data["Channel"])
  84. }
  85. }
  86. return chans, nil
  87. } else {
  88. return nil, errors.New(res["Message"])
  89. }
  90. }
  91. func ConfbridgeReinvite(src, context, confID string) {
  92. if ExtenStatus(src) != "Idle" {
  93. lfshook.NewLogger().Infof(" ConfbridgeReinvite ext:%s Not Idle !", src)
  94. return
  95. }
  96. chanel := fmt.Sprintf("Local/%s@%s", src, context)
  97. action := map[string]string{
  98. "Action": "Originate",
  99. "Channel": chanel,
  100. "Exten": "000",
  101. "Context": "confbridge-join",
  102. "CallerID": fmt.Sprintf("%s<%s>", "", ""),
  103. "Priority": "1",
  104. "Variable": fmt.Sprintf("CBID=%s", confID),
  105. "async": "true",
  106. }
  107. lfshook.NewLogger().Infof("dial action %+v", action)
  108. res, _, err := AminInstance.Send(action)
  109. if err != nil {
  110. lfshook.NewLogger().Errorf("%+v", err)
  111. }
  112. lfshook.NewLogger().Info(res)
  113. }
  114. // Dial 拨打号码
  115. func Dial(src, dst, dialrule, callerID, callerName string) {
  116. chanel := fmt.Sprintf("%s/%s@default", "Local", src)
  117. action := map[string]string{
  118. "Action": "Originate",
  119. "Channel": chanel,
  120. "Exten": dst,
  121. "Context": dialrule,
  122. "CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
  123. "Priority": "1",
  124. "async": "true",
  125. }
  126. lfshook.NewLogger().Infof("dial action %+v", action)
  127. res, _, err := AminInstance.Send(action)
  128. if err != nil {
  129. lfshook.NewLogger().Errorf("%+v", err)
  130. }
  131. lfshook.NewLogger().Info(res)
  132. }