gateway.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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('localhost', '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("bridge", "user/1001", call_uuid)
  28. destination = "user/1001"
  29. # msg = ESL.ESLevent("sendmsg", call_uuid)
  30. # msg.addHeader("call-command", "execute")
  31. # msg.addHeader("execute-app-name", "bridge")
  32. # msg.addHeader("execute-app-arg", destination)
  33. # msg.addHeader("execute-app-arg", destination)
  34. # # 发送消息以执行 bridge 操作
  35. # self.con.sendEvent(msg)
  36. print(f"Call {call_uuid} is bridged to {destination}")
  37. def stop(self):
  38. self.is_stopping = True
  39. class OutboundClient:
  40. @staticmethod
  41. def ack(esl, con):
  42. info = con.getInfo()
  43. print(json.loads(info.serialize('json')))
  44. event_name = info.getHeader("Event-Name")
  45. uuid = info.getHeader("unique-id")
  46. print(uuid, event_name)
  47. # destination = "user/1001"
  48. # msg = ESL.ESLevent("sendmsg", uuid)
  49. # msg.addHeader("call-command", "execute")
  50. # msg.addHeader("execute-app-name", "bridge")
  51. # msg.addHeader("execute-app-arg", destination)
  52. # # 发送消息以执行 bridge 操作
  53. # con.sendEvent(msg)
  54. # print(f"Call {uuid} is bridged to {destination}")
  55. # con.execute("park", "", uuid)
  56. con.execute("bridge", "user/1001", uuid)
  57. # con.execute("answer", "", uuid)
  58. # con.execute("playback", "/Users/davidliu/sip/pjproject/voip/test111.wav", uuid)
  59. con.disconnect()
  60. class ESLRequestHandler(socketserver.BaseRequestHandler):
  61. def setup(self):
  62. print(self.client_address, 'connected!')
  63. fd = self.request.fileno()
  64. print('0000', fd)
  65. con = ESL.ESLconnection(fd)
  66. print('Connected: ', con.connected())
  67. if con.connected():
  68. threading.Thread(target=self.ack, args=(con, )).start()
  69. else:
  70. print("Failed to connect to FreeSWITCH")
  71. def ack(self, con):
  72. OutboundClient.ack(self, con)
  73. def start(self, HOST= '0.0.0.0', PORT=8084):
  74. # HOST, PORT = "0.0.0.0", 8084
  75. # 创建一个 TCP 服务器
  76. with socketserver.ThreadingTCPServer((HOST, PORT), self.ESLRequestHandler) as server:
  77. print(f"ESL server listening on {HOST}:{PORT}")
  78. server.serve_forever()
  79. def main():
  80. on = InboundClient()
  81. # out = OutboundClient()
  82. # threading.Thread(target=out.start, args=()).start()
  83. threading.Thread(target=on.start, args=()).start()
  84. if __name__ == '__main__':
  85. main()