#!/usr/bin/env python3 # encoding:utf-8 import json import ESL import threading import socketserver from http.server import BaseHTTPRequestHandler from http.server import HTTPServer class InboundClient: def __init__(self): self.is_stopping = False self.con = ESL.ESLconnection('localhost', '8021', '4918257983818884358') def start(self): if self.con.connected(): print('inbound esl connected ... ') self.con.events('plain', 'all') while not self.is_stopping: e = self.con.recvEvent() if e: print(json.loads(e.serialize('json'))) # print(e.serialize('json')) event_name = e.getHeader("Event-Name") if event_name == "CHANNEL_PARK": # 获取通话的 UUID call_uuid = e.getHeader("Unique-ID") print(f"Incoming call with UUID: {call_uuid}") self.con.execute("bridge", "user/1001", call_uuid) destination = "user/1001" # msg = ESL.ESLevent("sendmsg", call_uuid) # msg.addHeader("call-command", "execute") # msg.addHeader("execute-app-name", "bridge") # msg.addHeader("execute-app-arg", destination) # msg.addHeader("execute-app-arg", destination) # # 发送消息以执行 bridge 操作 # self.con.sendEvent(msg) print(f"Call {call_uuid} is bridged to {destination}") def stop(self): self.is_stopping = True class OutboundClient: @staticmethod def ack(esl, con): info = con.getInfo() print(json.loads(info.serialize('json'))) event_name = info.getHeader("Event-Name") uuid = info.getHeader("unique-id") print(uuid, event_name) # destination = "user/1001" # msg = ESL.ESLevent("sendmsg", uuid) # msg.addHeader("call-command", "execute") # msg.addHeader("execute-app-name", "bridge") # msg.addHeader("execute-app-arg", destination) # # 发送消息以执行 bridge 操作 # con.sendEvent(msg) # print(f"Call {uuid} is bridged to {destination}") # con.execute("park", "", uuid) con.execute("bridge", "user/1001", uuid) # con.execute("answer", "", uuid) # con.execute("playback", "/Users/davidliu/sip/pjproject/voip/test111.wav", uuid) con.disconnect() class ESLRequestHandler(socketserver.BaseRequestHandler): def setup(self): print(self.client_address, 'connected!') fd = self.request.fileno() print('0000', fd) con = ESL.ESLconnection(fd) print('Connected: ', con.connected()) if con.connected(): threading.Thread(target=self.ack, args=(con, )).start() else: print("Failed to connect to FreeSWITCH") def ack(self, con): OutboundClient.ack(self, con) def start(self, HOST= '0.0.0.0', PORT=8084): # HOST, PORT = "0.0.0.0", 8084 # 创建一个 TCP 服务器 with socketserver.ThreadingTCPServer((HOST, PORT), self.ESLRequestHandler) as server: print(f"ESL server listening on {HOST}:{PORT}") server.serve_forever() def main(): on = InboundClient() # out = OutboundClient() # threading.Thread(target=out.start, args=()).start() threading.Thread(target=on.start, args=()).start() if __name__ == '__main__': main()