From a46c4fd07c6a06d828d49aed79d9fbb432d20097 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E5=91=A8?= Date: Mon, 30 Oct 2023 10:06:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=98=BF=E9=87=8C?= =?UTF-8?q?=E4=BA=91=E8=AF=AD=E9=9F=B3=E9=80=9A=E7=9F=A5=E5=8F=AF=E8=83=BD?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E5=8F=82=E6=95=B0=E8=BF=87=E9=95=BF=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#433)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../voice/aliyun/AliyunVoiceNotifier.java | 98 ++++++++++++++----- .../voice/aliyun/AliyunVoiceTemplate.java | 41 +++++++- 2 files changed, 108 insertions(+), 31 deletions(-) diff --git a/jetlinks-components/notify-component/notify-voice/src/main/java/org/jetlinks/community/notify/voice/aliyun/AliyunVoiceNotifier.java b/jetlinks-components/notify-component/notify-voice/src/main/java/org/jetlinks/community/notify/voice/aliyun/AliyunVoiceNotifier.java index 8ab60813..c4114d03 100755 --- a/jetlinks-components/notify-component/notify-voice/src/main/java/org/jetlinks/community/notify/voice/aliyun/AliyunVoiceNotifier.java +++ b/jetlinks-components/notify-component/notify-voice/src/main/java/org/jetlinks/community/notify/voice/aliyun/AliyunVoiceNotifier.java @@ -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 { public Mono send(@Nonnull AliyunVoiceTemplate template, @Nonnull Values context) { return Mono.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 { public Mono close() { return Mono.fromRunnable(client::shutdown); } + + private Mono 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 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 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; + } } diff --git a/jetlinks-components/notify-component/notify-voice/src/main/java/org/jetlinks/community/notify/voice/aliyun/AliyunVoiceTemplate.java b/jetlinks-components/notify-component/notify-voice/src/main/java/org/jetlinks/community/notify/voice/aliyun/AliyunVoiceTemplate.java index 13a5143b..c4084d24 100755 --- a/jetlinks-components/notify-component/notify-voice/src/main/java/org/jetlinks/community/notify/voice/aliyun/AliyunVoiceTemplate.java +++ b/jetlinks-components/notify-component/notify-voice/src/main/java/org/jetlinks/community/notify/voice/aliyun/AliyunVoiceTemplate.java @@ -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 { @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 ttsParam; public String createTtsParam(Map ctx) { + Map variables = getVariables(); - return JSON.toJSONString(ctx); + return JSON.toJSONString(Maps.filterEntries(renderMap(ctx), + e -> variables.containsKey(e.getKey()))); } - public String getCalledNumber(Map ctx) { - return get(CALLED_NUMBER_KEY, ctx, this::getCalledNumber); + public Flux getCalledNumber(Map 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 { VariableDefinition .builder() .id(CALLED_NUMBER_KEY) - .name("收信人") + .name("被叫号码") .description("收信人手机号码") + .expand(NotifyVariableBusinessConstant.businessId, + NotifyVariableBusinessConstant.NotifyVariableBusinessTypes.userType) .required(true) .build() ); } + + public enum TemplateType { + voice, tts + } }