gateway.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env python3
  2. # encoding:utf-8
  3. import json
  4. import ESL
  5. import threading
  6. import socketserver
  7. from http.server import BaseHTTPRequestHandler
  8. from http.server import HTTPServer
  9. class InboundClient:
  10. def __init__(self):
  11. self.is_stopping = False
  12. self.con = ESL.ESLconnection('pbx.fuxicarbon.com', '8021', '4918257983818884358')
  13. def start(self):
  14. if self.con.connected():
  15. print('inbound esl connected ... ')
  16. self.con.events('plain', 'all')
  17. while not self.is_stopping:
  18. e = self.con.recvEvent()
  19. if e:
  20. print(json.loads(e.serialize('json')))
  21. # print(e.serialize('json'))
  22. event_name = e.getHeader("Event-Name")
  23. if event_name == "CHANNEL_PARK":
  24. # 获取通话的 UUID
  25. call_uuid = e.getHeader("Unique-ID")
  26. print(f"Incoming call with UUID: {call_uuid}")
  27. # self.con.execute("transfer", "user/1001", call_uuid)
  28. self.con.execute("transfer", "1001 XML pbx.fuxicarbon.com", call_uuid)
  29. # self.con.execute("bridge", "user/1001@pbx.fuxicarbon.com", call_uuid)
  30. # self.con.execute("bridge", "user/1001", call_uuid)
  31. # self.con.execute("transfer", "1001 XML default", call_uuid)
  32. destination = "user/1001"
  33. # msg = ESL.ESLevent("sendmsg", call_uuid)
  34. # msg.addHeader("call-command", "execute")
  35. # msg.addHeader("execute-app-name", "bridge")
  36. # msg.addHeader("execute-app-arg", destination)
  37. # msg.addHeader("execute-app-arg", destination)
  38. # # 发送消息以执行 bridge 操作
  39. # self.con.sendEvent(msg)
  40. print(f"Call {call_uuid} is bridged to {destination}")
  41. def stop(self):
  42. self.is_stopping = True
  43. class OutboundClient:
  44. @staticmethod
  45. def ack(esl, con):
  46. info = con.getInfo()
  47. print(json.loads(info.serialize('json')))
  48. event_name = info.getHeader("Event-Name")
  49. uuid = info.getHeader("unique-id")
  50. print(uuid, event_name)
  51. # destination = "user/1001"
  52. # msg = ESL.ESLevent("sendmsg", uuid)
  53. # msg.addHeader("call-command", "execute")
  54. # msg.addHeader("execute-app-name", "bridge")
  55. # msg.addHeader("execute-app-arg", destination)
  56. # # 发送消息以执行 bridge 操作
  57. # con.sendEvent(msg)
  58. # print(f"Call {uuid} is bridged to {destination}")
  59. # con.execute("park", "", uuid)
  60. con.execute("bridge", "user/1001", uuid)
  61. # con.execute("answer", "", uuid)
  62. # con.execute("playback", "/Users/davidliu/sip/pjproject/voip/test111.wav", uuid)
  63. con.disconnect()
  64. class ESLRequestHandler(socketserver.BaseRequestHandler):
  65. def setup(self):
  66. print(self.client_address, 'connected!')
  67. fd = self.request.fileno()
  68. print('0000', fd)
  69. con = ESL.ESLconnection(fd)
  70. print('Connected: ', con.connected())
  71. if con.connected():
  72. threading.Thread(target=self.ack, args=(con, )).start()
  73. else:
  74. print("Failed to connect to FreeSWITCH")
  75. def ack(self, con):
  76. OutboundClient.ack(self, con)
  77. def start(self, HOST= '0.0.0.0', PORT=8084):
  78. # HOST, PORT = "0.0.0.0", 8084
  79. # 创建一个 TCP 服务器
  80. with socketserver.ThreadingTCPServer((HOST, PORT), self.ESLRequestHandler) as server:
  81. print(f"ESL server listening on {HOST}:{PORT}")
  82. server.serve_forever()
  83. def main():
  84. on = InboundClient()
  85. # out = OutboundClient()
  86. # threading.Thread(target=out.start, args=()).start()
  87. threading.Thread(target=on.start, args=()).start()
  88. if __name__ == '__main__':
  89. main()