xuhy
7 天以前 640d93c464c65a0ef128f7f357a3e9abe44fbd2c
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
package com.ruoyi.admin.voice.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.admin.voice.service.IAXBInterfaceDemo;
import com.ruoyi.admin.voice.util.HttpUtilClient;
import com.ruoyi.common.core.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * AXB模式接口测试
 */
@Slf4j
public class AXBInterfaceDemoImpl implements IAXBInterfaceDemo {
 
    private String appKey; // APP_Key
    private String appSecret; // APP_Secret
    private String ompDomainName; // APP接入地址
 
    public AXBInterfaceDemoImpl(String appKey, String appSecret, String ompDomainName) {
        this.appKey = appKey;
        this.appSecret = appSecret;
        this.ompDomainName = ompDomainName;
    }
 
    /**
     * Build the real url of https request | 构建隐私保护通话平台请求路径
     * 
     * @param path 接口访问URI
     * @return
     */
    private String buildOmpUrl(String path) {
        return ompDomainName + path;
    }
 
    @Override
    public String axbBindNumber(String relationNum, String callerNum, String calleeNum) {
        if (StringUtils.isBlank(relationNum) || StringUtils.isBlank(callerNum) || StringUtils.isBlank(calleeNum)) {
            log.info("axbBindNumber set params error");
            throw new ServiceException("axbBindNumber set params error");
        }
 
        // 必填,AXB模式绑定接口访问URI
        String url = "/rest/caas/relationnumber/partners/v1.0";
        String realUrl = buildOmpUrl(url);
 
        // 封装JOSN请求
        JSONObject json = new JSONObject();
        json.put("relationNum", relationNum); // X号码(关系号码)
        json.put("callerNum", callerNum); // A方真实号码(手机或固话)
        json.put("calleeNum", calleeNum); // B方真实号码(手机或固话)
 
        /**
         * 选填,各参数要求请参考"AXB模式绑定接口"
         */
//         json.put("areaCode", "0755"); //城市码
//         json.put("areaMatchMode", "1"); //号码筛选方式
//         json.put("callDirection", 0); //允许呼叫的方向
//         json.put("duration", 86400); //绑定关系保持时间
//         json.put("recordFlag", false); //是否通话录音
//         json.put("recordHintTone", "recordHintTone.wav"); //录音提示音
//         json.put("maxDuration", 60); //单次通话最长时间
//         json.put("lastMinVoice", "lastMinVoice.wav"); //通话最后一分钟提示音
//         json.put("privateSms", true); //是否支持短信功能
//         JSONObject preVoice = new JSONObject();
//         preVoice.put("callerHintTone", "callerHintTone.wav"); //设置A拨打X号码时的通话前等待音
//         preVoice.put("calleeHintTone", "calleeHintTone.wav"); //设置B拨打X号码时的通话前等待音
//         json.put("preVoice", preVoice); //个性化通话前等待音
 
        String result = HttpUtilClient.sendPost(appKey, appSecret, realUrl, json.toString());
        log.info("Response is :" + result);
        return result;
    }
 
    @Override
    public void axbModifyNumber(String subscriptionId, String callerNum, String calleeNum) {
        if (StringUtils.isBlank(subscriptionId)) {
            log.info("axbModifyNumber set params error");
            return;
        }
 
        // 必填,AXB模式绑定信息修改接口访问URI
        String url = "/rest/caas/relationnumber/partners/v1.0";
        String realUrl = buildOmpUrl(url);
 
        // 封装JOSN请求
        JSONObject json = new JSONObject();
        json.put("subscriptionId", subscriptionId); // 绑定关系ID
        if (StringUtils.isNotBlank(callerNum)) {
            json.put("callerNum", callerNum); // 将A方修改为新的号码(手机或固话)
        }
        if (StringUtils.isNotBlank(calleeNum)) {
            json.put("calleeNum", calleeNum); // 将B方修改为新的号码(手机或固话)
        }
 
        /**
         * 选填,各参数要求请参考"AXB模式绑定信息修改接口"
         */
//         json.put("callDirection", 0); //允许呼叫的方向
//         json.put("duration", 86400); //绑定关系保持时间
//         json.put("maxDuration", 90); //单次通话最长时间
//         json.put("lastMinVoice", "lastMinVoice.wav"); //通话最后一分钟提示音
//         json.put("privateSms", true); //是否支持短信功能
//         json.put("recordFlag", false); //是否通话录音
//         JSONObject preVoice = new JSONObject();
//         preVoice.put("callerHintTone", "callerHintTone.wav"); //设置A拨打X号码时的通话前等待音
//         preVoice.put("calleeHintTone", "calleeHintTone.wav"); //设置B拨打X号码时的通话前等待音
//         json.put("preVoice", preVoice); //个性化通话前等待音
 
        String result = HttpUtilClient.sendPut(appKey, appSecret, realUrl, json.toString());
        log.info("Response is :" + result);
    }
 
