Skip to content

Commit

Permalink
fix: 修复阿里云语音通知可能提示参数过长问题 (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao authored Oct 30, 2023
1 parent 807f162 commit a46c4fd
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;

Expand Down Expand Up @@ -76,33 +77,13 @@ public Provider getProvider() {
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());
Expand All @@ -113,4 +94,69 @@ public Mono<Void> send(@Nonnull AliyunVoiceTemplate template, @Nonnull Values co
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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -58,10 +83,16 @@ protected List<VariableDefinition> getEmbeddedVariables() {
VariableDefinition
.builder()
.id(CALLED_NUMBER_KEY)
.name("收信人")
.name("被叫号码")
.description("收信人手机号码")
.expand(NotifyVariableBusinessConstant.businessId,
NotifyVariableBusinessConstant.NotifyVariableBusinessTypes.userType)
.required(true)
.build()
);
}

public enum TemplateType {
voice, tts
}
}

0 comments on commit a46c4fd

Please sign in to comment.