142 lines
4.7 KiB
Go
142 lines
4.7 KiB
Go
|
|
/*获取对应sdk
|
|||
|
|
go get -u github.com/tencentcloud/tencentcloud-sdk-go
|
|||
|
|
*/
|
|||
|
|
package voicemsgMgr
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"fmt"
|
|||
|
|
|
|||
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
|||
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
|
|||
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
|
|||
|
|
vms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vms/v20200902" // 引入 vms
|
|||
|
|
"goutil/logUtil"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
const (
|
|||
|
|
//请求方法
|
|||
|
|
REG_METHOD = "POST"
|
|||
|
|
//域名
|
|||
|
|
END_POINT = "vms.tencentcloudapi.com"
|
|||
|
|
//签名方式
|
|||
|
|
SIGN_METHOD = "TC3-HMAC-SHA256"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
//语音电话结构体
|
|||
|
|
type VoiceMessage struct {
|
|||
|
|
//语音发送客户端
|
|||
|
|
client *vms.Client
|
|||
|
|
|
|||
|
|
//配置
|
|||
|
|
voiceMessageConfig *VoiceMessageConfig
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//拨打语音电话
|
|||
|
|
//telephone:电话号码(格式:+[国家或地区码][用户号码])
|
|||
|
|
//playTimes:播放次数,可选,最多3次,默认2次
|
|||
|
|
//para:参数,根据配置的模板传入参数格式,按顺序传入
|
|||
|
|
func (this *VoiceMessage) SendVoice(telephone string, playTimes uint64, param ...string) (err error) {
|
|||
|
|
//初始化请求参数
|
|||
|
|
request := vms.NewSendTtsVoiceRequest()
|
|||
|
|
|
|||
|
|
// 模板 ID,必须填写在控制台审核通过的模板 ID,可登录 [语音消息控制台] 查看模板 ID
|
|||
|
|
request.TemplateId = common.StringPtr(this.voiceMessageConfig.TemplateId)
|
|||
|
|
|
|||
|
|
//模板构造参数,模板参数,若模板没有参数,则为空数组
|
|||
|
|
paramLit := []string{}
|
|||
|
|
if len(param) > 0 {
|
|||
|
|
for _, val := range param {
|
|||
|
|
paramLit = append(paramLit, val)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
request.TemplateParamSet = common.StringPtrs(paramLit)
|
|||
|
|
|
|||
|
|
/* 被叫手机号码,采用 e.164 标准,格式为+[国家或地区码][用户号码]
|
|||
|
|
* 例如:+8613711112222,其中前面有一个+号,86为国家码,13711112222为手机号*/
|
|||
|
|
request.CalledNumber = common.StringPtr(telephone)
|
|||
|
|
|
|||
|
|
//在语音控制台添加应用后生成的实际SdkAppid,示例如1400006666
|
|||
|
|
request.VoiceSdkAppid = common.StringPtr(this.voiceMessageConfig.VoiceSdkAppid)
|
|||
|
|
|
|||
|
|
//播放次数,可选,最多3次,默认2次
|
|||
|
|
request.PlayTimes = common.Uint64Ptr(playTimes)
|
|||
|
|
|
|||
|
|
//用户的 session 内容,腾讯 server 回包中会原样返回
|
|||
|
|
request.SessionContext = common.StringPtr("test")
|
|||
|
|
|
|||
|
|
//通过 client 对象调用想要访问的接口,需要传入请求对象
|
|||
|
|
_, err = this.client.SendTtsVoice(request)
|
|||
|
|
// 处理异常
|
|||
|
|
if _, ok := err.(*errors.TencentCloudSDKError); ok {
|
|||
|
|
logUtil.ErrorLog(fmt.Sprintf("SDK异常,语音电话拨打失败,错误信息为:%s", err))
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
//非SDK异常,直接失败。实际代码中可以加入其他的处理
|
|||
|
|
if err != nil {
|
|||
|
|
logUtil.ErrorLog(fmt.Sprintf("非SDK异常,语音电话拨打失败,错误信息为:%s", err))
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//创建新的对象
|
|||
|
|
//secretId:API密钥Id
|
|||
|
|
//secretKey:API密钥key
|
|||
|
|
//region:地域
|
|||
|
|
//templateId:模板ID
|
|||
|
|
//appid:应用后生成的实际SdkAppid
|
|||
|
|
func NewVoiceMessage(secretId, secretKey, region, templateId, appid string) *VoiceMessage {
|
|||
|
|
//构造配置模型
|
|||
|
|
voiceMessageConfigObj := &VoiceMessageConfig{
|
|||
|
|
SecretId: secretId,
|
|||
|
|
SecretKey: secretKey,
|
|||
|
|
Region: region,
|
|||
|
|
TemplateId: templateId,
|
|||
|
|
VoiceSdkAppid: appid,
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//通过配置实例化
|
|||
|
|
return NewVoiceMessageBuConfig(voiceMessageConfigObj)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//通过配置实例化
|
|||
|
|
func NewVoiceMessageBuConfig(voiceMessageConfigObj *VoiceMessageConfig) *VoiceMessage {
|
|||
|
|
//实例化一个认证对象,入参需要传入腾讯云账户密钥对 secretId 和 secretKey
|
|||
|
|
credential := common.NewCredential(
|
|||
|
|
voiceMessageConfigObj.SecretId,
|
|||
|
|
voiceMessageConfigObj.SecretKey,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
/* 非必要步骤:
|
|||
|
|
* 实例化一个客户端配置对象,可以指定超时时间等配置 */
|
|||
|
|
cpf := profile.NewClientProfile()
|
|||
|
|
/* SDK 默认使用 POST 方法
|
|||
|
|
* 如需使用 GET 方法,可以在此处设置,但 GET 方法无法处理较大的请求 */
|
|||
|
|
cpf.HttpProfile.ReqMethod = REG_METHOD
|
|||
|
|
/* SDK 有默认的超时时间,非必要请不要进行调整
|
|||
|
|
* 如有需要请在代码中查阅以获取最新的默认值 */
|
|||
|
|
//cpf.HttpProfile.ReqTimeout = 5
|
|||
|
|
/* SDK 会自动指定域名,通常无需指定域名,但访问金融区的服务时必须手动指定域名
|
|||
|
|
* 例如 VMS 的上海金融区域名为 vms.ap-shanghai-fsi.tencentcloudapi.com */
|
|||
|
|
cpf.HttpProfile.Endpoint = END_POINT
|
|||
|
|
/* SDK 默认用 TC3-HMAC-SHA256 进行签名,非必要请不要修改该字段 */
|
|||
|
|
cpf.SignMethod = SIGN_METHOD
|
|||
|
|
|
|||
|
|
//初始化客户端
|
|||
|
|
vmsclient, err := vms.NewClient(credential, voiceMessageConfigObj.Region, cpf)
|
|||
|
|
if err != nil {
|
|||
|
|
logUtil.ErrorLog(fmt.Sprintf("初始化VMS的client 对象失败,错误信息为:%s", err))
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//构造对象
|
|||
|
|
voiceMessageObj := &VoiceMessage{
|
|||
|
|
client: vmsclient,
|
|||
|
|
voiceMessageConfig: voiceMessageConfigObj,
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//返回对象
|
|||
|
|
return voiceMessageObj
|
|||
|
|
}
|