腾讯云短信服务使用记录与.NET Core C#代码分享
1、即使是相同的短信簽名與短信正文模板,也需要針對(duì)“國(guó)內(nèi)文本短信”與“海外文本短信”分別申請(qǐng)。開始不知道,以為只要申請(qǐng)一次,給國(guó)外手機(jī)發(fā)短信時(shí)給api傳對(duì)應(yīng)的國(guó)家碼就行,后來(lái)才發(fā)現(xiàn)需要分別申請(qǐng)。
2、短信服務(wù)web api響應(yīng)“手機(jī)號(hào)內(nèi)容頻率限制”錯(cuò)誤。這是由于在30秒內(nèi)向同一手機(jī)號(hào)多次發(fā)送了相同內(nèi)容的短信,這是騰訊云短信服務(wù)的默認(rèn)限制——“相同內(nèi)容短信對(duì)同一個(gè)手機(jī)號(hào),30秒內(nèi)發(fā)送短信條數(shù)不超過(guò)1條”,可以通過(guò)“應(yīng)用配置”的“短信頻率配置”修改這個(gè)限制。
3、騰訊云短信服務(wù)沒有提供 .NET Core 的 SDK,我們自己實(shí)現(xiàn)的代碼如下:
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社區(qū)新聞,深度好文,歡迎訪問(wèn)公眾號(hào)文章匯總 http://www.csharpkit.com
總結(jié)
以上是生活随笔為你收集整理的腾讯云短信服务使用记录与.NET Core C#代码分享的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SQL2017 Azure SQL新功能
- 下一篇: 开源纯C#工控网关+组态软件(六)图元组