开封医院的中间件

EslClientService.java 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. package midware.service.init;
  2. import com.alibaba.fastjson2.JSON;
  3. import lombok.extern.slf4j.Slf4j;
  4. import midware.service.eslclient.entity.Agent;
  5. import midware.service.eslclient.entity.Channel;
  6. import midware.service.eslclient.EslCommon;
  7. import midware.service.eslclient.EslEventListener;
  8. import midware.service.eslclient.entity.Session;
  9. import midware.util.config.EslClientConfig;
  10. import midware.util.enums.EslAgentEnum;
  11. import midware.util.enums.EslCommandEnum;
  12. import midware.util.enums.EslEventEnum;
  13. import midware.util.helper.StringHelper;
  14. import midware.util.helper.TtsHelper;
  15. import org.freeswitch.esl.client.inbound.Client;
  16. import org.freeswitch.esl.client.transport.SendMsg;
  17. import org.freeswitch.esl.client.transport.message.EslMessage;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.core.annotation.Order;
  20. import org.springframework.stereotype.Service;
  21. import javax.annotation.PostConstruct;
  22. import javax.annotation.PreDestroy;
  23. import java.io.File;
  24. import java.text.SimpleDateFormat;
  25. import java.util.ArrayList;
  26. import java.util.Date;
  27. import java.util.concurrent.ScheduledThreadPoolExecutor;
  28. import java.util.concurrent.TimeUnit;
  29. import java.util.stream.Stream;
  30. @Slf4j
  31. @Service
  32. @Order(1)
  33. public class EslClientService {
  34. @Autowired
  35. private Client client;
  36. @Autowired
  37. private EslClientConfig config;
  38. @PostConstruct
  39. public void init() {
  40. client.addEventListener(new EslEventListener());
  41. //单独起1个线程,定时检测连接状态
  42. new ScheduledThreadPoolExecutor(1).scheduleAtFixedRate(() -> {
  43. if (!client.canSend()) {
  44. try {
  45. if (EslCommon.agents.size() > 0) {
  46. for (Agent a : EslCommon.agents) {
  47. logout(a.getAgent(), a.getGroup(),"");
  48. a.setAgentState(EslAgentEnum.logout.ordinal());
  49. }
  50. EslCommon.agents=new ArrayList<>();
  51. }
  52. client.connect(config.getHost(), config.getPort(), config.getPassword(), config.getTimeout());
  53. //移除事件
  54. client.cancelEventSubscriptions();
  55. Thread.sleep(500);
  56. //移除事件
  57. client.cancelEventSubscriptions();
  58. //设置监听事件
  59. client.setEventSubscriptions("plain", "all");
  60. Stream.of(EslEventEnum.values()).filter(s -> StringHelper.isEmpty(s.getSubclass()))
  61. .map(Enum::name).forEach(s -> client.addEventFilter("Event-Name", s));
  62. Stream.of(EslEventEnum.values()).filter(s -> StringHelper.isNotEmpty(s.getSubclass()))
  63. .map(EslEventEnum::getSubclass).forEach(s -> client.addEventFilter("Event-Subclass", s));
  64. //扫描已注册的分机
  65. scanExten();
  66. } catch (Exception e) {
  67. log.error("fs reConnect Exception", e);
  68. try {
  69. client.close();
  70. } catch (Exception e1) {
  71. }
  72. }
  73. }
  74. }, 1, 5000, TimeUnit.MILLISECONDS);
  75. if (client.canSend()) {
  76. //删除所有坐席
  77. delAgentAll();
  78. //扫描已注册的分机
  79. scanExten();
  80. //设置中间件根目录
  81. String path=new File("").getAbsolutePath();
  82. client.sendAsyncApiCommand(EslCommandEnum.global_setvar.name(), "md_base_dir=" +path );
  83. }
  84. EslCommon.checkAuth();
  85. }
  86. @PreDestroy
  87. public void destroy() {
  88. if (EslCommon.agents.size() > 0) {
  89. for (Agent a : EslCommon.agents) {
  90. logout(a.getAgent(), a.getGroup(),"");
  91. a.setAgentState(EslAgentEnum.logout.ordinal());
  92. }
  93. EslCommon.agents=new ArrayList<>();
  94. }
  95. }
  96. //删除所有坐席
  97. public void delAgentAll() {
  98. try {
  99. String command = EslCommandEnum.callcenter_config.name();
  100. //获取坐席组中所有坐席
  101. EslMessage message = client.sendSyncApiCommand(command, "tier list");
  102. if (message != null && message.getBodyLines().size() > 0) {
  103. for (String line : message.getBodyLines()) {
  104. if (!line.startsWith("queue") && !line.startsWith("+OK")) {
  105. String[] lines = line.split("\\|");
  106. client.sendAsyncApiCommand(command, "agent del " + lines[1]);
  107. client.sendAsyncApiCommand(command, "tier del " + lines[0] + " " + lines[1]);
  108. }
  109. }
  110. }
  111. //获取所有坐席
  112. EslMessage message1 = client.sendSyncApiCommand(command, "agent list");
  113. if (message1 != null && message1.getBodyLines().size() > 0) {
  114. for (String line : message1.getBodyLines()) {
  115. if (!line.startsWith("name") && !line.startsWith("+OK")) {
  116. String[] lines = line.split("\\|");
  117. client.sendAsyncApiCommand(command, "agent del " + lines[0]);
  118. }
  119. }
  120. }
  121. } catch (Exception e) {
  122. log.error("删除所有坐席失败", e);
  123. }
  124. }
  125. //扫描已注册的分机
  126. public void scanExten() {
  127. try {
  128. //挂断所有
  129. client.sendSyncApiCommand("hupall", "");
  130. //获取已注册的分机
  131. EslMessage message = client.sendSyncApiCommand("sofia status profile internal reg", "");
  132. if (message != null && message.getBodyLines().size() > 0) {
  133. String ip = "";
  134. for (String line : message.getBodyLines()) {
  135. if (line.startsWith("Auth-User:")) {
  136. String exten = line.substring(10).trim();
  137. Channel chan = EslCommon.getChanByExten(exten);
  138. if (chan == null) {
  139. chan = new Channel();
  140. chan.setType(1);
  141. chan.setNumber(exten);
  142. EslCommon.channels.add(chan);
  143. }
  144. chan.setAnswer(false);
  145. chan.setChanId("");
  146. chan.setRecordPath("");
  147. chan.setCallType(0);
  148. chan.setSessionId("");
  149. chan.setRecordId(0);
  150. chan.setRingTime(null);
  151. chan.setSessionSort(0);
  152. chan.setIp(ip);
  153. }
  154. if (line.startsWith("IP:")) {
  155. ip = line.substring(3).trim();
  156. }
  157. }
  158. }
  159. } catch (Exception e) {
  160. log.error("扫描已注册的分机失败", e);
  161. }
  162. }
  163. //坐席签入
  164. public boolean login(String agent, String ext, String group,String action) {
  165. String result = "";
  166. try {
  167. String command = EslCommandEnum.callcenter_config.name();
  168. // 添加座席
  169. String arg = " agent add " + agent + " Callback";
  170. result = client.sendAsyncApiCommand(command, arg);
  171. // 设置呼叫字符串
  172. arg = " agent set contact " + agent + " [call_timeout=30]user/" + ext;
  173. client.sendAsyncApiCommand(command, arg);
  174. // 座席登录后默认空闲
  175. arg = " agent set status " + agent + " Available";
  176. client.sendAsyncApiCommand(command, arg);
  177. // 座席登录后默认空闲
  178. arg = " agent set state " + agent + " Waiting";
  179. client.sendAsyncApiCommand(command, arg);
  180. // 最大未接次数,到达次数后不再转接,0禁用
  181. arg = " agent set max_no_answer " + agent + " 0";
  182. client.sendAsyncApiCommand(command, arg);
  183. // 话后处理时间(s)
  184. //成功处理一个通话后,多久才会有电话进入的等待时长
  185. arg = " agent set wrap_up_time " + agent + " 20";
  186. client.sendAsyncApiCommand(command, arg);
  187. // 挂机间隔时间(s)
  188. //来电拒接后多久才会有电话进入的等待时长,0禁用
  189. arg = " agent set reject_delay_time " + agent + " 0";
  190. client.sendAsyncApiCommand(command, arg);
  191. // 忙重试间隔时间(s)
  192. //来电遇忙后多久才会有电话进入的等待时长
  193. arg = " agent set busy_delay_time " + agent + " 0";
  194. client.sendAsyncApiCommand(command, arg);
  195. // 添加梯队到队列等价于坐席组
  196. group = StringHelper.isEmpty(group) ? "ZXZ" : group;
  197. arg = " tier add " + group + " " + agent + " 1 1"
  198. +"\r\nJob-UUID:" + action + "|"+ System.currentTimeMillis();
  199. client.sendAsyncApiCommand(command, arg);
  200. } catch (Exception e) {
  201. log.error(agent + "|" + ext + "|" + group + " 签入失败", e);
  202. }
  203. return !result.equals("");
  204. }
  205. //坐席签出
  206. public boolean logout(String agent, String group,String action) {
  207. String result = "";
  208. try {
  209. String command = EslCommandEnum.callcenter_config.name();
  210. // 删除座席
  211. String arg = " agent del " + agent;
  212. result = client.sendAsyncApiCommand(command, arg);
  213. // 删除梯队
  214. group = StringHelper.isEmpty(group) ? "ZXZ" : group;
  215. arg = " tier del " + group + " " + agent
  216. +"\r\nJob-UUID:" + action + "|"+ System.currentTimeMillis();
  217. client.sendAsyncApiCommand(command, arg);
  218. } catch (Exception e) {
  219. log.error(agent + "|" + group + " 签出失败", e);
  220. }
  221. return !result.equals("");
  222. }
  223. //坐席置忙/置闲
  224. public boolean setWork(String agent, boolean isWork,String action) {
  225. String result = "";
  226. try {
  227. String command = EslCommandEnum.callcenter_config.name();
  228. String state = isWork ? "'Available'" : "'On Break'";
  229. String arg = " agent set status " + agent + " " + state
  230. +"\r\nJob-UUID:" + action + "|"+ System.currentTimeMillis();
  231. result = client.sendAsyncApiCommand(command, arg);
  232. } catch (Exception e) {
  233. String state = isWork ? "置闲" : "置忙";
  234. log.error(agent + "|" + isWork + " " + state + "失败", e);
  235. }
  236. return !result.equals("");
  237. }
  238. //删除
  239. public boolean kill(String chanId,String action) {
  240. String result = "";
  241. try {
  242. result = client.sendAsyncApiCommand(EslCommandEnum.uuid_kill.name(), chanId
  243. + "\r\nJob-UUID: " + action + "|" + System.currentTimeMillis());
  244. } catch (Exception e) {
  245. log.error(chanId + " 删除失败", e);
  246. }
  247. return !result.equals("");
  248. }
  249. //app挂断
  250. public boolean killApp(String chanId) {
  251. String result = "";
  252. try {
  253. String command = EslCommandEnum.uuid_broadcast.name();
  254. String arg = chanId + " hangup::NORMAL_CLEARING" ;
  255. result = client.sendAsyncApiCommand(command, arg);
  256. } catch (Exception e) {
  257. log.error( chanId + " app挂断失败", e);
  258. }
  259. return !result.equals("");
  260. }
  261. //分机呼叫
  262. public boolean extenCall(String callerNum, String calleeNum,String action) {
  263. String result = "";
  264. try {
  265. String fix = action.substring(action.lastIndexOf("|") + 1);
  266. String command = EslCommandEnum.originate.name();
  267. // String arg = " {instant_ringback=true,origination_caller_id_number=" + callerNum
  268. // + ",call_called=" + calleeNum.substring(fix.length()) + ",record_concat_video=true"
  269. // + ",transfer_ringback=local_stream://moh,ignore_early_media=true"
  270. // + "}user/" + callerNum + " &bridge(" + getCallString(calleeNum) + ")"
  271. // + "\r\nJob-UUID: " + action + "|" + System.currentTimeMillis();
  272. String arg = " {origination_caller_id_number=" + callerNum + ",call_called=" + calleeNum.substring(fix.length())
  273. + ",record_concat_video=true,transfer_ringback=local_stream://moh,ringback=${us-ring}"
  274. + "}user/" + callerNum + " &bridge(" + getCallString(calleeNum) + ")"
  275. + "\r\nJob-UUID: " + action + "|" + System.currentTimeMillis();
  276. result = client.sendAsyncApiCommand(command, arg);
  277. } catch (Exception e) {
  278. log.error(callerNum + "|" + calleeNum + " 分机呼叫失败", e);
  279. }
  280. return !result.equals("");
  281. }
  282. //呼叫2个号码并桥接
  283. public boolean bridgeNum(String callerNum, String calleeNum,String action) {
  284. String result = "";
  285. try {
  286. String command = EslCommandEnum.originate.name();
  287. String arg = " {origination_caller_id_number=" + callerNum + ",call_called=" + calleeNum
  288. + ",call_type=2}" + getCallString(callerNum) + " &bridge(" + getCallString(calleeNum) + ")"
  289. + "\r\nJob-UUID: " + action + "|" + System.currentTimeMillis();
  290. result = client.sendAsyncApiCommand(command, arg);
  291. } catch (Exception e) {
  292. log.error(callerNum + "|" + calleeNum + " 呼叫号码并桥接失败", e);
  293. }
  294. return !result.equals("");
  295. }
  296. //协商呼叫
  297. public boolean consult(String chanId, String callerNum,String calleeNum,String action) {
  298. String result = "";
  299. try {
  300. String fix = action.substring(action.lastIndexOf("|")+1);
  301. String command = EslCommandEnum.uuid_broadcast.name();
  302. String arg = chanId + " att_xfer::{origination_caller_id_number=" + callerNum+",record_concat_video=true"
  303. + ",call_called=" + calleeNum.substring(fix.length()) + "}" + getCallString(calleeNum)
  304. + "\r\nJob-UUID: " + action + "|" + System.currentTimeMillis();
  305. result = client.sendAsyncApiCommand(command, arg);
  306. } catch (Exception e) {
  307. log.error(chanId + "|" + calleeNum + " 协商呼叫失败", e);
  308. }
  309. return !result.equals("");
  310. }
  311. //强插
  312. public boolean insert(String callerNum,String calleeNum, String sessionId,String action) {
  313. String result = "";
  314. try {
  315. String command = EslCommandEnum.originate.name();
  316. String arg = " {origination_caller_id_number=" + callerNum + ",cc_member_session_uuid=" + sessionId
  317. + ",call_called=" + calleeNum + "}user/" + callerNum + " &three_way(" + sessionId + ")"
  318. + "\r\nJob-UUID: " + action + "|" + System.currentTimeMillis();
  319. result = client.sendAsyncApiCommand(command, arg);
  320. } catch (Exception e) {
  321. log.error(callerNum + "|" + calleeNum + "|" + sessionId + " 强插失败", e);
  322. }
  323. return !result.equals("");
  324. }
  325. //强截
  326. public boolean intercept(String callerNum,String calleeNum, String chanId,String sessionId,String action) {
  327. String result = "";
  328. try {
  329. Session session = EslCommon.getSessionById(sessionId);
  330. String command = EslCommandEnum.originate.name();
  331. String arg = " {origination_caller_id_number=" + callerNum + ",cc_member_session_uuid=" + sessionId
  332. + ",call_called=" + calleeNum + "}user/" + callerNum + " &intercept(" + chanId + ")"
  333. + "\r\nJob-UUID: " + action + "|" + System.currentTimeMillis();
  334. result = client.sendAsyncApiCommand(command, arg);
  335. } catch (Exception e) {
  336. log.error(callerNum + "|" + calleeNum + "|" + chanId + "|" + sessionId + " 强截失败", e);
  337. }
  338. return !result.equals("");
  339. }
  340. //监听
  341. public boolean listen(String callerNum,String calleeNum, String sessionId,String action) {
  342. String result = "";
  343. try {
  344. String command = EslCommandEnum.originate.name();
  345. String arg = " {origination_caller_id_number=" + callerNum+ ",cc_member_session_uuid=" + sessionId
  346. + ",call_called=" + calleeNum+ "}user/" + callerNum + " &eavesdrop(" + sessionId + ")"
  347. + "\r\nJob-UUID: " + action + "|" + System.currentTimeMillis();
  348. result = client.sendAsyncApiCommand(command, arg);
  349. } catch (Exception e) {
  350. log.error(callerNum + "|" + sessionId + " 监听失败", e);
  351. }
  352. return !result.equals("");
  353. }
  354. //播放排队位置
  355. public boolean playPosition(int num, String chanId) {
  356. String result = "";
  357. try {
  358. String command = EslCommandEnum.uuid_broadcast.name();
  359. //String path = "/home/wav/" + num + ".mp3";
  360. String path = TtsHelper.TextToSpeechPosition("你当前的位置为" + num, num + "");
  361. String arg = chanId + " playback::" + path;
  362. result = client.sendAsyncApiCommand(command, arg);
  363. } catch (Exception e) {
  364. log.error(num + "|" + chanId + " 播放排队位置失败", e);
  365. }
  366. return !result.equals("");
  367. }
  368. //播放坐席工号
  369. public boolean playAgent(String agent, String chanId) {
  370. String result = "";
  371. try {
  372. // String command = EslCommandEnum.sched_broadcast.name();
  373. // String path = "/home/wav/8001.wav";
  374. // //1秒后播放坐席工号
  375. // String arg = " +1 " + chanId + " playback::" + path + " both";
  376. String command = EslCommandEnum.uuid_broadcast.name();
  377. //String path = "/home/wav/8001.wav";
  378. String at = agent;
  379. try {
  380. Integer.parseInt(agent);
  381. at = agent.replaceAll("(.)", " $1").substring(1);
  382. } catch (Exception ex) { }
  383. String path = TtsHelper.TextToSpeechAgent("你好," + at + "号话务员为您服务", agent);
  384. String arg = chanId + " playback::" + path + " both";
  385. result = client.sendAsyncApiCommand(command, arg);
  386. } catch (Exception e) {
  387. log.error(chanId + " 播放坐席工号失败", e);
  388. }
  389. return !result.equals("");
  390. }
  391. //播放语音
  392. public boolean playBack(String path, String chanId) {
  393. String result = "";
  394. try {
  395. String command = EslCommandEnum.uuid_broadcast.name();
  396. //path = "/home/wav/8001.wav";
  397. String arg = chanId + " playback::" + path;
  398. result = client.sendAsyncApiCommand(command, arg);
  399. } catch (Exception e) {
  400. log.error(path + "|" + chanId + " 播放语音失败", e);
  401. }
  402. return !result.equals("");
  403. }
  404. //播放语音并挂断
  405. public boolean playHangup(String path, String chanId) {
  406. String result = "";
  407. try {
  408. String command = EslCommandEnum.uuid_broadcast.name();
  409. //path = "/home/wav/8001.wav";
  410. String arg = chanId + " playback!::" + path;
  411. result = client.sendAsyncApiCommand(command, arg);
  412. } catch (Exception e) {
  413. log.error(path + "|" + chanId + " 播放语音并挂断失败", e);
  414. }
  415. return !result.equals("");
  416. }
  417. //放音收号
  418. public boolean playAndGetDigits(String path,String var, String chanId) {
  419. String result = "";
  420. try {
  421. String command = EslCommandEnum.uuid_broadcast.name();
  422. //path = "/home/wav/8001.wav";
  423. String arg = chanId + " 'play_and_get_digits::1 1 2 2000 # " + path + " silence_stream://250 " + var + " \\d+'";
  424. result = client.sendAsyncApiCommand(command, arg);
  425. } catch (Exception e) {
  426. log.error(path + "|" + var + "|" + chanId + " 放音收号失败", e);
  427. }
  428. return !result.equals("");
  429. }
  430. //放音识别
  431. public boolean playAndDetectSpeech(String path,String var, String chanId) {
  432. String result = "";
  433. try {
  434. String command = EslCommandEnum.uuid_broadcast.name();
  435. //path = "/home/wav/8001.wav";
  436. String arg = chanId + " 'play_and_detect_speech::" + path + " detect:unimrcp:mrcpv2 "
  437. + "{start-input-timers=false,no-input-timeout=5000,define-grammar=false," + var + "=0}hello'";
  438. result = client.sendAsyncApiCommand(command, arg);
  439. } catch (Exception e) {
  440. log.error(path + "|" + var + "|" + chanId + " 放音识别失败", e);
  441. }
  442. return !result.equals("");
  443. }
  444. //会话加入会议
  445. public boolean talkJoinMeeting(String sessionId) {
  446. String result = "";
  447. try {
  448. String command = EslCommandEnum.uuid_transfer.name();
  449. //String arg = sessionId + " -both " + sessionId + " xml ExtenMeeting";
  450. String at = "threeway";
  451. Session session = EslCommon.getSessionById(sessionId);
  452. if (session != null && session.isVideo()) at = "video-mcu-stereo";
  453. String arg = sessionId + " -both 'set:hangup_after_bridge=false,set:record_concat_video=true,"
  454. + "conference:" + sessionId + "@" + at + "' inline";
  455. result = client.sendAsyncApiCommand(command, arg);
  456. if (!result.equals("") && session != null) session.setMeeting(true);
  457. } catch (Exception e) {
  458. log.error(sessionId + " 会话加入会议失败", e);
  459. }
  460. return !result.equals("");
  461. }
  462. //呼叫号码加入会议
  463. public boolean callJoinMeeting(String callerNum, String calleeNum, String meetingId,String action) {
  464. String result = "";
  465. try {
  466. String fix = action.substring(action.lastIndexOf("|") + 1);
  467. String command = EslCommandEnum.originate.name();
  468. String at = "threeway", argstr = "";
  469. Session session = EslCommon.getSessionById(meetingId);
  470. if (session != null && session.isVideo()) {
  471. at = "video-mcu-stereo";
  472. String parentPath = "files/video/meeting/" + new SimpleDateFormat("yyyyMMdd").format(new Date());
  473. String path = new File(parentPath).getAbsolutePath() + "/" + meetingId + ".mp4";
  474. session.setVideoPath(parentPath + "/" + meetingId + ".mp4");
  475. argstr = ",record_concat_video=true,conference_auto_record=" + path;
  476. }
  477. String arg = " {origination_caller_id_number=" + callerNum + ",cc_member_session_uuid=" + meetingId
  478. + ",call_called=" + calleeNum.substring(fix.length())
  479. + ",transfer_ringback=local_stream://moh,ringback=${us-ring}"
  480. + argstr + "}" + getCallString(calleeNum)
  481. // + " " + meetingId + " xml ExtenMeeting";
  482. + " &conference(" + meetingId + "@" + at + ")"
  483. + "\r\nJob-UUID: " + action + "|" + System.currentTimeMillis();
  484. result = client.sendAsyncApiCommand(command, arg);
  485. } catch (Exception e) {
  486. log.error(callerNum + "|" + calleeNum + "|" + meetingId + " 呼叫加入会议失败", e);
  487. }
  488. return !result.equals("");
  489. }
  490. //开启/关闭静音
  491. public boolean setMute(String chanId, boolean isMute,String action) {
  492. String result = "";
  493. try {
  494. String command = EslCommandEnum.uuid_audio.name();
  495. String arg = chanId + (isMute ? " start" : " stop") + " write mute -4"
  496. +"\r\nJob-UUID: " + action + "|" + System.currentTimeMillis();
  497. result = client.sendAsyncApiCommand(command, arg);
  498. } catch (Exception e) {
  499. String state = isMute ? "开启" : "关闭";
  500. log.error(chanId + " " + state + "静音失败", e);
  501. }
  502. return !result.equals("");
  503. }
  504. //开启/关闭保持
  505. public boolean setHold(String chanId, boolean isHold,String action) {
  506. String result = "";
  507. try {
  508. String command = EslCommandEnum.uuid_hold.name();
  509. String arg = (isHold ? " " : " off") + " " + chanId
  510. + "\r\nJob-UUID: " + action + "|" + System.currentTimeMillis();
  511. result = client.sendAsyncApiCommand(command, arg);
  512. } catch (Exception e) {
  513. String state = isHold ? "开启" : "关闭";
  514. log.error(chanId + " " + state + "保持失败", e);
  515. }
  516. return !result.equals("");
  517. }
  518. //录音
  519. public boolean record(String chanId, String filePath) {
  520. String result = "";
  521. try {
  522. String command = EslCommandEnum.uuid_record.name();
  523. String arg = chanId + " start " + filePath;
  524. result = client.sendAsyncApiCommand(command, arg);
  525. } catch (Exception e) {
  526. log.error(chanId + "|" + filePath + " 录音失败", e);
  527. }
  528. return !result.equals("");
  529. }
  530. //录音停止
  531. public boolean recordStop(String chanId, String filePath) {
  532. String result = "";
  533. try {
  534. String command = EslCommandEnum.uuid_record.name();
  535. String arg = chanId + " stop " + filePath;
  536. result = client.sendAsyncApiCommand(command, arg);
  537. } catch (Exception e) {
  538. log.error(chanId + "|" + filePath + " 录音停止失败", e);
  539. }
  540. return !result.equals("");
  541. }
  542. //转满意度
  543. public boolean turnMyd(String chanId,String action) {
  544. String result = "";
  545. try {
  546. String command = EslCommandEnum.uuid_transfer.name();
  547. //String arg = chanId + " turnmyd xml ForExten";
  548. String arg = chanId + " 'set:hangup_after_bridge=false,ivr:myd' inline"
  549. +"\r\nJob-UUID: " + action + "|" + System.currentTimeMillis();
  550. result = client.sendAsyncApiCommand(command, arg);
  551. } catch (Exception e) {
  552. log.error(chanId + " 转满意度失败", e);
  553. }
  554. return !result.equals("");
  555. }
  556. //转移号码
  557. public boolean transfer(String chanId,String callerNum, String calleeNum,String action) {
  558. String result = "";
  559. try {
  560. String fix = action.substring(action.lastIndexOf("|") + 1);
  561. String command = EslCommandEnum.uuid_transfer.name();
  562. //String arg = chanId + " " + calleeNum + " xml ForExten";
  563. // String arg = chanId + " 'm:^:set:hangup_after_bridge=false^set:record_concat_video=true^"
  564. // + "set:instant_ringback=true^set:transfer_ringback=local_stream://moh^"
  565. // + "bridge:{ignore_early_media=true,origination_caller_id_number=" + callerNum + ",call_called="
  566. // + calleeNum.substring(fix.length()) + "}" + getCallString(calleeNum) + "' inline"
  567. // + "\r\nJob-UUID: " + action + "|" + System.currentTimeMillis();
  568. String arg = chanId + " 'm:^:set:hangup_after_bridge=false^set:record_concat_video=true^"
  569. + "set:transfer_ringback=local_stream://moh^set:ringback=${us-ring}^"
  570. + "bridge:{origination_caller_id_number=" + callerNum + ",call_called="
  571. + calleeNum.substring(fix.length()) + "}" + getCallString(calleeNum) + "' inline"
  572. + "\r\nJob-UUID: " + action + "|" + System.currentTimeMillis();
  573. result = client.sendAsyncApiCommand(command, arg);
  574. } catch (Exception e) {
  575. log.error(chanId + "|" + calleeNum + " 转移失败", e);
  576. }
  577. return !result.equals("");
  578. }
  579. //发送按键
  580. public boolean sendDtmf(String chanId, String dtmf,String action) {
  581. String result = "";
  582. try {
  583. String command = EslCommandEnum.uuid_send_dtmf.name();
  584. String arg = chanId + " " + dtmf
  585. +"\r\nJob-UUID: " + action + "|" + System.currentTimeMillis();
  586. result = client.sendAsyncApiCommand(command, arg);
  587. } catch (Exception e) {
  588. log.error(chanId + "|" + dtmf + " 发送按键失败", e);
  589. }
  590. return !result.equals("");
  591. }
  592. //设置会议人员离开是否播放声音
  593. public boolean setConferenceExitSound(String meetingId, boolean isplay) {
  594. String result = "";
  595. try {
  596. String command = EslCommandEnum.conference.name();
  597. String arg = meetingId + " exit_sound " + (isplay ? "on" : "off");
  598. result = client.sendAsyncApiCommand(command, arg);
  599. } catch (Exception e) {
  600. log.error(meetingId + "|" + isplay + " 设置会议离开失败", e);
  601. }
  602. return !result.equals("");
  603. }
  604. //设置会议人员是否静音
  605. public boolean setConferenceIsMute(String meetingId, String memberId, boolean isMute,String action) {
  606. String result = "";
  607. try {
  608. String command = EslCommandEnum.conference.name();
  609. if (isMute) {
  610. String arg = meetingId + " deaf " + memberId;
  611. client.sendAsyncApiCommand(command, arg);
  612. arg = meetingId + " mute " + memberId
  613. +"\r\nJob-UUID: " + action + "|" + System.currentTimeMillis();
  614. result = client.sendAsyncApiCommand(command, arg);
  615. } else {
  616. String arg = meetingId + " undeaf " + memberId;
  617. client.sendAsyncApiCommand(command, arg);
  618. arg = meetingId + " stop all " + memberId;
  619. client.sendAsyncApiCommand(command, arg);
  620. arg = meetingId + " unmute " + memberId
  621. +"\r\nJob-UUID: " + action + "|" + System.currentTimeMillis();
  622. result = client.sendAsyncApiCommand(command, arg);
  623. }
  624. } catch (Exception e) {
  625. log.error(meetingId + "|" + memberId + "|" + isMute + " 设置会议静音失败", e);
  626. }
  627. return !result.equals("");
  628. }
  629. //自动外呼
  630. public boolean autoCall(String callerNum, String calleeNum,String taskId,String phoneId) {
  631. String result = "";
  632. try {
  633. String command = EslCommandEnum.originate.name();
  634. String arg = " {origination_caller_id_number=" + callerNum + ",task_id=" + taskId + ",phone_id=" + phoneId + ",call_type=3}"
  635. + getCallString(calleeNum) + " &park()";
  636. result = client.sendAsyncApiCommand(command, arg);
  637. } catch (Exception e) {
  638. log.error(callerNum + "|" + calleeNum + " 自动外呼失败", e);
  639. }
  640. return !result.equals("");
  641. }
  642. //api命令
  643. public boolean apiCommand(String command, String arg) {
  644. String result = "";
  645. try {
  646. result = client.sendAsyncApiCommand(command, arg);
  647. } catch (Exception e) {
  648. log.error(command + "|" + arg + " api命令失败", e);
  649. }
  650. return !result.equals("");
  651. }
  652. public boolean appCommand(String chanId,String command, String arg) {
  653. try {
  654. SendMsg sm=new SendMsg(chanId);
  655. sm.addCallCommand("execute");
  656. sm.addExecuteAppName(command);
  657. sm.addExecuteAppArg(arg);
  658. return client.sendMessage(sm).isOk();
  659. } catch (Exception e) {
  660. log.error(command + "|" + arg + " app命令失败", e);
  661. }
  662. return false;
  663. }
  664. //呼叫字符串
  665. private String getCallString(String calleeNum){
  666. //呼叫内线分机号码
  667. String callStr = "user/" + calleeNum;
  668. //呼叫外线号码
  669. if (!EslCommon.existExten(calleeNum)) {
  670. callStr = "sofia/gateway/hykj/" + calleeNum;
  671. }
  672. return callStr;
  673. }
  674. }