-
Notifications
You must be signed in to change notification settings - Fork 4
/
TwilioLib.pas
100 lines (85 loc) · 2.8 KB
/
TwilioLib.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
{======================================================
Author Marcus Fernström
Contributor Thaddy de Koning
License LGPL3
Version 0.4
GitHub https://github.com/MFernstrom/TwilioLib
=======================================================}
unit TwilioLib;
{$mode objfpc}{$H+}{$notes off}
interface
uses
Classes, SysUtils, fphttpclient, fpjson, jsonparser, opensslsockets;
type
ITwilio = interface
['{7C5DF745-E72C-4652-9FB1-94992F181207}']
procedure send_sms(const from_number, to_number, message: String;var Output: TStrings);
end;
{ TTwilio }
TTwilio = class(TInterfacedObject, ITwilio)
private
account_sid, auth_token, url : String;
public
constructor create(const sid, token : String);
destructor destroy;override;
procedure send_sms(const from_number, to_number, message: String;var Output: TStrings);
end;
ETWilioException = Class(Exception);
implementation
constructor TTwilio.create(const sid, token: String );
begin
Inherited create;
account_sid := sid;
auth_token := token;
url := 'https://api.twilio.com/2010-04-01/Accounts/' + sid + '/Messages.json';
end;
destructor TTwilio.Destroy;
begin
inherited Destroy;
end;
procedure TTwilio.send_sms(const from_number, to_number, message: String;
var Output: TStrings);
var
postVars: TStringlist;
responseData: TStringStream;
pos: Integer = 0;
jObject : TJSONObject = nil;
begin
// Check if the output variable has been initialized
if Assigned(Output) then
begin
Output.Clear;
postVars := TStringlist.Create;
responseData := TStringStream.Create('');
try
// Prep resultVar
Output.CommaText := 'sid=,date_created=,date_updated=,date_sent=,account_sid=,to=,from=,body=,status=,num_segments=,num_media=,direction=,api_version=,price=,price_unit=,error_code=,error_message=,uri=,raw=';
// postVars holds the API data needed, to/from/body
postVars.Add('To=' + to_number);
postVars.Add('From=' + from_number);
postVars.Add('Body=' + message);
// Make the api call
With TFPHttpClient.Create(Nil) do
try
UserName := account_sid;
Password := auth_token;
FormPost(url, postVars, responseData);
finally
Free;
end;
jObject := GetJSON( responseData.DataString ) as TJSONObject;
for pos := 0 to Output.Count-1 do
if jObject.Get(Output.Names[pos]) <> Null then
Output.Values[Output.Names[pos]] := jObject.Get(Output.Names[pos]);
Output.Values['raw'] := responseData.DataString;
finally
responseData.Free;
jObject.Free;
postVars.Free;
end
end else
raise ETWilioException.Create('No output assigned') at
get_caller_addr(get_frame),
get_caller_frame(get_frame);;
end;
end.