fix: 修复阿里云语音通知可能提示参数过长问题 (#433)
This commit is contained in:
parent
807f1629ca
commit
a46c4fd07c
|
|
@ -17,6 +17,7 @@ import org.jetlinks.community.notify.*;
|
|||
import org.jetlinks.community.notify.template.TemplateManager;
|
||||
import org.jetlinks.community.notify.voice.VoiceProvider;
|
||||
import org.jetlinks.core.Values;
|
||||
import org.springframework.util.StringUtils;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.core.scheduler.Schedulers;
|
||||
|
||||
|
|
@ -76,33 +77,13 @@ public class AliyunVoiceNotifier extends AbstractNotifier<AliyunVoiceTemplate> {
|
|||
public Mono<Void> send(@Nonnull AliyunVoiceTemplate template, @Nonnull Values context) {
|
||||
|
||||
return Mono.<Void>defer(() -> {
|
||||
try {
|
||||
CommonRequest request = new CommonRequest();
|
||||
request.setSysMethod(MethodType.POST);
|
||||
request.setSysDomain(domain);
|
||||
request.setSysVersion("2017-05-25");
|
||||
request.setSysAction("SingleCallByTts");
|
||||
request.setSysConnectTimeout(connectTimeout);
|
||||
request.setSysReadTimeout(readTimeout);
|
||||
request.putQueryParameter("RegionId", regionId);
|
||||
request.putQueryParameter("CalledShowNumber", template.getCalledShowNumbers());
|
||||
request.putQueryParameter("CalledNumber", template.getCalledNumber(context.getAllValues()));
|
||||
request.putQueryParameter("TtsCode", template.getTtsCode());
|
||||
request.putQueryParameter("PlayTimes", String.valueOf(template.getPlayTimes()));
|
||||
request.putQueryParameter("TtsParam", template.createTtsParam(context.getAllValues()));
|
||||
|
||||
CommonResponse response = client.getCommonResponse(request);
|
||||
|
||||
log.info("发起语音通知完成 {}:{}", response.getHttpResponse().getStatus(), response.getData());
|
||||
|
||||
JSONObject json = JSON.parseObject(response.getData());
|
||||
if (!"ok".equalsIgnoreCase(json.getString("Code"))) {
|
||||
return Mono.error(new BusinessException(json.getString("Message"), json.getString("Code")));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return Mono.error(e);
|
||||
if (AliyunVoiceTemplate.TemplateType.voice == template.getTemplateType()) {
|
||||
return convertVoiceRequest(template, context)
|
||||
.flatMap(this::handleRequest);
|
||||
} else {
|
||||
return convertTtsRequest(template, context)
|
||||
.flatMap(this::handleRequest);
|
||||
}
|
||||
return Mono.empty();
|
||||
}).doOnEach(ReactiveLogger.onError(err -> {
|
||||
log.info("发起语音通知失败", err);
|
||||
})).subscribeOn(Schedulers.boundedElastic());
|
||||
|
|
@ -113,4 +94,69 @@ public class AliyunVoiceNotifier extends AbstractNotifier<AliyunVoiceTemplate> {
|
|||
public Mono<Void> close() {
|
||||
return Mono.fromRunnable(client::shutdown);
|
||||
}
|
||||
|
||||
private Mono<Void> handleRequest(CommonRequest request) {
|
||||
try {
|
||||
CommonResponse response = client.getCommonResponse(request);
|
||||
|
||||
if (response == null) {
|
||||
throw new BusinessException("error.unsupported_voice_notification_type");
|
||||
}
|
||||
|
||||
log.info("发起语音通知完成 {}:{}", response.getHttpResponse().getStatus(), response.getData());
|
||||
|
||||
JSONObject json = JSON.parseObject(response.getData());
|
||||
if (!"ok".equalsIgnoreCase(json.getString("Code"))) {
|
||||
return Mono.error(new BusinessException(json.getString("Message"), json.getString("Code")));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return Mono.error(e);
|
||||
}
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
|
||||
Mono<CommonRequest> convertVoiceRequest(AliyunVoiceTemplate template, Values context){
|
||||
return template
|
||||
.getCalledNumber(context.getAllValues())
|
||||
.next()
|
||||
.map(calledNumber -> {
|
||||
CommonRequest request = convert(template);
|
||||
request.putQueryParameter("CalledNumber", calledNumber);
|
||||
request.setSysAction("SingleCallByVoice");
|
||||
request.putQueryParameter("VoiceCode", template.getTemplateCode());
|
||||
return request;
|
||||
});
|
||||
}
|
||||
|
||||
Mono<CommonRequest> convertTtsRequest(AliyunVoiceTemplate template, Values context){
|
||||
return template
|
||||
.getCalledNumber(context.getAllValues())
|
||||
.next()
|
||||
.map(calledNumber -> {
|
||||
CommonRequest request = convert(template);
|
||||
request.putQueryParameter("CalledNumber", calledNumber);
|
||||
request.putQueryParameter("TtsParam", template.createTtsParam(context.getAllValues()));
|
||||
request.putQueryParameter("TtsCode", template.getTemplateCode());
|
||||
request.setSysAction("SingleCallByTts");
|
||||
return request;
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
CommonRequest convert(AliyunVoiceTemplate template){
|
||||
CommonRequest request = new CommonRequest();
|
||||
request.setSysMethod(MethodType.POST);
|
||||
request.setSysDomain(domain);
|
||||
request.setSysVersion("2017-05-25");
|
||||
request.setSysConnectTimeout(connectTimeout);
|
||||
request.setSysReadTimeout(readTimeout);
|
||||
request.putQueryParameter("RegionId", regionId);
|
||||
// 使用公共号池模板时,不填写主叫显号
|
||||
if (StringUtils.hasText(template.getCalledShowNumbers())) {
|
||||
request.putQueryParameter("CalledShowNumber", template.getCalledShowNumbers());
|
||||
}
|
||||
request.putQueryParameter("PlayTimes", String.valueOf(template.getPlayTimes()));
|
||||
return request;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,17 @@
|
|||
package org.jetlinks.community.notify.voice.aliyun;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.google.common.collect.Maps;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.jetlinks.community.notify.NotifyVariableBusinessConstant;
|
||||
import org.jetlinks.community.notify.template.AbstractTemplate;
|
||||
import org.jetlinks.community.notify.template.Template;
|
||||
import org.jetlinks.community.notify.template.VariableDefinition;
|
||||
import org.jetlinks.community.relation.RelationConstants;
|
||||
import org.jetlinks.community.relation.utils.RelationUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
|
@ -27,24 +31,45 @@ public class AliyunVoiceTemplate extends AbstractTemplate<AliyunVoiceTemplate> {
|
|||
|
||||
@Schema(description = "通知模版ID")
|
||||
@NotBlank(message = "[ttsCode]不能为空")
|
||||
@Deprecated
|
||||
private String ttsCode;
|
||||
|
||||
@Schema(description = "通知模版ID")
|
||||
private String templateCode;
|
||||
|
||||
private String calledShowNumbers;
|
||||
|
||||
private String calledNumber;
|
||||
|
||||
private TemplateType templateType = TemplateType.tts;
|
||||
|
||||
@Schema(description = "通知播放次数")
|
||||
private int playTimes = 1;
|
||||
|
||||
private Map<String, String> ttsParam;
|
||||
|
||||
public String createTtsParam(Map<String, Object> ctx) {
|
||||
Map<String, VariableDefinition> variables = getVariables();
|
||||
|
||||
return JSON.toJSONString(ctx);
|
||||
return JSON.toJSONString(Maps.filterEntries(renderMap(ctx),
|
||||
e -> variables.containsKey(e.getKey())));
|
||||
}
|
||||
|
||||
public String getCalledNumber(Map<String, Object> ctx) {
|
||||
return get(CALLED_NUMBER_KEY, ctx, this::getCalledNumber);
|
||||
public Flux<String> getCalledNumber(Map<String, Object> ctx) {
|
||||
if (StringUtils.hasText(this.getCalledNumber())) {
|
||||
return Flux.just(this.getCalledNumber());
|
||||
}
|
||||
//如果没有指定固定值,则从上下文中获取
|
||||
return RelationUtils
|
||||
.resolve(CALLED_NUMBER_KEY, ctx, RelationConstants.UserProperty.telephone)
|
||||
.map(String::valueOf);
|
||||
}
|
||||
|
||||
public String getTemplateCode() {
|
||||
if (templateCode == null) {
|
||||
return ttsCode;
|
||||
}
|
||||
return templateCode;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
|
|
@ -58,10 +83,16 @@ public class AliyunVoiceTemplate extends AbstractTemplate<AliyunVoiceTemplate> {
|
|||
VariableDefinition
|
||||
.builder()
|
||||
.id(CALLED_NUMBER_KEY)
|
||||
.name("收信人")
|
||||
.name("被叫号码")
|
||||
.description("收信人手机号码")
|
||||
.expand(NotifyVariableBusinessConstant.businessId,
|
||||
NotifyVariableBusinessConstant.NotifyVariableBusinessTypes.userType)
|
||||
.required(true)
|
||||
.build()
|
||||
);
|
||||
}
|
||||
|
||||
public enum TemplateType {
|
||||
voice, tts
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue