VoiceUtil.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package com.care.common.util;
  2. import com.tencentcloudapi.common.Credential;
  3. import com.tencentcloudapi.common.exception.TencentCloudSDKException;
  4. // 导入可选配置类
  5. import com.tencentcloudapi.common.profile.ClientProfile;
  6. import com.tencentcloudapi.common.profile.HttpProfile;
  7. // 导入对应VMS模块的client
  8. import com.tencentcloudapi.vms.v20200902.VmsClient;
  9. // 导入要请求接口对应的request response类
  10. import com.tencentcloudapi.vms.v20200902.models.SendStatus;
  11. import com.tencentcloudapi.vms.v20200902.models.SendTtsVoiceRequest;
  12. import com.tencentcloudapi.vms.v20200902.models.SendTtsVoiceResponse;
  13. /**
  14. * Tencent Cloud Vms SendTtsVoice
  15. * https://cloud.tencent.com/document/product/1128/51558
  16. *
  17. */
  18. public class VoiceUtil {
  19. public static SendStatus sendTtsVoice(String endpoint,
  20. String region,
  21. String secretId,
  22. String secretKey,
  23. String voiceSdkAppId,
  24. String templateId,
  25. String phoneNumber,
  26. String[] templateParam) {
  27. try {
  28. /* 必要步骤:
  29. * 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。
  30. * 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。
  31. * 您也可以直接在代码中写死密钥对,但是小心不要将代码复制、上传或者分享给他人,
  32. * 以免泄露密钥对危及您的财产安全。
  33. * CAM密匙查询: https://console.cloud.tencent.com/cam/capi*/
  34. Credential cred = new Credential(secretId, secretKey);
  35. // 实例化一个http选项,可选,没有特殊需求可以跳过
  36. HttpProfile httpProfile = new HttpProfile();
  37. // 设置代理
  38. // httpProfile.setProxyHost("host");
  39. // httpProfile.setProxyPort(port);
  40. /* SDK默认使用POST方法。
  41. * 如果您一定要使用GET方法,可以在这里设置。GET方法无法处理一些较大的请求 */
  42. httpProfile.setReqMethod("POST");
  43. /* SDK有默认的超时时间,非必要请不要进行调整
  44. * 如有需要请在代码中查阅以获取最新的默认值 */
  45. httpProfile.setConnTimeout(60);
  46. /* SDK会自动指定域名。通常是不需要特地指定域名的,但是如果您访问的是金融区的服务
  47. * 则必须手动指定域名,例如vms的上海金融区域名: vms.ap-shanghai-fsi.tencentcloudapi.com */
  48. httpProfile.setEndpoint(endpoint);
  49. /* 非必要步骤:
  50. * 实例化一个客户端配置对象,可以指定超时时间等配置 */
  51. ClientProfile clientProfile = new ClientProfile();
  52. /* SDK默认用TC3-HMAC-SHA256进行签名
  53. * 非必要请不要修改这个字段 */
  54. clientProfile.setSignMethod("TC3-HMAC-SHA256");
  55. clientProfile.setHttpProfile(httpProfile);
  56. /* 实例化要请求产品(以vms为例)的client对象
  57. * 第二个参数是地域信息,可以直接填写字符串ap-guangzhou,或者引用预设的常量 */
  58. VmsClient client = new VmsClient(cred, region, clientProfile);
  59. /* 实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数
  60. * 您可以直接查询SDK源码确定接口有哪些属性可以设置
  61. * 属性可能是基本类型,也可能引用了另一个数据结构
  62. * 推荐使用IDE进行开发,可以方便的跳转查阅各个接口和数据结构的文档说明 */
  63. SendTtsVoiceRequest req = new SendTtsVoiceRequest();
  64. /* 填充请求参数,这里request对象的成员变量即对应接口的入参
  65. * 您可以通过官网接口文档或跳转到request对象的定义处查看请求参数的定义
  66. * 基本类型的设置:
  67. * 帮助链接:
  68. * 语音消息控制台:https://console.cloud.tencent.com/vms
  69. * vms helper:https://cloud.tencent.com/document/product/1128/37720 */
  70. // 模板 ID,必须填写在控制台审核通过的模板 ID,可登录 [语音消息控制台] 查看模板 ID
  71. req.setTemplateId(templateId);
  72. // 模板参数,若模板没有参数,请提供为空数组
  73. req.setTemplateParamSet(templateParam);
  74. /* 被叫手机号码,采用 e.164 标准,格式为+[国家或地区码][用户号码]
  75. * 例如:+8613711112222,其中前面有一个+号,86为国家码,13711112222为手机号 */
  76. req.setCalledNumber(phoneNumber);
  77. // 在 [语音控制台] 添加应用后生成的实际SdkAppid,示例如1400006666
  78. req.setVoiceSdkAppid(voiceSdkAppId);
  79. // 播放次数,可选,最多3次,默认2次
  80. Long playTimes = 2L;
  81. req.setPlayTimes(playTimes);
  82. // 用户的 session 内容,腾讯 server 回包中会原样返回
  83. String sessionContext = "xxxx";
  84. req.setSessionContext(sessionContext);
  85. /* 通过 client 对象调用 SendTtsVoice 方法发起请求。注意请求方法名与请求对象是对应的
  86. * 返回的 res 是一个 SendTtsVoiceResponse 类的实例,与请求对象对应 */
  87. SendTtsVoiceResponse res = client.SendTtsVoice(req);
  88. // 输出json格式的字符串回包
  89. System.out.println(SendTtsVoiceResponse.toJsonString(res));
  90. // 也可以取出单个值,您可以通过官网接口文档或跳转到response对象的定义处查看返回字段的定义
  91. System.out.println(res.getRequestId());
  92. return res.getSendStatus();
  93. } catch (TencentCloudSDKException e) {
  94. e.printStackTrace();
  95. return null;
  96. }
  97. }
  98. public static void main(String[] args) {
  99. }
  100. }