using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TencentCloud.Common; using TencentCloud.Common.Profile; using TencentCloud.Sms.V20210111; using TencentCloud.Sms.V20210111.Models; using ChouBox.Code.Contract; using ChouBox.Model.Entities; using ChouBox.Code.TencentCloudExtend.Model; namespace ChouBox.Code.TencentCloudExtend; /// /// 腾讯云发送短信 /// /// public class TencentSMSSendVerificationCode(TencentSMSConfig tencentSMSConfig, string PhoneNum) : ISendVerificationCode { public async Task SendVerificationCode(string code, int expireTime) { if (string.IsNullOrEmpty(code)) { throw new ArgumentNullException("参数错误"); } string phoneNum = PhoneNum; string verificationCode = code; if (!phoneNum.StartsWith("+86")) { phoneNum = "+86" + phoneNum; } try { // 必要步骤: // 实例化一个认证对象,入参需要传入腾讯云账户密钥对 SecretId,SecretKey。 // 为了保护密钥安全,建议将密钥设置在环境变量中或者配置文件中。 // 硬编码密钥到代码中有可能随代码泄露而暴露,有安全隐患,并不推荐。 // 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。 // SecretId、SecretKey 查询:https://console.cloud.tencent.com/cam/capi Credential cred = new Credential { SecretId = tencentSMSConfig.SecretId, SecretKey = tencentSMSConfig.SecretKey }; /* 非必要步骤: * 实例化一个客户端配置对象,可以指定超时时间等配置 */ ClientProfile clientProfile = new ClientProfile(); /* SDK默认用TC3-HMAC-SHA256进行签名 * 非必要请不要修改这个字段 */ clientProfile.SignMethod = ClientProfile.SIGN_TC3SHA256; /* 非必要步骤 * 实例化一个客户端配置对象,可以指定超时时间等配置 */ HttpProfile httpProfile = new HttpProfile(); /* SDK默认使用POST方法。 * 如果您一定要使用GET方法,可以在这里设置。GET方法无法处理一些较大的请求 */ httpProfile.ReqMethod = tencentSMSConfig.ReqMethod; httpProfile.Timeout = tencentSMSConfig.Timeout; // 请求连接超时时间,单位为秒(默认60秒) /* 指定接入地域域名,默认就近地域接入域名为 sms.tencentcloudapi.com ,也支持指定地域域名访问,例如广州地域的域名为 sms.ap-guangzhou.tencentcloudapi.com */ httpProfile.Endpoint = "sms.tencentcloudapi.com"; // 代理服务器,当您的环境下有代理服务器时设定(无需要直接忽略) // httpProfile.WebProxy = Environment.GetEnvironmentVariable("HTTPS_PROXY"); clientProfile.HttpProfile = httpProfile; /* 实例化要请求产品(以sms为例)的client对象 * 第二个参数是地域信息,可以直接填写字符串ap-guangzhou,支持的地域列表参考 https://cloud.tencent.com/document/api/382/52071#.E5.9C.B0.E5.9F.9F.E5.88.97.E8.A1.A8 */ SmsClient client = new SmsClient(cred, "ap-nanjing", clientProfile); /* 实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数 * 您可以直接查询SDK源码确定SendSmsRequest有哪些属性可以设置 * 属性可能是基本类型,也可能引用了另一个数据结构 * 推荐使用IDE进行开发,可以方便的跳转查阅各个接口和数据结构的文档说明 */ SendSmsRequest req = new SendSmsRequest(); /* 基本类型的设置: * SDK采用的是指针风格指定参数,即使对于基本类型您也需要用指针来对参数赋值。 * SDK提供对基本类型的指针引用封装函数 * 帮助链接: * 短信控制台: https://console.cloud.tencent.com/smsv2 * 腾讯云短信小助手: https://cloud.tencent.com/document/product/382/3773#.E6.8A.80.E6.9C.AF.E4.BA.A4.E6.B5.81 */ /* 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId,示例如1400006666 */ // 应用 ID 可前往 [短信控制台](https://console.cloud.tencent.com/smsv2/app-manage) 查看 req.SmsSdkAppId = tencentSMSConfig.SmsSdkAppId; /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 */ // 签名信息可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-sign) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-sign) 的签名管理查看 req.SignName = tencentSMSConfig.SignName; /* 模板 ID: 必须填写已审核通过的模板 ID */ // 模板 ID 可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-template) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-template) 的正文模板管理查看 req.TemplateId = tencentSMSConfig.TemplateId; /* 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空 */ req.TemplateParamSet = new String[] { verificationCode, expireTime.ToString() }; /* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号] * 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/ req.PhoneNumberSet = new String[] { phoneNum }; /* 用户的 session 内容(无需要可忽略): 可以携带用户侧 ID 等上下文信息,server 会原样返回 */ req.SessionContext = ""; /* 短信码号扩展号(无需要可忽略): 默认未开通,如需开通请联系 [腾讯云短信小助手] */ req.ExtendCode = ""; /* 国内短信无需填写该项;国际/港澳台短信已申请独立 SenderId 需要填写该字段,默认使用公共 SenderId,无需填写该字段。注:月度使用量达到指定量级可申请独立 SenderId 使用,详情请联系 [腾讯云短信小助手](https://cloud.tencent.com/document/product/382/3773#.E6.8A.80.E6.9C.AF.E4.BA.A4.E6.B5.81)。 */ req.SenderId = ""; SendSmsResponse resp = await client.SendSms(req); Console.WriteLine(AbstractModel.ToJsonString(resp)); // 检查短信发送状态 if (resp.SendStatusSet != null && resp.SendStatusSet.Length > 0) { foreach (var status in resp.SendStatusSet) { if (status.Code != "Ok") { // 检查特定错误码并提供更友好的错误信息 if (status.Code == "LimitExceeded.PhoneNumberSameContentDailyLimit" || status.Code == "LimitExceeded.PhoneNumberDailyLimit") { throw new Exception($"您的手机号今日接收该内容的短信次数已达上限,请明天再试"); } else if (status.Code == "LimitExceeded.PhoneNumberThirtySecondLimit") { throw new Exception($"您的手机号短信请求过于频繁,请30秒后再试"); } else { throw new Exception($"短信发送失败:手机号 {status.PhoneNumber},错误代码 {status.Code},错误信息 {status.Message}"); } } } } else { throw new Exception("短信发送失败:未收到发送状态"); } } catch (Exception e) { Console.WriteLine(e.ToString()); throw; // 重新抛出异常,而不是返回false } //Console.Read(); return true; } }