利用阿里大于接口发短信(Delphi版)

阿里大于是阿里通信旗下产品,融合了三大运营商的通信能力,提供包括短信、语音、流量直充、私密专线、店铺手机号等个性化服务。每条四分五,价钱还算公道,经老农测试,响应速度非常快,基本上是秒到。官方文档提供了JAVA、.NET、PHP、Python、C/C++、NodeJS 等语言的 Demo,唯独没有 Dephi,但这也不能怪马云,毕竟 Delphi 实在太小众了。

   最近用 Delphi 写个 App,注册用户需要用到手机短信验证,于是找到的阿里大于,使用 Delphi 10.1 berlin 写了个简单的 Demo 并测试通过,现在交出代码:

 1

阿里大于是阿里通信旗下产品,融合了三大运营商的通信能力,提供包括短信、语音、流量直充、私密专线、店铺手机号等个性化服务。每条四分五,价钱还算公道,经老农测试,响应速度非常快,基本上是秒到。官方文档提供了JAVA、.NET、PHP、Python、C/C++、NodeJS 等语言的 Demo,唯独没有 Dephi,但这也不能怪马云,毕竟 Delphi 实在太小众了。

   最近用 Delphi 写个 App,注册用户需要用到手机短信验证,于是找到的阿里大于,使用 Delphi 10.1 berlin 写了个简单的 Demo 并测试通过,现在交出代码:

 1 /// 全能地图(QQ:64445322)
 2 /// 
 3 /// 利用阿里大于接口发短信
 4 /// 阿里大于网址:http://www.alidayu.com
 5 /// 阿里大于短信接口文档:https://api.alidayu.com/doc2/apiDetail.htm?apiId=25450
 6 /// 
 7 /// TOP分配给应用的AppKey
 8 /// AppSecret
 9 /// 接收手机号码
10 /// 短信签名,传入的短信签名必须是在阿里大于“管理中心-短信签名管理”中的可用签名
11 /// 短信模板ID
12 /// 短信模板变量,例如:{"code":"1234","product":"alidayu"}
13 /// 下发结果消息
14 /// 是否成功,True = 成功 ,false = 失败
15 function SendSMS(const AppKey, AppSecret, ReceiveNumber, FreeSignName, TemplateCode, TemplateContent: string; var ResultMsg: string): Boolean;
16 
17   // 签名算法:http://open.taobao.com/doc2/detail.htm?articleId=101617&docType=1&treeId=1
18   function MakeSign(const AParams: TStringList; const AppSecret: string): string;
19   var
20     I: Integer;
21     Data: string;
22   begin
23     // 参数排序
24     AParams.Sort;
25 
26     // 参数拼接
27     Data := '';
28     for I := 0 to AParams.Count - 1 do
29       Data := Data + AParams[I].Replace('=', '');
30 
31     // HMAC 算法
32     Result := THashMD5.GetHMAC(Data, AppSecret).ToUpper;
33   end;
34 
35 var
36   HTTP: TNetHTTPClient;
37   JsonObject: TJSONObject;
38   Params: TStringList;
39   Response: string;
40 begin
41   Result := False;
42 
43   HTTP := TNetHTTPClient.Create(nil);
44   Params := TStringList.Create();
45   try
46     Params.Values['app_key'] := AppKey;
47     Params.Values['format'] := 'json';
48     Params.Values['method'] := 'alibaba.aliqin.fc.sms.num.send';
49     Params.Values['sign_method'] := 'hmac';
50     Params.Values['timestamp'] := FormatDateTime('yyyy-MM-dd HH:mm:ss', Now);
51     Params.Values['v'] := '2.0';
52     Params.Values['sms_type'] := 'normal';
53     Params.Values['sms_free_sign_name'] := FreeSignName;
54     Params.Values['rec_num'] := ReceiveNumber;
55     Params.Values['sms_template_code'] := TemplateCode;
56     Params.Values['sms_param'] := TemplateContent;
57     Params.Values['sign'] := MakeSign(Params, AppSecret);
58 
59     HTTP.ContentType := 'application/x-www-form-urlencoded';
60     try
61       Response := HTTP.Post('https://eco.taobao.com/router/rest', Params).ContentAsString();
62     except
63       on E: Exception do
64       begin
65         ResultMsg := E.Message;
66         Exit;
67       end;
68     end;
69 
70     JsonObject := TJSONObject.ParseJSONValue(Response) as TJSONObject;
71     try
72       if JsonObject <> nil then
73       begin
74         if JsonObject.TryGetValue('alibaba_aliqin_fc_sms_num_send_response.result.success', ResultMsg) then
75           Result := ResultMsg.ToUpper = 'TRUE'
76         else if JsonObject.TryGetValue('error_response.msg', ResultMsg) then
77           Result := False;
78       end;
79 
80     finally
81       JsonObject.Free;
82     end;
83 
84   finally
85     HTTP.Free;
86     Params.Free;
87   end;
88 
89 end;

 有不少同学还在使用D7,不知道怎么用,稍微改改就可以了。

 1 function SendSMS(const AppKey, AppSecret, ReceiveNumber, FreeSignName, TemplateCode, TemplateContent: string; var ResultMsg: string): Boolean;
 2 
 3   function GetStringMD5(const AInPut: string): string;
 4   var
 5     MD5: TIdHashMessageDigest5;
 6     Digest: T4x4LongWordRecord;
 7   begin
 8     MD5 := TIdHashMessageDigest5.Create;
 9     try
