腾讯云短信服务使用记录与.NET Core C#代码分享
1、即使是相同的短信簽名與短信正文模板,也需要針對“國內文本短信”與“海外文本短信”分別申請。開始不知道,以為只要申請一次,給國外手機發短信時給api傳對應的國家碼就行,后來才發現需要分別申請。
2、短信服務web api響應“手機號內容頻率限制”錯誤。這是由于在30秒內向同一手機號多次發送了相同內容的短信,這是騰訊云短信服務的默認限制——“相同內容短信對同一個手機號,30秒內發送短信條數不超過1條”,可以通過“應用配置”的“短信頻率配置”修改這個限制。
3、騰訊云短信服務沒有提供 .NET Core 的 SDK,我們自己實現的代碼如下:
public class TencentCloudSmsService : ISmsService
{
? ? private static readonly HttpClient _httpClient =?
? ? ? ? new HttpClient { BaseAddress = new Uri("https://yun.tim.qq.com") };
? ? private readonly string _appId;
? ? private readonly string _appKey;
? ? private const string SIGNATURE = "...";
? ? private const int DOMESTIC_TEMPLATE_ID = 1234;
? ? private const int OVERSEA_TEMPLATE_ID = 5678;
? ? private readonly ILogger _logger;
? ? public TencentCloudSmsService(IConfiguration conf,
? ? ? ? ILoggerFactory loggerFactory)
? ? {
? ? ? ? _appId = conf["tencentCloudSms:appId"];
? ? ? ? if (string.IsNullOrEmpty(_appId))
? ? ? ? ? ? throw new ArgumentException($"{nameof(_appId)} must have a value");
? ? ? ? _appKey = conf["tencentCloudSms:appKey"];
? ? ? ? if (string.IsNullOrEmpty(_appKey))
? ? ? ? ? ? throw new ArgumentException($"{nameof(_appKey)} must have a value");
? ? ? ? _logger = loggerFactory.CreateLogger<TencentCloudSmsService>();
? ? }
? ? public async Task<bool> SendCode(string countryCode, long mobile, int code)
? ? {
? ? ? ? var random = GetRandom();
? ? ? ? var timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
? ? ? ? var data = new
? ? ? ? {
? ? ? ? ? ? tel = new { nationcode = countryCode.Replace("+", ""), mobile = mobile.ToString() },
? ? ? ? ? ? sign = SIGNATURE,
? ? ? ? ? ? tpl_id = countryCode == "+86" ? DOMESTIC_TEMPLATE_ID : OVERSEA_TEMPLATE_ID ,
? ? ? ? ? ? @params = new[] { code.ToString() },
? ? ? ? ? ? sig = ComputeSignature(mobile, random, timestamp),
? ? ? ? ? ? time = timestamp,
? ? ? ? ? ? extend = "",
? ? ? ? ? ? ext = ""
? ? ? ? };
? ? ? ? var url = $"/v5/tlssmssvr/sendsms?sdkappid={_appId}&random={random}";
? ? ? ? _logger.LogDebug("Post to " + _httpClient.BaseAddress + url);
? ? ? ? var response = await _httpClient.PostAsJsonAsync<dynamic>(url, data);
? ? ? ? _logger.LogDebug("Post data:\n" + JsonConvert.SerializeObject(data));
? ? ? ? response.EnsureSuccessStatusCode();
? ? ? ? var result = await response.Content.ReadAsAsync<dynamic>();
? ? ? ? if(result.result != 0)
? ? ? ? {
? ? ? ? ? ? _logger.LogError($"Failed to send message to {countryCode}-{mobile}: {result.errmsg}"); ? ? ? ? ? ? ? ?
? ? ? ? ? ? return false;
? ? ? ? }
? ? ? ? return true;
? ? }
? ? private string ComputeSignature(long mobile, int random, long timestamp)
? ? {
? ? ? ? var input = $"appkey={_appKey}&random={random}&time={timestamp}&mobile={mobile}";
? ? ? ? var hasBytes = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(input));
? ? ? ? return string.Join("", hasBytes.Select(b => b.ToString("x2")));
? ? }
? ? private int GetRandom()
? ? {
? ? ? ? return new Random().Next(100000, 999999);
? ? }
}
原文地址:http://www.cnblogs.com/dudu/p/7782376.html
.NET社區新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com
總結
以上是生活随笔為你收集整理的腾讯云短信服务使用记录与.NET Core C#代码分享的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL2017 Azure SQL新功能
- 下一篇: 在ASP.NET Core中使用AOP来