liusheng
2025-02-27 e00ef99886b9ab84f39c81432f8c7640e93026f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package com.smartor.config;
 
import com.ruoyi.common.utils.HttpUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpEntity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import javax.mail.Multipart;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
 
@Component
public class PhoneUtils {
 
    @Value("${phoneIP}")
    private String phoneIP;
 
    @Value("${phonePort}")
    private String phonePort;
 
    /**
     * 添加分机add_user
     */
    public String addUser(String user, String pwd) {
        Map<String, Object> map = new HashMap<>();
        map.put("user", user);
        map.put("password", pwd);
        return sendReq(map, "/tel/ai_api/add_user");
    }
 
    /**
     * 删除分机delete_user
     *
     * @return
     */
    public String delUser(String user) {
        Map<String, Object> map = new HashMap<>();
        map.put("user", user);
        return sendReq(map, "/tel/ai_api/delete_user");
    }
 
    /**
     * 3、检查分机是否存在check_user
     *
     * @return
     */
    public String checkUser(String user) {
        Map<String, Object> map = new HashMap<>();
        map.put("user", user);
        return sendReq(map, "/tel/ai_api/check_user");
    }
 
 
    /**
     * 列出全部坐席或者指定坐席 list_agent
     *
     * @param name
     * @return
     */
    public String listAgent(String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        return sendReq(map, "/tel/ai_api/list_agent");
    }
 
    /**
     * 添加坐席add_agent
     * 向系统中添加一个坐席,后续需要set_agent_state,set_agent_status,set_agent_contact, tier_add 设置坐席状体,sip_uri和把坐席加入/绑定一个呼叫队列
     *
     * @param name
     * @return
     */
    public String addAgent(String name, String type) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        if (StringUtils.isEmpty(type)) map.put("type", "callback");
 