10       Digest := MD5.HashValue(AInPut);
11       Result := MD5.AsHex(Digest);
12     finally
13       MD5.Free;
14     end;
15   end;
16 
17 // 签名算法:http://open.taobao.com/doc2/detail.htm?articleId=101617&docType=1&treeId=1
18   function MakeSign(const AParams: TStringList; const AppSecret: string): string;
19   var
20     I: Integer;
21     Data: string;
22   begin
23     // 参数排序
24     AParams.Sort;
25     // 参数拼接
26     Data := '';
27     for I := 0 to AParams.Count - 1 do
28       Data := Data + StringReplace(AParams[I], '=', '', [rfReplaceAll]);
29     // MD5 算法
30     Result := GetStringMD5(AppSecret + Data + AppSecret);
31   end;
32 
33 var
34   HTTP: TIdHTTP;
35   Params: TStringList;
36   Response: string;
37   JsonObject: ISuperObject;
38 begin
39   Result := False;
40 
41   HTTP := TIdHTTP.Create(nil);
42   Params := TStringList.Create();
43   try
44     Params.Values['app_key'] := AppKey;
45     Params.Values['format'] := 'json';
46     Params.Values['method'] := 'alibaba.aliqin.fc.sms.num.send';
47     Params.Values['sign_method'] := 'md5';
48     Params.Values['timestamp'] := FormatDateTime('yyyy-MM-dd HH:mm:ss', Now);
49     Params.Values['v'] := '2.0';
50     Params.Values['sms_type'] := 'normal';
51     Params.Values['sms_free_sign_name'] := UTF8Encode(FreeSignName);
52     Params.Values['rec_num'] := ReceiveNumber;
53     Params.Values['sms_template_code'] := TemplateCode;
54     Params.Values['sms_param'] := UTF8Encode(TemplateContent);
55     Params.Values['sign'] := MakeSign(Params, AppSecret);
56 
57     HTTP.HandleRedirects := True;
58     HTTP.Request.AcceptCharSet := 'utf-8';
59     HTTP.Request.ContentType := 'application/x-www-form-urlencoded';
60     try
61       Response := HTTP.Post('http://gw.api.taobao.com/router/rest', Params);
62     except
63       on E: Exception do
64       begin
65         ResultMsg := E.Message;
66         Exit;
67       end;
68     end;
69 
70     JsonObject := SO(Response);
71     if JsonObject <> nil then
72     begin
73       ResultMsg := JsonObject.S['alibaba_aliqin_fc_sms_num_send_response.result.success'];
74       if ResultMsg <> '' then
75         Result := UpperCase(ResultMsg) = 'TRUE'
76       else
77       begin
78         ResultMsg := JsonObject.S['error_response.msg'];
79         Result := False;
80       end;
81     end;
82 
83   finally
84     HTTP.Free;
85     Params.Free;
86   end;
87 
88 end;

 有不少同学还在使用D7,不知道怎么用,稍微改改就可以了。

 1 function SendSMS(const AppKey, AppSecret, ReceiveNumber, FreeSignName, TemplateCode, TemplateContent: string; var ResultMsg: string): Boolean;
 2 
 3   function GetStringMD5(const AInPut: string): string;
 4   var
 5     MD5: TIdHashMessageDigest5;
 6     Digest: T4x4LongWordRecord;
 7   begin
 8     MD5 := TIdHashMessageDigest5.Create;
 9     try
