package com.usky.alarm.service.config.voice;
/**
 * 语音拨号API
 *
 * @author han
 * @date 2023/9/12
 */
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dyvmsapi.model.v20170525.SingleCallByTtsRequest;
import com.aliyuncs.dyvmsapi.model.v20170525.SingleCallByTtsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 阿里云语音服务调用接口 - 根据文本模板来进行语音通知
 */
@Component
public class VoiceNotice {
    @Value("${oss.product}")
    private String product;
    @Value("${oss.domain}")
    private String domain;
    @Value("${oss.accessKeyId}")
    private String accessKeyId;
    @Value("${oss.accessKeySecret}")
    private String accessKeySecret;

    /**
     * 调用阿里云语音服务的方法
     *    当address为空的时候, 就说明不是采用面对面咨询的方式
     *    使用GeneralUtil.isNotNullAndEmpty(address)判断
     *
     * @return
     * @throws ClientException
     */
    public SingleCallByTtsResponse sendCVoice(String phone, String visitorName, String gz,String bjsj) throws ClientException{
        //设置访问超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        //初始化acsClient 暂时不支持多region
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        SingleCallByTtsRequest request = new SingleCallByTtsRequest();
        //必填-被叫显号,可在语音控制台中找到所购买的显号
        request.setCalledShowNumber("07952097202");
        //必填-被叫号码
        request.setCalledNumber(phone);
        //必填-Tts模板ID
        request.setTtsCode("TTS_173475471");
        // 这里主要是填写我们语音文字模板里面的参数 ${param}
        request.setTtsParam("{\"type\":\"" + visitorName +"\", \"gz\":\"" + gz + "\", \"bjsj\":\"" + bjsj + "\"}");
        //可选-音量 取值范围 0--200
        request.setVolume(200);
        //可选-播放次数
        request.setPlayTimes(2);
        //可选-外部扩展字段,此ID将在回执消息中带回给调用方
        SingleCallByTtsResponse singleCallByTtsResponse = acsClient.getAcsResponse(request);
        if(singleCallByTtsResponse.getCode() != null && singleCallByTtsResponse.getCode().equals("OK")) {
            //请求成功
            System.out.print(singleCallByTtsResponse.getRequestId()+singleCallByTtsResponse.getCode()+"15122423833");
        }
        return singleCallByTtsResponse;
    }
}