    @Override
    public void axbUnbindNumber(String subscriptionId, String relationNum) {
        if (StringUtils.isBlank(subscriptionId) && StringUtils.isBlank(relationNum)) {
            log.info("axbUnbindNumber set params error");
            return;
        }
 
        // 必填,AXB模式解绑接口访问URI
        String url = "/rest/caas/relationnumber/partners/v1.0";
        String realUrl = buildOmpUrl(url);
 
        // 申明对象
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(subscriptionId)) {
            map.put("subscriptionId", subscriptionId); // 绑定关系ID
        } else {
            map.put("relationNum", relationNum); // X号码(关系号码)
        }
 
        String result = HttpUtilClient.sendDelete(appKey, appSecret, realUrl, HttpUtilClient.map2UrlEncodeString(map));
        log.info("Response is :" + result);
    }
 
    @Override
    public void axbQueryBindRelation(String subscriptionId, String relationNum) {
        if (StringUtils.isBlank(subscriptionId) && StringUtils.isBlank(relationNum)) {
            log.info("axbQueryBindRelation set params error");
            return;
        }
 
        // 必填,AXB模式绑定信息查询接口访问URI
        String url = "/rest/caas/relationnumber/partners/v1.0";
        String realUrl = buildOmpUrl(url);
 
        // 申明对象
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(subscriptionId)) {
            map.put("subscriptionId", subscriptionId); // 绑定关系ID
        } else {
            map.put("relationNum", relationNum); // X号码(关系号码)
            
            /**
             * 选填,各参数要求请参考"AXB模式绑定信息查询接口"
             */
//            map.put("pageIndex", 1); //查询的分页索引,从1开始编号
//            map.put("pageSize", 20); //查询的分页大小,即每次查询返回多少条数据
        }
 
        String result = HttpUtilClient.sendGet(appKey, appSecret, realUrl, HttpUtilClient.map2UrlEncodeString(map));
        log.info("Response is :" + result);
    }
 
    @Override
    public String axbGetRecordDownloadLink(String recordDomain, String fileName) {
        if (StringUtils.isBlank(recordDomain) || StringUtils.isBlank(fileName)) {
            log.info("axbGetRecordDownloadLink set params error");
            throw new ServiceException("axbGetRecordDownloadLink set params error");
        }
        // 必填,AXB模式获取录音文件下载地址接口访问URI
        String url = "/rest/provision/voice/record/v1.0";
        String realUrl = buildOmpUrl(url);
 
        // 申明对象
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("recordDomain", recordDomain); // 录音文件存储的服务器域名
        map.put("fileName", fileName); // 录音文件名
 
        String result = HttpUtilClient.sendGet(appKey, appSecret, realUrl, HttpUtilClient.map2UrlEncodeString(map));
        log.info("Response is :" + result);
        return result;
    }
 
    @Override
    public void axbStopCall(String sessionid) {
        if (StringUtils.isBlank(sessionid)) {
            log.info("axbStopCall set params error");
            return;
        }
 
        // 必填,AXB模式终止呼叫接口访问URI
        String url = "/rest/httpsessions/callStop/v2.0";
        String realUrl = buildOmpUrl(url);
 
        // 封装JOSN请求
        JSONObject json = new JSONObject();
        json.put("sessionid", sessionid); // 呼叫会话ID
        json.put("signal", "call_stop"); // 取值固定为"call_stop"
 
        String result = HttpUtilClient.sendPost(appKey, appSecret, realUrl, json.toString());
        log.info("Response is :" + result);
    }
}