        return sendReq(map, "/tel/ai_api/add_agent");
    }
 
    /**
     * 删除坐席del_agent
     * 从呼叫队列删除坐席
     *
     * @param name
     * @return
     */
    public String delAgent(String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        return sendReq(map, "/tel/ai_api/del_agent");
    }
 
    /**
     * 获取坐席当前所在通话的uuid
     *
     * @param name
     * @return
     */
    public String getAgentUUID(String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        return sendReq(map, "/tel/ai_api/get_agent_uuid");
    }
 
    /**
     * 添加坐席后(add_agent),需要设置坐席地址(sip uri),坐席才可以被访问到
     *
     * @param name
     * @param contact
     * @return
     */
    public String setAgentUUID(String name, String contact) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("contact", contact);
        return sendReq(map, "/tel/ai_api/set_agent_contact");
    }
 
    /**
     * 设置坐席的status,有固定几个状态
     *
     * @param name
     * @param status
     * @return
     */
    public String setAgentStatus(String name, String status) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("status", status);
        return sendReq(map, "/tel/ai_api/set_agent_status");
    }
 
    /**
     * 获取坐席状态
     *
     * @param name
     * @return
     */
    public String getAgentStatus(String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        return sendReq(map, "/tel/ai_api/get_agent_status");
    }
 
 
    /**
     * 设设置坐席
     *
     * @param name
     * @param state
     * @return
     */
    public String setAgentState(String name, String state) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("state", state);
        return sendReq(map, "/tel/ai_api/set_agent_state");
    }
 
    /**
     * 获取坐席状态
     *
     * @param name
     * @return
     */
    public String getAgentState(String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        return sendReq(map, "/tel/ai_api/get_agent_state");
    }
 
    /**
     * 手动外呼
     *
     * @return
     */
    public String manualOutbound(String kg_uuid, String kg_file, String data, String app_id, String ani, String special_ch, String sign, String extension, String dnis, Boolean force_call) {
        Map<String, Object> map = new HashMap<>();
        map.put("kg_uuid", kg_uuid);
        map.put("kg_file", kg_file);
        map.put("data", data);
        map.put("app_id", app_id);
        map.put("ani", ani);
        map.put("special_ch", special_ch);
        map.put("sign", sign);
        map.put("extension", extension);
        map.put("dnis", dnis);
        map.put("force_call", force_call);
        if (force_call == null) map.put("force_call", true);
        return sendReq(map, "/tel/ai_api/manual_outbound");
    }
 
    /**
     * tts合成和播放
     *
     * @param fileText
     * @param uuid
     * @return
     */
    public String ttsPlayback(String fileText, String uuid) {
        System.out.println("=====================问题内容:" + fileText + "UUID ; " + uuid);
        Map<String, Object> map = new HashMap<>();
        map.put("text", fileText);
        map.put("uuid", uuid);
 
        return sendReq(map, "/tel/ai_api/tts_playback");
    }
 
    /**
     * wav文件播放接口
     *
     * @param wav_file
     * @param uuid
     * @return
     */
    public String manualOutbound(Multipart wav_file, String uuid) {
        Map<String, Object> map = new HashMap<>();
        map.put("wav_file", wav_file);
        map.put("uuid", uuid);
 
        return sendReq(map, "/tel/ai_api/playback");
    }
 
    /**
     * 机器人外呼 ob
     *
     * @return
     */
    public String ob(String kg_uuid, String kg_file, String data, String app_id, String ani, String special_ch, String sign, String dnis, String call_uuid, Boolean force_call) {
        Map<String, Object> map = new HashMap<>();
        map.put("kg_uuid", kg_uuid);
        map.put("kg_file", kg_file);
        map.put("data", data);
        map.put("app_id", app_id);
        map.put("ani", ani);
        map.put("special_ch", special_ch);
        map.put("sign", sign);
        map.put("dnis", dnis);
        map.put("call_uuid", call_uuid);
        map.put("force_call", force_call);
        map.put("fs_node_id", 8021);
        if (force_call == null) map.put("force_call", true);
        return sendReq(map, "/tel/ai_api/outbound");
    }
 
    /**
     * 挂断通话
     *
     * @return
     */
    public String hangup(String kg_uuid, String dnis, String data, String app_id, String ani, String special_ch, String sign, String call_uuid) {
        Map<String, Object> map = new HashMap<>();
        map.put("kg_uuid", kg_uuid);
        map.put("kg_file", kg_uuid);
        map.put("dnis", dnis);
        map.put("data", data);
        map.put("app_id", app_id);
        map.put("ani", ani);
        map.put("special_ch", special_ch);
        map.put("sign", sign);
        map.put("call_uuid", call_uuid);
        map.put("fs_node_id", "");
 
 
//        map.put("app_id", app_id);
//        map.put("kg_uuid", kg_uuid);
//        map.put("ani", ani);
//        map.put("dnis", dnis);
//        map.put("call_direction", call_direction_str);
//        map.put("hangup_cause", hangup_cause);
//        map.put("endpoint_disposition", endpoint_disposition);
//        map.put("wait_msec", wait_msec);
//        map.put("duration_msec", duration_msec);
//        map.put("answer_msec", answer_msec);
//        map.put("bill_msec", bill_msec);
//        map.put("start_stamp", start_stamp);
//        map.put("answered_stamp", answered_stamp);
//        map.put("end_stamp", end_stamp);
//        map.put("vad_total_talk_time_msec", vad_total_talk_time_msec);
//        map.put("time_stamp", time_stamp);
        return sendReq(map, "/tel/ai_api/hangup");
    }
 
    private String sendReq(Map<String, Object> map, String path) {
        HttpEntity<Map<String, Object>> req = new HttpEntity<>(getHead(), map);
        if (StringUtils.isEmpty(phoneIP)) {
            phoneIP = "http://124.220.50.51";
            phonePort = "8001";
        }
        HttpEntity<String> stringHttpEntity = HttpUtil.postJsonRequestV2(phoneIP + ":" + phonePort + path, req, String.class);
        String responseBody = null;
        try {
            responseBody = new String(stringHttpEntity.getBody().getBytes("ISO-8859-1"), "UTF-8");
            System.out.println("电话信息返回 : " + responseBody);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return new String(stringHttpEntity.getBody().getBytes(StandardCharsets.UTF_8));
    }
 
    private Map<String, String> getHead() {
        HashMap<String, String> header = new HashMap<>();
        header.put("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko");
        header.put("Content-Type", "application/json");
        return header;
    }
}