API调用腾讯云短信

Posted likeheart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了API调用腾讯云短信相关的知识,希望对你有一定的参考价值。

在之前介绍的火车票查询工具中,利用邮件和短信将查询结果推送给用户。免费短信的条数只有5条,用完之后只能单独使用邮件提醒。

最近发现腾讯云的福利,简单的介绍一下用法。

 

 

 

 

 

 

 

 

 

 

腾讯云-》产品-》通信服务-》短信-》开通服务-》添加应用-》创建签名和模板-》等待审核通过-》按照Demo测试

 

 

在整个流程中,最耗时的就是创建签名和模板,对个人来说只能选择app、公众号或小程序的方式进行申请,多亏之前折腾过一点小程序,从而闯关成功。

接下来查看官方Demo https://github.com/qcloudsms/qcloudsms/tree/master/demo/csharp

整理后的测试Demo

调用代码:

 1 using QcloudSms;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace ConsoleTest
 9 {
10     class SmsSDKDemo
11     {
12         /// <summary>
13         /// appId
14         /// </summary>
15         private static int appId = 140xxxxx;
16         /// <summary>
17         /// appKey
18         /// </summary>
19         private static string appKey = "xxxxxxxxxxxxxxx";
20         /// <summary>
21         /// 接收手机号
22         /// </summary>
23         private static string phoneNumber = "xxxxxxxx";
24         /// <summary>
25         /// 短信模板ID
26         /// </summary>
27         private static int tmplateId = xxxxxxx;
28 
29         static void Main(string[] args)
30         {
31             try
32             {
33                 SmsSingleSenderResult singleResult;
34                 SmsSingleSender singleSender = new SmsSingleSender(appId, appKey);
35 
36                 singleResult = singleSender.Send(SmsType.普通短信, phoneNumber, "");
37                 Console.WriteLine(singleResult);
38 
39                 List<string> templParams = new List<string>();
40                 templParams.Add("G2619");
41                 templParams.Add("二等座:23 无座:128");
42                 singleResult = singleSender.SendWithParam(phoneNumber, tmplateId, templParams);
43                 Console.WriteLine(singleResult);
44             }
45             catch (Exception e)
46             {
47                 Console.WriteLine(e);
48             }
49             Console.Read();
50         }
51     }
52 }
  1 namespace QcloudSms
  2 {
  3     #region 短信类型枚举
  4     /// <summary>
  5     /// 短信类型枚举
  6     /// </summary>
  7     public enum SmsType
  8     {
  9         普通短信 = 0,
 10         营销短信 = 1
 11     }
 12     #endregion
 13 
 14     #region 单发
 15     /// <summary>
 16     /// 单发
 17     /// </summary>
 18     class SmsSingleSender
 19     {
 20         #region 变量
 21         /// <summary>
 22         /// appId
 23         /// </summary>
 24         private int appId;
 25         /// <summary>
 26         /// appkey
 27         /// </summary>
 28         private string appkey;
 29         /// <summary>
 30         /// url
 31         /// </summary>
 32         private string url = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms";
 33         /// <summary>
 34         /// util
 35         /// </summary>
 36         private SmsSenderUtil util = new SmsSenderUtil();
 37         #endregion
 38 
 39         #region 构造
 40         /// <summary>
 41         /// 构造函数
 42         /// </summary>
 43         /// <param name="sdkappid"></param>
 44         /// <param name="appkey"></param>
 45         public SmsSingleSender(int sdkappid, string appkey)
 46         {
 47             this.appId = sdkappid;
 48             this.appkey = appkey;
 49         }
 50         #endregion
 51 
 52         #region 普通单发短信接口
 53         /// <summary>
 54         /// 普通单发短信接口,明确指定内容,如果有多个签名,请在内容中以【】的方式添加到信息内容中,否则系统将使用默认签名
 55         /// </summary>
 56         /// <param name="type">短信类型,0 为普通短信,1 营销短信</param>
 57         /// <param name="phoneNumber">不带国家码的手机号</param>
 58         /// <param name="msg">信息内容,必须与申请的模板格式一致,否则将返回错误</param>
 59         /// <param name="extend">短信码号扩展号,格式为纯数字串,其他格式无效。默认没有开通</param>
 60         /// <param name="ext">服务端原样返回的参数,可填空</param>
 61         /// <returns>SmsSingleSenderResult</returns>
 62         public SmsSingleSenderResult Send(SmsType type, string phoneNumber, string msg, string extend = "", string ext = "")
 63         {
 64             long random = util.GetRandom();
 65             long curTime = util.GetCurTime();
 66 
 67             // 按照协议组织 post 请求包体
 68             JObject tel = new JObject();
 69             tel.Add("nationcode", SmsSenderUtil.nationCode);
 70             tel.Add("mobile", phoneNumber);
 71             JObject data = new JObject();
 72             data.Add("tel", tel);
 73             data.Add("msg", msg);
 74             data.Add("type", (int)type);
 75             data.Add("sig", util.StrToHash(string.Format("appkey={0}&random={1}&time={2}&mobile={3}", appkey, random, curTime, phoneNumber)));
 76             data.Add("time", curTime);
 77             data.Add("extend", extend);
 78             data.Add("ext", ext);
 79 
 80             string wholeUrl = url + "?sdkappid=" + appId + "&random=" + random;
 81             HttpWebRequest request = util.GetPostHttpConn(wholeUrl);
 82             byte[] requestData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data));
 83             request.ContentLength = requestData.Length;
 84             Stream requestStream = request.GetRequestStream();
 85             requestStream.Write(requestData, 0, requestData.Length);
 86             requestStream.Close();
 87 
 88             // 接收返回包
 89             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 90             Stream responseStream = response.GetResponseStream();
 91             StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
 92             string responseStr = streamReader.ReadToEnd();
 93             streamReader.Close();
 94             responseStream.Close();
 95             SmsSingleSenderResult result;
 96             if (HttpStatusCode.OK == response.StatusCode)
 97             {
 98                 result = util.ResponseStrToSingleSenderResult(responseStr);
 99             }
100             else
101             {
102                 result = new SmsSingleSenderResult();
103                 result.result = -1;
104                 result.errmsg = "http error " + response.StatusCode + " " + responseStr;
105             }
106             return result;
107         }
108         #endregion
109 
110         #region 指定模板单发
111         /// <summary>
112         /// 指定模板单发
113         /// </summary>
114         /// <param name="phoneNumber">不带国家码的手机号</param>
115         /// <param name="templId">模板 id</param>
116         /// <param name="templParams">模板参数列表,如模板 {1}...{2}...{3},那么需要带三个参数</param>
117         /// <param name="sign">短信签名,如果使用默认签名,该字段可缺省</param>
118         /// <param name="extend">扩展码,可填空</param>
119         /// <param name="ext">服务端原样返回的参数,可填空</param>
120         /// <returns>SmsSingleSenderResult</returns>
121         public SmsSingleSenderResult SendWithParam(string phoneNumber, int templId, List<string> templParams, string sign = "", string extend = "", string ext = "")
122         {
123             long random = util.GetRandom();
124             long curTime = util.GetCurTime();
125 
126             // 按照协议组织 post 请求包体
127             JObject tel = new JObject();
128             tel.Add("nationcode", SmsSenderUtil.nationCode);
129             tel.Add("mobile", phoneNumber);
130             JObject data = new JObject();
131             data.Add("tel", tel);
132             data.Add("sig", util.CalculateSigForTempl(appkey, random, curTime, phoneNumber));
133             data.Add("tpl_id", templId);
134             data.Add("params", util.SmsParamsToJSONArray(templParams));
135             data.Add("sign", sign);
136             data.Add("time", curTime);
137             data.Add("extend", extend);
138             data.Add("ext", ext);
139 
140             string wholeUrl = url + "?sdkappid=" + appId + "&random=" + random;
141             HttpWebRequest request = util.GetPostHttpConn(wholeUrl);
142             byte[] requestData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data));
143             request.ContentLength = requestData.Length;
144             Stream requestStream = request.GetRequestStream();
145             requestStream.Write(requestData, 0, requestData.Length);
146             requestStream.Close();
147 
148             // 接收返回包
149             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
150             Stream responseStream = response.GetResponseStream();
151             StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
152             string responseStr = streamReader.ReadToEnd();
153             streamReader.Close();
154             responseStream.Close();
155             SmsSingleSenderResult result;
156             if (HttpStatusCode.OK == response.StatusCode)
157             {
158                 result = util.ResponseStrToSingleSenderResult(responseStr);
159             }
160             else
161             {
162                 result = new SmsSingleSenderResult();
163                 result.result = -1;
164                 result.errmsg = "http error " + response.StatusCode + " " + responseStr;
165             }
166             return result;
167         }
168         #endregion
169     }
170     #endregion
171 
172     #region 单发结果
173     /// <summary>
174     /// 单发结果
175     /// </summary>
176     class SmsSingleSenderResult
177     {
178         /// <summary>
179         /// 错误码,0 表示成功(计费依据),非 0 表示失败
180         /// </summary>
181         public int result { set; get; }
182         /// <summary>
183         /// 错误消息,result 非 0 时的具体错误信息
184         /// </summary>
185         public string errmsg { set; get; }
186         /// <summary>
187         /// 用户的 session 内容,腾讯 server 回包中会原样返回
188         /// </summary>
189         public string ext { set; get; }
190         /// <summary>
191         /// 本次发送标识 id,标识一次短信下发记录
192         /// </summary>
193         public string sid { set; get; }
194         /// <summary>
195         /// 短信计费的条数
196         /// </summary>
197         public int fee { set; get; }
198         /// <summary>
199         /// ToString()
200         /// </summary>
201         /// <returns></returns>
202         public override string ToString()
203         {
204             return string.Format("SmsSingleSenderResult\\nresult {0}\\nerrMsg {1}\\next {2}\\nsid {3}\\nfee {4}", result, errmsg, ext, sid, fee);
205         }
206     }
207     #endregion
208 
209     #region 群发
210     /// <summary>
211     /// 群发
212     /// </summary>
213     class SmsMultiSender
214     {
215         #region 变量
216         /// <summary>
217         /// appId
218         /// </summary>
219         private int appId;
220         /// <summary>
221         /// appkey
222         /// </summary>
223         private string appkey;
224         /// <summary>
225         /// url
226         /// </summary>
227         private string url = "https://yun.tim.qq.com/v5/tlssmssvr/sendmultisms2";
228         /// <summary>
229         /// util
230         /// </summary>
231         private SmsSenderUtil util = new SmsSenderUtil();
232         #endregion
233 
234         #region 构造
235         /// <summary>
236         /// 构造
237         /// </summary>
238         /// <param name="sdkappid"></param>
239         /// <param name="appkey"></param>
240         public SmsMultiSender(int sdkappid, string appkey)
241         {
242             this.appId = sdkappid;
243             this.appkey = appkey;
244         }
245         #endregion
246 
247         #region 普通群发短信接口
248         /// <summary>
249         /// 普通群发短信接口,明确指定内容,如果有多个签名,请在内容中以【】的方式添加到信息内容中,否则系统将使用默认签名
250         ///【注意】 海外短信无群发功能
251         /// </summary>
252         /// <param name="type">短信类型,0 为普通短信,1 营销短信</param>
253         /// <param name="nationCode"></param>
254         /// <param name="phoneNumbers">不带国家码的手机号列表</param>
255         /// <param name="msg">信息内容,必须与申请的模板格式一致,否则将返回错误</param>
256         /// <param name="extend">扩展码,可填空</param>
257         /// <param name="ext">服务端原样返回的参数,可填空</param>
258         /// <returns>SmsMultiSenderResult</returns>
259         public SmsMultiSenderResult Send(SmsType type, List<string> phoneNumbers, string msg, string extend = "", string ext = "")
260         {
261             long random = util.GetRandom();
262             long curTime = util.GetCurTime();
263 
264             // 按照协议组织 post 请求包体
265             JObject data = new JObject();
266             data.Add("tel", util.PhoneNumbersToJSONArray(SmsSenderUtil.nationCode, phoneNumbers));
267             data.Add("type", (int)type);
268             data.Add("msg", msg);
Python调用腾讯API发送短信验证码

使用腾讯云发送短信API接口实现(完整流程)

接入腾讯云短信服务(史上最详细+该短信服务如何申请成功+发送短信验证码API讲解+相关错误分析)

腾讯云短息验证码接口

Python调用腾讯云短信接口

借助云开发实现小程序短信验证码的发送