10       Digest := MD5.HashValue(AInPut);
11       Result := MD5.AsHex(Digest);
12     finally
13       MD5.Free;
14     end;
15   end;
16 
17 // 签名算法:http://open.taobao.com/doc2/detail.htm?articleId=101617&docType=1&treeId=1
18   function MakeSign(const AParams: TStringList; const AppSecret: string): string;
19   var
20     I: Integer;
21     Data: string;
22   begin
23     // 参数排序
24     AParams.Sort;
25     // 参数拼接
26     Data := '';
27     for I := 0 to AParams.Count - 1 do
28       Data := Data + StringReplace(AParams[I], '=', '', [rfReplaceAll]);
29     // MD5 算法
30     Result := GetStringMD5(AppSecret + Data + AppSecret);
31   end;
32 
33 var
34   HTTP: TIdHTTP;
35   Params: TStringList;
36   Response: string;
37   JsonObject: ISuperObject;
38 begin
39   Result := False;
40 
41   HTTP := TIdHTTP.Create(nil);
42   Params := TStringList.Create();
43   try
44     Params.Values['app_key'] := AppKey;
45     Params.Values['format'] := 'json';
46     Params.Values['method'] := 'alibaba.aliqin.fc.sms.num.send';
47     Params.Values['sign_method'] := 'md5';
48     Params.Values['timestamp'] := FormatDateTime('yyyy-MM-dd HH:mm:ss', Now);
49     Params.Values['v'] := '2.0';
50     Params.Values['sms_type'] := 'normal';
51     Params.Values['sms_free_sign_name'] := UTF8Encode(FreeSignName);
52     Params.Values['rec_num'] := ReceiveNumber;
53     Params.Values['sms_template_code'] := TemplateCode;
54     Params.Values['sms_param'] := UTF8Encode(TemplateContent);
55     Params.Values['sign'] := MakeSign(Params, AppSecret);
56 
57     HTTP.HandleRedirects := True;
58     HTTP.Request.AcceptCharSet := 'utf-8';
59     HTTP.Request.ContentType := 'application/x-www-form-urlencoded';
60     try
61       Response := HTTP.Post('http://gw.api.taobao.com/router/rest', Params);
62     except
63       on E: Exception do
64       begin
65         ResultMsg := E.Message;
66         Exit;
67       end;
68     end;
69 
70     JsonObject := SO(Response);
71     if JsonObject <> nil then
72     begin
73       ResultMsg := JsonObject.S['alibaba_aliqin_fc_sms_num_send_response.result.success'];
74       if ResultMsg <> '' then
75         Result := UpperCase(ResultMsg) = 'TRUE'
76       else
77       begin
78         ResultMsg := JsonObject.S['error_response.msg'];
79         Result := False;
80       end;
81     end;
82 
83   finally
84     HTTP.Free;
85     Params.Free;
86   end;
87 
88 end;

你可能感兴趣的:(Delphi,python)