api.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. #!/usr/bin/env python3
  2. # encoding:utf-8
  3. import json
  4. from typing import Dict, Any, Optional, List
  5. from src.core.callcenter.constant import get_json_dict
  6. from src.core.callcenter.exception import SipUriSyntaxException
  7. from src.core.datasource import SIP_SERVER
  8. from src.core.callcenter.esl.constant.esl_constant import SPLIT, SIP_HEADER
  9. from src.core.callcenter.enumeration import CallType, DeviceType, Direction, CdrType, NextType
  10. class BaseApi:
  11. @classmethod
  12. def from_json(cls, json_string):
  13. data = get_json_dict(json_string)
  14. return cls(**data)
  15. def to_json_string(self):
  16. return json.dumps(self, default=lambda x: x.__dict__, ensure_ascii=False)
  17. def __repr__(self):
  18. return self.to_json_string()
  19. class AgentCallRequest(BaseApi):
  20. """
  21. 呼叫请求对象
  22. """
  23. def __init__(self, saas_id, agent_id, call_type: CallType, caller, called, caller_display="", called_display="",
  24. uuid1=None, uuid2=None, follow_data: Dict[str, Any] = {}):
  25. # 租户id
  26. self.saas_id = saas_id
  27. # 坐席号
  28. self.agent_id = agent_id
  29. # 呼叫类型
  30. self.call_type: CallType = call_type
  31. # 主叫,如果没有传,则使用坐席号码
  32. self.caller = caller
  33. # 被叫
  34. self.called = called
  35. # 主叫显号,接口没有传就用主技能组配置
  36. self.caller_display = caller_display
  37. # 被叫显号,接口没传就用主技能配置
  38. self.called_display = called_display
  39. # uuid1
  40. self.uuid1 = uuid1
  41. # uuid2
  42. self.uuid2 = uuid2
  43. # 随路数据
  44. self.follow_data = follow_data
  45. class AgentRequest(BaseApi):
  46. def __init__(self, saas_id, agent_number, agent_name, out_id, agent_password, agent_type, phone_number, distribute,
  47. agent_state, identity_type):
  48. # 租户隔离
  49. self.saas_id = saas_id
  50. # 坐席工号, 生成返回
  51. self.agent_number = agent_number
  52. # 坐席名称
  53. self.agent_name = agent_name
  54. # 外部展示Id
  55. self.out_id = out_id
  56. # 坐席密码
  57. self.agent_password = agent_password
  58. # 坐席类型 0:普通坐席 ;1:组长:2:主管
  59. self.agent_type = agent_type
  60. # 分机号
  61. self.phone_number = phone_number
  62. # 分配标志 0:不参与排队;1:参与排队
  63. self.distribute = distribute
  64. # 账号状态 0:可用;1:禁用
  65. self.agent_state = agent_state
  66. # 身份状态
  67. self.identity_type = identity_type
  68. class AgentQueryRequest(BaseApi):
  69. def __init__(self, saas_id, agent_number, out_id, agent_type, agent_name, page=1, size=10):
  70. # 租户隔离
  71. self.saas_id = saas_id
  72. # 坐席工号
  73. self.agent_number = agent_number
  74. # 坐席名称
  75. self.agent_name = agent_name
  76. # 外部展示Id
  77. self.out_id = out_id
  78. # 坐席类型 0:普通坐席 ;1:组长:2:主管
  79. self.agent_type = agent_type
  80. self.page = page,
  81. self.size = size
  82. class AgentActionRequest(BaseApi):
  83. """
  84. 坐席操作
  85. """
  86. def __init__(self, saas_id, agent_id, agent_number=None, out_id=None, identity_type=None, scene='manual', service_id=None, **kwargs):
  87. self.saas_id =saas_id
  88. # 坐席工号
  89. self.agent_id = agent_id
  90. # # 坐席工号
  91. self.agent_number = agent_number
  92. # 外部展示Id
  93. self.out_id = out_id
  94. # 身份状态
  95. self.identity_type = identity_type
  96. # 场景 manual:手动外呼; robot:机器人外呼; monitor:监听
  97. self.scene = scene
  98. self.service_id = service_id
  99. class HumanServiceQueryRequest(BaseApi):
  100. """人工组查询"""
  101. def __init__(self, saas_id, service_id, agent_state, agent_ids: Dict[str, Any] = {}):
  102. self.saasId = saas_id
  103. self.serviceId = service_id
  104. self.agentState = agent_state
  105. self.agentIds = agent_ids
  106. class AgentMonitorData(BaseApi):
  107. """坐席状态信息"""
  108. def __init__(self, saas_id=None, agent_num=None, agent_name=None, out_id=None,
  109. check_state=None, online_state=None, check_in_time=None, check_out_time=None,
  110. service_state=None, busy_time=None, idle_time=None, call_time=None, hang_time=None,
  111. heart_state=None, heart_time=None, update_time=None, check_scene=None):
  112. self.saas_id = saas_id
  113. self.agent_num = agent_num
  114. self.agent_name = agent_name
  115. self.out_id = out_id
  116. self.check_state = check_state
  117. self.online_state = online_state
  118. self.check_in_time = check_in_time
  119. self.check_out_time = check_out_time
  120. self.service_state = service_state
  121. self.busy_time = busy_time
  122. self.idle_time = idle_time
  123. self.call_time = call_time
  124. self.hang_time = hang_time
  125. self.heart_state = heart_state
  126. self.heart_time = heart_time
  127. self.update_time = update_time
  128. self.check_scene = check_scene
  129. class AgentEventData(BaseApi):
  130. def __init__(self, app_code, user_id, data: Dict[str, Any] = {}):
  131. self.appCode = app_code
  132. self.userId = user_id
  133. self.data = data
  134. class AgentStateData(BaseApi):
  135. def __init__(self, _s=None, _t=None, status=None, time=None, assign_time=None, phone_num=None):
  136. self._s = _s
  137. self._t = _t
  138. self.status = status
  139. self.time = time
  140. self.assign_time = assign_time
  141. self.phone_num = phone_num
  142. class HangupCallRequest(BaseApi):
  143. def __init__(self, saas_id, call_id, agent_number):
  144. # saasId(必填)
  145. self.saas_id = saas_id
  146. # 呼叫唯一id(选填)
  147. self.call_id = call_id
  148. # 分机号(必填)
  149. self.agent_number = agent_number
  150. class CheckInCallRequest(BaseApi):
  151. """检查是否在通话中实体"""
  152. def __init__(self, saas_id, agent_number):
  153. self.saas_id = saas_id
  154. self.agent_number = agent_number
  155. class MakeCallContext(BaseApi):
  156. def __init__(self,
  157. sip_server=SIP_SERVER,
  158. route_gateway_name="gateway-fxo",
  159. display: Optional[str] = None,
  160. caller: Optional[str] = None,
  161. called: Optional[str] = None,
  162. call_id: Optional[str] = None,
  163. device_id: Optional[str] = None,
  164. eavesdrop: Optional[str] = None,
  165. device_type: Optional[int] = None,
  166. timeout: Optional[int] = 30,
  167. originate_timeout: Optional[int] = 30,
  168. sip_header_map: Optional[Dict[str, str]] = [],
  169. called_prefix: Optional[str] = "",
  170. service_id: Optional[str] = None,
  171. call_type: Optional[int] = None):
  172. # fs 地址
  173. self.sip_server = sip_server
  174. # 线路名(非必传)
  175. self.route_gateway_name = route_gateway_name
  176. # 外显号(必传)
  177. self.display = display
  178. # 该腿的呼叫者(必传)
  179. self.caller = caller
  180. # 该腿的被呼叫者(必传)
  181. self.called = called
  182. # 该腿的callId (必传)
  183. self.call_id = call_id
  184. # 该腿的uuid(必传)
  185. self.device_id = device_id
  186. # 是否是监听腿 监听腿必填true,其它不填
  187. self.eavesdrop = eavesdrop
  188. # 当前腿的类型(必填 客户 坐席 监听者 机器人)
  189. self.device_type = device_type
  190. # 超时挂断
  191. self.timeout = timeout
  192. # 超时挂断(后面可以考虑删掉)
  193. self.originate_timeout = originate_timeout
  194. # 额外的sip头添加(非必填)
  195. self.sip_header_map = sip_header_map or {}
  196. # 被叫前缀 (用户腿必填)
  197. self.called_prefix = called_prefix
  198. # 任务id(机器人外呼必填)
  199. self.service_id = service_id
  200. # 呼叫类型(必填)
  201. self.call_type = call_type
  202. def get_sip_header(self) -> str:
  203. headers = [
  204. f"{SIP_HEADER}contact_user={self.display}",
  205. "ring_asr=true",
  206. f"origination_caller_id_number={self.display}",
  207. f"origination_caller_id_name={self.display}",
  208. f"origination_uuid={self.device_id}",
  209. "absolute_codec_string=^^:PCMU:PCMA,",
  210. ]
  211. if self.originate_timeout is not None:
  212. headers.append(f"originate_timeout={self.originate_timeout}")
  213. headers += [
  214. "RECORD_STEREO=true",
  215. "RECORD_APPEND=true",
  216. f"{SIP_HEADER}call_id={self.call_id}",
  217. f"{SIP_HEADER}caller={self.caller}",
  218. f"{SIP_HEADER}called={self.called}",
  219. f"{SIP_HEADER}device_id={self.device_id}",
  220. f"{SIP_HEADER}device_type={self.device_type}",
  221. ]
  222. if self.eavesdrop:
  223. headers.append(f"{SIP_HEADER}eavesdrop={self.eavesdrop}")
  224. if self.service_id:
  225. headers.append(f"{SIP_HEADER}service_id={self.service_id}")
  226. if self.device_type == DeviceType.CUSTOMER.code:
  227. headers += [
  228. "RECORD_STEREO_SWAP=true"
  229. ]
  230. else:
  231. headers += [
  232. "RECORD_STEREO_SWAP=false",
  233. "continue_on_fail=true"
  234. ]
  235. if self.sip_header_map:
  236. headers.extend([f"{SIP_HEADER}{k}={v}" for k, v in self.sip_header_map.items()])
  237. return SPLIT.join(headers)
  238. def get_called(self) -> str:
  239. if self.called_prefix and self.device_type == DeviceType.CUSTOMER.code:
  240. return f"{self.called_prefix}{self.called}"
  241. return self.called
  242. def get_realm(self):
  243. sip_uri = SipURI(self.sip_server)
  244. return sip_uri.get_host()
  245. class SipURI:
  246. DEFAULT_PORT = -1
  247. SIP_SCHEME = "sip"
  248. SCHEME_SEPARATOR = ':'
  249. def __init__(self, sip_uri: str):
  250. self.string_representation = sip_uri
  251. self.userinfo = None
  252. self.host = None
  253. self.port = self.DEFAULT_PORT
  254. self.uri_parameters = {}
  255. scheme = f"{self.SIP_SCHEME}{self.SCHEME_SEPARATOR}"
  256. if not sip_uri.startswith(scheme):
  257. raise SipUriSyntaxException(f"SIP URI must start with {scheme}")
  258. buf = sip_uri[len(scheme):]
  259. at_pos = buf.find("@")
  260. if at_pos == 0:
  261. raise SipUriSyntaxException("userinfo cannot start with a '@'")
  262. if at_pos > 0:
  263. self.userinfo = buf[:at_pos]
  264. buf = buf[at_pos + 1:]
  265. end_hostport = buf.find(";")
  266. if end_hostport == 0:
  267. raise SipUriSyntaxException("hostport not present or it cannot start with ';'")
  268. if end_hostport < 0:
  269. end_hostport = len(buf)
  270. hostport = buf[:end_hostport]
  271. buf = buf[end_hostport:]
  272. colon_pos = hostport.find(":")
  273. if colon_pos > -1:
  274. if colon_pos == len(hostport) - 1:
  275. raise SipUriSyntaxException("hostport cannot terminate with a ':'")
  276. self.port = int(hostport[colon_pos + 1:])
  277. self.host = hostport[:colon_pos]
  278. else:
  279. self.host = hostport
  280. if buf == ";":
  281. buf = ""
  282. while buf:
  283. if buf[0] == ";":
  284. buf = buf[1:]
  285. next_semicolon = buf.find(";")
  286. if next_semicolon < 0:
  287. next_semicolon = len(buf)
  288. next_equals = buf.find("=")
  289. if next_equals < 0 or next_equals > next_semicolon:
  290. next_equals = next_semicolon
  291. key = buf[:next_equals]
  292. value = buf[next_equals + 1:next_semicolon] if next_equals < next_semicolon else ""
  293. self.uri_parameters[key] = value
  294. buf = buf[next_semicolon:]
  295. def __str__(self):
  296. return self.string_representation
  297. def get_host(self):
  298. return self.host
  299. def get_port(self):
  300. return self.port
  301. def get_uri_parameters(self):
  302. return self.uri_parameters
  303. def get_userinfo(self):
  304. return self.userinfo
  305. class AgentInfo(BaseApi):
  306. def __init__(self, saas_id=None, agent_number=None, realm=None, sip_server=None,
  307. call_id=None, device_id=None, real_device_id=None, line_id=None, fs_user=None, domain=None,
  308. service_time=0, max_ready_time=0, total_ready_time=0, ready_times=0, not_ready_times=0,
  309. total_after_time=0, max_talk_time=0, total_talk_time=0, total_ring_times=0, total_answer_times=0):
  310. self.saas_id = saas_id
  311. self.agent_number = agent_number
  312. self.realm = realm
  313. self.sip_server = sip_server
  314. self.call_id = call_id
  315. self.device_id = device_id
  316. # 机器人外呼时真实fs
  317. self.real_device_id = real_device_id
  318. self.line_id = line_id
  319. self.fs_user = fs_user
  320. self.domain = domain
  321. # 坐席最近的一次服务时间,电话则是振铃时间(秒)
  322. self.service_time = service_time
  323. # 最大空闲时长
  324. self.max_ready_time = max_ready_time
  325. # 累计空闲
  326. self.total_ready_time = total_ready_time
  327. # 空闲次数
  328. self.ready_times = ready_times
  329. # 忙碌次数
  330. self.not_ready_times = not_ready_times
  331. # 累计话后时间长
  332. self.total_after_time = total_after_time
  333. # 最大通话时长
  334. self.max_talk_time = max_talk_time
  335. # 当日累计通话时长
  336. self.total_talk_time = total_talk_time
  337. # 振铃次数
  338. self.total_ring_times = total_ring_times
  339. # 当日累计接听次数
  340. self.total_answer_times = total_answer_times
  341. def get_realm(self):
  342. """
  343. Get the realm by extracting the host from the SIP server if available,
  344. otherwise return the current realm.
  345. """
  346. try:
  347. if self.sip_server:
  348. sip_uri = SipURI(self.sip_server)
  349. return sip_uri.get_host() if sip_uri.get_host() else self.realm
  350. except Exception as ex:
  351. print(f"Error parsing SIP server URI: {ex}")
  352. return self.realm
  353. def is_fs_user(self):
  354. """
  355. Determine if the agent is a FreeSWITCH user.
  356. Default is True if fs_user is None or True.
  357. """
  358. return self.fs_user is None or self.fs_user is True
  359. class CallInfo(BaseApi):
  360. def __init__(self, core_uuid=None, call_id=None, conference=None, saas_id=None, group_id=None,
  361. hidden_customer=0, caller_display=None, caller=None, called_display=None, called=None,
  362. number_location=None, agent_key=None, agent_name=None, login_type=None, ivr_id=None, task_id=None,
  363. media_host=None, sip_server=None, client_host=None, record=None, record2=None, record_time=None,
  364. call_time=None, call_type=None, direction=None, answer_flag=None, wait_time=None, answer_count=0,
  365. hangup_dir=None, sdk_hangup=0, hangup_code=None, answer_time=None, end_time=None, talk_time=None,
  366. frist_queue_time=None, queue_start_time=None, queue_end_time=None, overflow_count=0,
  367. uuid1=None, uuid2=None, cdr_notify_url=None, queue_level=None, device_list=[],
  368. device_info_map: Dict[str, Any] = {}, follow_data: Dict[str, Any] = {},
  369. process_data: Dict[str, Any] = {}, next_commands=[], call_details=[]):
  370. self.core_uuid = core_uuid # 通话唯一标识
  371. self.call_id = call_id # 通话唯一标识
  372. self.conference = conference # 会议号
  373. self.saas_id = saas_id # 企业id
  374. self.group_id = group_id # 所在技能组id
  375. self.hidden_customer = hidden_customer # 隐藏客户号码(0:不隐藏;1:隐藏)
  376. self.caller_display = caller_display # 主叫显号
  377. self.caller = caller # 主叫
  378. self.called_display = called_display # 被叫显号
  379. self.called = called # 被叫
  380. self.number_location = number_location # 号码归属地
  381. self.agent_key = agent_key # 坐席
  382. self.agent_name = agent_name # 坐席名称
  383. self.login_type = login_type # 坐席登录类型
  384. self.ivr_id = ivr_id # ivr
  385. self.task_id = task_id # 任务ID
  386. self.media_host = media_host # 媒体
  387. self.sip_server = sip_server # 服务地址
  388. self.client_host = client_host # 客户服务地址
  389. self.record = record # 录音地址
  390. self.record2 = record2 # 第三方存储地址
  391. self.record_time = record_time # 录音开始时间
  392. self.call_time = call_time # 呼叫开始时间
  393. self.call_type = call_type # 呼叫类型 (Enum CallType)
  394. self.direction = direction # 呼叫方向 (Enum Direction)
  395. self.answer_flag = answer_flag # 通话标识
  396. self.wait_time = wait_time # 等待时长
  397. self.answer_count = answer_count # 应答设备数
  398. self.hangup_dir = hangup_dir # 挂机方向 (1主叫挂机, 2:被叫挂机, 3:平台挂机)
  399. self.sdk_hangup = sdk_hangup # sdk挂机标志
  400. self.hangup_code = hangup_code # 挂机原因
  401. self.answer_time = answer_time # 接听时间
  402. self.end_time = end_time # 最后一侧电话挂机时间
  403. self.talk_time = talk_time # 通话时长(秒)
  404. self.frist_queue_time = frist_queue_time # 第一次进队列时间
  405. self.queue_start_time = queue_start_time # 进入技能组时间
  406. self.queue_end_time = queue_end_time # 出技能组时间
  407. self.overflow_count = overflow_count # 溢出次数
  408. self.uuid1 = uuid1 # uuid1
  409. self.uuid2 = uuid2 # uuid2
  410. self.cdr_notify_url = cdr_notify_url # 话单通知地址
  411. self.queue_level = queue_level # 排队等级,默认是进队列时间
  412. self.device_list = device_list # 当前通话的设备
  413. self.device_info_map = device_info_map, # K-V
  414. self.follow_data = follow_data # 呼叫随路数据(作为落单数据)
  415. self.process_data = process_data # 模块流程间数据
  416. self.next_commands = next_commands # 执行下一步命令
  417. self.call_details = call_details # 电话流程
  418. @classmethod
  419. def from_json(cls, json_string):
  420. data = get_json_dict(json_string)
  421. device_list = [DeviceInfo(**x) for x in data.get('device_list', [])]
  422. device_info_map: Dict[str, Any] = {key: DeviceInfo(**value) for key, value in data.get('device_info_map', {}).items()}
  423. follow_data: Dict[str, Any] = {key: value for key, value in data.get('follow_data', {}).items()}
  424. process_data: Dict[str, Any] = {key: value for key, value in data.get('process_data', {}).items()}
  425. next_commands = [NextCommand(**x) for x in data.get('next_commands', [])]
  426. call_details = [CallDetail(**x) for x in data.get('call_details', [])]
  427. return cls(core_uuid=data.get('core_uuid'), call_id=data.get('call_id'), conference=data.get('conference'),
  428. saas_id=data.get('saas_id'), group_id=data.get('group_id'),
  429. hidden_customer=data.get('hidden_customer'), caller_display=data.get('caller_display'),
  430. caller=data.get('caller'), called_display=data.get('called_display'), called=data.get('called'),
  431. number_location=data.get('number_location'), agent_key=data.get('agent_key'),
  432. agent_name=data.get('agent_name'), login_type=data.get('login_type'), ivr_id=data.get('ivr_id'),
  433. task_id=data.get('task_id'), media_host=data.get('media_host'), sip_server=data.get('sip_server'),
  434. client_host=data.get('client_host'), record=data.get('record'), record2=data.get('record2'),
  435. record_time=data.get('record_time'), call_time=data.get('call_time'),
  436. call_type=data.get('call_type'), direction=data.get('direction'),
  437. answer_flag=data.get('answer_flag'), wait_time=data.get('wait_time'),
  438. answer_count=data.get('answer_count'), hangup_dir=data.get('hangup_dir'),
  439. sdk_hangup=data.get('sdk_hangup'), hangup_code=data.get('hangup_code'),
  440. answer_time=data.get('answer_time'), end_time=data.get('end_time'), talk_time=data.get('talk_time'),
  441. frist_queue_time=data.get('frist_queue_time'), queue_start_time=data.get('queue_start_time'),
  442. queue_end_time=data.get('queue_end_time'), overflow_count=data.get('overflow_count'),
  443. uuid1=data.get('uuid1'), uuid2=data.get('uuid2'), cdr_notify_url=data.get('cdr_notify_url'),
  444. queue_level=data.get('queue_level'), device_list=device_list,
  445. device_info_map=device_info_map, follow_data=follow_data, process_data=process_data,
  446. next_commands=next_commands, call_details=call_details)
  447. def __str__(self):
  448. return self.to_json_string()
  449. def to_json_string(self):
  450. new_dict = {
  451. "core_uuid": self.core_uuid,
  452. "call_id": self.call_id,
  453. "conference": self.conference,
  454. "saas_id": self.saas_id,
  455. "group_id": self.group_id,
  456. "hidden_customer": self.hidden_customer,
  457. "caller_display": self.caller_display,
  458. "caller": self.caller,
  459. "called_display": self.called_display,
  460. "called": self.called,
  461. "number_location": self.number_location,
  462. "agent_key": self.agent_key,
  463. "agent_name": self.agent_name,
  464. "login_type": self.login_type,
  465. "ivr_id": self.ivr_id,
  466. "task_id": self.task_id,
  467. "media_host": self.media_host,
  468. "sip_server": self.sip_server,
  469. "client_host": self.client_host,
  470. "record": self.record,
  471. "record2": self.record2,
  472. "record_time": self.record_time,
  473. "call_time": self.call_time,
  474. "call_type": self.call_type,
  475. "direction": self.direction,
  476. "answer_flag": self.answer_flag,
  477. "wait_time": self.wait_time,
  478. "answer_count": self.answer_count,
  479. "hangup_dir": self.hangup_dir,
  480. "sdk_hangup": self.sdk_hangup,
  481. "hangup_code": self.hangup_code,
  482. "answer_time": self.answer_time,
  483. "end_time": self.end_time,
  484. "talk_time": self.talk_time,
  485. "frist_queue_time": self.frist_queue_time,
  486. "queue_start_time": self.queue_start_time,
  487. "queue_end_time": self.queue_end_time,
  488. "overflow_count": self.overflow_count,
  489. "uuid1": self.uuid1,
  490. "uuid2": self.uuid2,
  491. "cdr_notify_url": self.cdr_notify_url,
  492. "queue_level": self.queue_level,
  493. "device_list": [vars(x) for x in self.device_list],
  494. "device_info_map": {key: vars(value) for key, value in self.device_info_map.items()},
  495. "follow_data": {key: vars(value) for key, value in self.follow_data.items()},
  496. "process_data": {key: vars(value) for key, value in self.process_data.items()},
  497. "next_commands": [vars(x) for x in self.next_commands],
  498. "call_details": [vars(x) for x in self.call_details],
  499. }
  500. print(new_dict)
  501. return json.dumps(new_dict, default=lambda x: x.__dict__, ensure_ascii=False)
  502. class DeviceInfo(BaseApi):
  503. def __init__(self, call_id=None, conference=None, device_id=None, agent_key=None, agent_name=None, device_type=None,
  504. cdr_type=None, from_agent=None, caller=None, called=None, display=None, called_location=None,
  505. caller_location=None, call_time=None, ring_start_time=None, ring_end_time=None, answer_time=None,
  506. bridge_time=None, end_time=None, talk_time=None, sip_protocol=None, channel_name=None,
  507. hangup_cause=None, ring_cause=None, sip_status=None, record=None, record_time=None,
  508. record_start_time=None, state=None, apparent_number=None, caller_display=None):
  509. self.call_id = call_id # 通话唯一标识
  510. self.conference = conference # 会议模式
  511. self.device_id = device_id # 设备id
  512. self.agent_key = agent_key # 坐席
  513. self.agent_name = agent_name # 坐席
  514. self.device_type = device_type # 1:坐席,2:客户,3:外线
  515. self.cdr_type = cdr_type # 1:呼入,2:外呼,3:内呼,4:转接,5:咨询,6:监听,7:强插,8:耳语
  516. self.from_agent = from_agent # 咨询或转接来源
  517. self.caller = caller # 主叫
  518. self.called = called # 被叫
  519. self.display = display # 显号
  520. self.called_location = called_location # 被叫归属地
  521. self.caller_location = caller_location # 被叫归属地
  522. self.call_time = call_time # 呼叫开始时间
  523. self.ring_start_time = ring_start_time # 振铃开始时间
  524. self.ring_end_time = ring_end_time # 振铃结束时间
  525. self.answer_time = answer_time # 接通时间
  526. self.bridge_time = bridge_time # 桥接时间
  527. self.end_time = end_time # 结束时间
  528. self.talk_time = talk_time # 通话时长
  529. self.sip_protocol = sip_protocol # 信令协议(tcp/udp)
  530. self.channel_name = channel_name # 呼叫地址
  531. self.hangup_cause = hangup_cause # 挂机原因
  532. self.ring_cause = ring_cause # 回铃音识别
  533. self.sip_status = sip_status # sip状态
  534. self.record = record # 录音地址
  535. self.record_time = record_time # 录音时长
  536. self.record_start_time = record_start_time # 录音开始时间
  537. self.state = state # 当前设备状态
  538. self.apparent_number = apparent_number # 外显号
  539. self.caller_display = caller_display # 主叫号
  540. class NextCommand(BaseApi):
  541. def __init__(self, device_id, next_type, next_value=None):
  542. # 记录执行设备
  543. self.device_id = device_id
  544. # 下一步执行命令
  545. self.next_type = next_type
  546. # 执行参数
  547. self.next_value = next_value
  548. class CallDetail(BaseApi):
  549. def __init__(self, id=None, cts=None, start_time=None, end_time=None, call_id=None,
  550. detail_index=None, transfer_type=None, transfer_id=None, reason=None,
  551. month=None, ext1=None, ext2=None, status=None):
  552. self.id = id # PK
  553. self.cts = cts # 创建时间
  554. self.start_time = start_time # 开始时间
  555. self.end_time = end_time # 结束时间
  556. self.call_id = call_id # 通话ID
  557. self.detail_index = detail_index # 顺序
  558. self.transfer_type = transfer_type # 类型
  559. self.transfer_id = transfer_id # 转接ID
  560. self.reason = reason # 出队列原因
  561. self.month = month # 月份
  562. self.ext1 = ext1 # 扩展字段1
  563. self.ext2 = ext2 # 扩展字段2
  564. self.status = status # 状态
  565. class RouteGateway(BaseApi):
  566. def __init__(self, id=None, saas_id=None, cts=None, uts=None, name=None, media_host=None,
  567. media_port=None, caller_prefix=None, called_prefix=None,
  568. profile='gateway/gateway-fxo', sip_header1='P-LIBRA-CallId=#{[callId]}',
  569. sip_header2='P-LIBRA-deviceId=#{[deviceId]}', sip_header3=None, status=None):
  570. self.id = id # PK
  571. self.saas_id = saas_id
  572. self.cts = cts # 创建时间
  573. self.uts = uts # 修改时间
  574. self.name = name # 号码
  575. self.media_host = media_host # 媒体地址
  576. self.media_port = media_port # 媒体端口
  577. self.caller_prefix = caller_prefix # 主叫号码前缀
  578. self.called_prefix = called_prefix # 被叫号码前缀
  579. self.profile = profile # 媒体拨号计划文件
  580. self.sip_header1 = sip_header1 # SIP头1
  581. self.sip_header2 = sip_header2 # SIP头2
  582. self.sip_header3 = sip_header3 # SIP头3
  583. self.status = status # 状态
  584. # 机器人外呼
  585. class BotChatRequest:
  586. def __init__(self, nodeId=None, userId=None, sessionId=None, taskId=None, asrText=None, recordId=None,
  587. ext: Dict = {}):
  588. self.nodeId = nodeId # 节点id
  589. self.userId = userId # 用户id
  590. self.sessionId = sessionId # 会话id
  591. self.recordId = recordId # 唯一标识
  592. self.taskId = taskId # 机器人任务id
  593. self.asrText = asrText # asr识别的文本
  594. self.ext = ext
  595. def to_json_string(self):
  596. return json.dumps(self.__dict__, ensure_ascii=False)
  597. @classmethod
  598. def from_json(cls, json_string):
  599. data = json.loads(json_string)
  600. return cls(**data)
  601. # def __repr__(self):
  602. # return (f"OutboundRobotRequestParams(node_id={self.node_id}, user_id={self.user_id}, "
  603. # f"session_id={self.session_id}, record_id={self.record_id}, task_id={self.task_id}, "
  604. # f"event_type={self.event_type}, asr_text={self.asr_text}, key_input={self.key_input})")
  605. class ChatAction:
  606. def __init__(self, actionCode=None, actionContent=None):
  607. self.actionCode = actionCode # normal:正常通话;hang:挂断;transfer:转人工
  608. self.actionContent = actionContent # 动作内容
  609. def to_json_string(self):
  610. return json.dumps(self.__dict__, ensure_ascii=False)
  611. @classmethod
  612. def from_json(cls, json_data):
  613. return cls(
  614. actionCode=json_data.get("actionCode"),
  615. actionContent=json_data.get("actionContent")
  616. )
  617. class ChatContent:
  618. def __init__(self, contentType=None, content=None, voiceUrl=None, voiceContent=None):
  619. self.contentType = contentType # 播放类型
  620. self.content = content # 播放内容
  621. self.voiceUrl = voiceUrl # 语音地址
  622. self.voiceContent = voiceContent # 语音文本
  623. def to_json_string(self):
  624. return json.dumps(self.__dict__, ensure_ascii=False)
  625. @classmethod
  626. def from_json(cls, json_data):
  627. return cls(
  628. contentType=json_data.get("contentType"),
  629. content=json_data.get("content"),
  630. voiceUrl=json_data.get("voiceUrl"),
  631. voiceContent=json_data.get("voiceContent")
  632. )
  633. class ChatMessage:
  634. def __init__(self, nodeId=None, contents=None, waitTime=None,
  635. action=None, inputType=None):
  636. self.nodeId = nodeId # 节点id
  637. self.contents = contents if contents is not None else [] # 内容列表
  638. self.waitTime = waitTime # 用户静默时长
  639. self.action = action # 动作代码
  640. self.inputType = inputType # dtmf类型
  641. def to_json_string(self):
  642. return json.dumps({
  643. "nodeId": self.nodeId,
  644. "contents": [content.__dict__ for content in self.contents],
  645. "waitTime": self.waitTime,
  646. "action": self.action.__dict__ if self.action else None,
  647. "inputType": self.inputType
  648. }, ensure_ascii=False)
  649. @classmethod
  650. def from_json(cls, json_data):
  651. contents = [ChatContent.from_json(item) for item in json_data.get("contents", [])]
  652. action = ChatAction.from_json(json_data.get("action", {})) if json_data.get("action") else None
  653. return cls(
  654. nodeId=json_data.get("nodeId"),
  655. contents=contents,
  656. waitTime=json_data.get("waitTime"),
  657. action=action,
  658. inputType=json_data.get("inputType")
  659. )
  660. class ChatResponse:
  661. def __init__(self, data=None, message=None, code=None):
  662. self.data = data if data is not None else ChatMessage()
  663. self.message = message
  664. self.code = code
  665. def to_json_string(self):
  666. return json.dumps({
  667. "data": json.loads(self.data.to_json_string()),
  668. "message": self.message,
  669. "code": self.code
  670. }, ensure_ascii=False)
  671. @classmethod
  672. def from_json(cls, json_string):
  673. data = json.loads(json_string)
  674. response_data = ChatMessage.from_json(data.get("data", {}))
  675. return cls(
  676. data=response_data,
  677. message=data.get("message"),
  678. code=data.get("code")
  679. )