Skip to content

Commit a0246a9

Browse files
committed
verify and check
1 parent 1a9b812 commit a0246a9

6 files changed

+262
-12
lines changed

TextFlow/TextFlow.cs

+58-7
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,80 @@ public class TextFlowClient
1010
public TextFlowClient(string apiKey)
1111
{
1212
if (apiKey == null) throw new ArgumentNullException(nameof(apiKey));
13+
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + apiKey);
1314
this.apiKey = apiKey;
1415
}
1516
public void useKey(string apiKey)
1617
{
1718
if (apiKey == null) throw new ArgumentNullException(nameof(apiKey));
1819
this.apiKey = apiKey;
20+
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + apiKey);
1921
}
20-
public async Task<TextFlowSendMessageResult> sendSMS(string recipient, string text)
22+
public async Task<TextFlowSendMessageResult> SendSMS(string phoneNumber, string text)
2123
{
22-
if (recipient == null || recipient.Length == 0) return new TextFlowSendMessageResult(false, 400, "You have not specified the recipient. ", new TextFlowSendMessageData());
24+
if (phoneNumber == null || phoneNumber.Length == 0) return new TextFlowSendMessageResult(false, 400, "You have not specified the recipient. ", new TextFlowSendMessageData());
2325
if (text == null || text.Length == 0) return new TextFlowSendMessageResult(false, 400, "You have not specified the message body. ", new TextFlowSendMessageData());
2426
if (apiKey == null || apiKey.Length == 0) return new TextFlowSendMessageResult(false, 400, "You have not specified the API key. Specify it by calling the useKey function. ", new TextFlowSendMessageData());
2527
var data = new Dictionary<string, string>()
2628
{
27-
{"recipient", recipient },
28-
{"text", text },
29-
{"apiKey", apiKey }
29+
{"phone_number", phoneNumber },
30+
{"text", text }
3031
};
3132
var content = new FormUrlEncodedContent(data);
32-
var response = await httpClient.PostAsync("https://textflow.me/messages/send", content);
33+
var response = await httpClient.PostAsync("https://textflow.me/api/send-sms", content);
3334
var responseString = await response.Content.ReadAsStringAsync();
34-
3535
return new TextFlowSendMessageResult(responseString);
3636
}
37+
public async Task<TextFlowVerifyPhoneResult> SendVerificationSMS(string phoneNumber, int seconds)
38+
{
39+
return await SendVerificationSMS(phoneNumber, null, seconds);
40+
}
41+
public async Task<TextFlowVerifyPhoneResult> SendVerificationSMS(string phoneNumber, string serviceName)
42+
{
43+
return await SendVerificationSMS(phoneNumber, serviceName, null);
44+
}
45+
public async Task<TextFlowVerifyPhoneResult> SendVerificationSMS(string phoneNumber)
46+
{
47+
return await SendVerificationSMS(phoneNumber, null, null);
48+
}
49+
public async Task<TextFlowVerifyPhoneResult> SendVerificationSMS(string phoneNumber, string? serviceName, int? seconds)
50+
{
51+
if (phoneNumber == null || phoneNumber.Length == 0) return new TextFlowVerifyPhoneResult(false, 400, "You have not specified the phone number to verify. ", new TextFlowVerifyPhoneData());
52+
if (apiKey == null || apiKey.Length == 0) return new TextFlowVerifyPhoneResult(false, 400, "You have not specified the API key. Specify it by calling the useKey function. ", new TextFlowVerifyPhoneData());
53+
var data = new Dictionary<string, string>()
54+
{
55+
{"phone_number", phoneNumber },
56+
{"test", "true" }
57+
};
58+
if (serviceName != null)
59+
{
60+
data.Add("service_name", serviceName);
61+
}
62+
if(seconds != null)
63+
{
64+
data.Add("seconds", seconds.Value.ToString());
65+
}
66+
var content = new FormUrlEncodedContent(data);
67+
var response = await httpClient.PostAsync("https://textflow.me/api/send-code", content);
68+
var responseString = await response.Content.ReadAsStringAsync();
69+
return new TextFlowVerifyPhoneResult(responseString);
70+
}
71+
72+
public async Task<TextFlowVerifyCodeResult> VerifyCode(string phone_number, string code)
73+
{
74+
if (phone_number == null || phone_number.Length == 0) return new TextFlowVerifyCodeResult(false, 400, "You have not specified the recipient. ");
75+
if (code == null || code.Length == 0) return new TextFlowVerifyCodeResult(false, 400, "You have not specified the code. ");
76+
if (apiKey == null || apiKey.Length == 0) return new TextFlowVerifyCodeResult(false, 400, "You have not specified the API key. Specify it by calling the useKey function. ");
77+
var data = new Dictionary<string, string>()
78+
{
79+
{"phone_number", phone_number },
80+
{"code", code },
81+
{"test", "true" }
82+
};
83+
var content = new FormUrlEncodedContent(data);
84+
var response = await httpClient.PostAsync("https://textflow.me/api/verify-code", content);
85+
var responseString = await response.Content.ReadAsStringAsync();
86+
return new TextFlowVerifyCodeResult(responseString);
87+
}
3788
}
3889
}

TextFlow/TextFlow.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<PackageTags>textflow;sms api;api client;api;text message;simple sms;sms verification;sms</PackageTags>
88
<Description>A package that enables you to send SMS by using TextFlow API. </Description>
99
<PackageId>TextFlow</PackageId>
10-
<Version>1.1.4</Version>
10+
<Version>1.2.0</Version>
1111
<Authors>TextFlow</Authors>
1212
<Company>TextFlow</Company>
1313
<Product>TextFlow</Product>

TextFlow/TextFlowSendMessageResult.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ namespace TextFlow
1010
{
1111
public class TextFlowSendMessageResult
1212
{
13-
public bool Ok { get; set; }
14-
public int Status { get; set; }
15-
public string Message { get; set; }
16-
public TextFlowSendMessageData Data { get; set; }
13+
public bool Ok { get; }
14+
public int Status { get; }
15+
public string Message { get; }
16+
public TextFlowSendMessageData Data { get; }
17+
18+
public String Json { get; }
1719
internal TextFlowSendMessageResult(bool ok, int status, string message, TextFlowSendMessageData data)
1820
{
21+
Json = "";
1922
Ok = ok;
2023
Status = status;
2124
Message = message;
@@ -25,6 +28,7 @@ public TextFlowSendMessageResult(string result)
2528
{
2629
try
2730
{
31+
Json = result.ToString();
2832
var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(result), new System.Xml.XmlDictionaryReaderQuotas());
2933
var root = XElement.Load(jsonReader);
3034

@@ -70,6 +74,8 @@ public TextFlowSendMessageResult(string result)
7074
{
7175
Console.WriteLine(ex.Message);
7276
}
77+
if(Json==null)
78+
Json = "";
7379
Ok = false;
7480
Status = 500;
7581
Message = "Server error. ";

TextFlow/TextFlowVerifyCodeResult.cs

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.Serialization.Json;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Xml.Linq;
8+
using System.Xml.XPath;
9+
namespace TextFlow
10+
{
11+
public class TextFlowVerifyCodeResult
12+
{
13+
public bool Ok { get; }
14+
public int Status { get; }
15+
public string Message { get; }
16+
public bool Valid { get; }
17+
public string? ValidCode { get; }
18+
public long? Expires { get; }
19+
public String Json { get; }
20+
internal TextFlowVerifyCodeResult(bool ok, int status, string message)
21+
{
22+
Json = "";
23+
Ok = ok;
24+
Status = status;
25+
Message = message;
26+
Valid = false;
27+
}
28+
public TextFlowVerifyCodeResult(string result)
29+
{
30+
try
31+
{
32+
Json = result.ToString();
33+
var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(result), new System.Xml.XmlDictionaryReaderQuotas());
34+
var root = XElement.Load(jsonReader);
35+
36+
var ok = root.XPathSelectElement("//ok");
37+
var status = root.XPathSelectElement("//status");
38+
var message = root.XPathSelectElement("//message");
39+
var valid = root.XPathSelectElement("//valid");
40+
var validCode = root.XPathSelectElement("//valid_code");
41+
var expires = root.XPathSelectElement("//expires");
42+
jsonReader.Close();
43+
if (ok == null || status == null || message == null || valid==null)
44+
{
45+
Ok = false;
46+
Status = 500;
47+
Message = "Server error. ";
48+
Valid = false;
49+
return;
50+
}
51+
Ok = bool.Parse(ok.Value.ToString());
52+
Status = int.Parse(status.Value);
53+
Message = message.Value;
54+
Valid = bool.Parse(valid.Value.ToString());
55+
if (validCode != null)
56+
{
57+
ValidCode = validCode.Value;
58+
}
59+
if (expires != null)
60+
{
61+
Expires = long.Parse(expires.Value);
62+
}
63+
return;
64+
}
65+
catch(Exception ex)
66+
{
67+
Console.WriteLine(ex.Message);
68+
}
69+
if(Json == null)
70+
{
71+
Json = "";
72+
}
73+
Ok = false;
74+
Status = 500;
75+
Message = "Server error. ";
76+
Valid = false;
77+
78+
}
79+
}
80+
}

TextFlow/TextFlowVerifyPhoneData.cs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace TextFlow
8+
{
9+
public class TextFlowVerifyPhoneData
10+
{
11+
public string VerificationCode { get; }
12+
public long Expires { get; }
13+
public string MessageText { get; }
14+
public TextFlowVerifyPhoneData(string verificationCode, long expires, string messageText)
15+
{
16+
VerificationCode = verificationCode;
17+
Expires = expires;
18+
MessageText = messageText;
19+
}
20+
public TextFlowVerifyPhoneData()
21+
{
22+
VerificationCode = "";
23+
Expires = 0;
24+
MessageText = "";
25+
}
26+
27+
}
28+
}

TextFlow/TextFlowVerifyPhoneResult.cs

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.Serialization.Json;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Xml.Linq;
8+
using System.Xml.XPath;
9+
namespace TextFlow
10+
{
11+
public class TextFlowVerifyPhoneResult
12+
{
13+
public bool Ok { get; set; }
14+
public int Status { get; set; }
15+
public string Message { get; set; }
16+
public TextFlowVerifyPhoneData Data { get; set; }
17+
public String Json { get; }
18+
internal TextFlowVerifyPhoneResult(bool ok, int status, string message, TextFlowVerifyPhoneData data)
19+
{
20+
Json = "";
21+
Ok = ok;
22+
Status = status;
23+
Message = message;
24+
Data = data;
25+
}
26+
public TextFlowVerifyPhoneResult(string result)
27+
{
28+
try
29+
{
30+
Json = result.ToString();
31+
var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(result), new System.Xml.XmlDictionaryReaderQuotas());
32+
var root = XElement.Load(jsonReader);
33+
34+
var ok = root.XPathSelectElement("//ok");
35+
var status = root.XPathSelectElement("//status");
36+
var message = root.XPathSelectElement("//message");
37+
var verificationCode = root.XPathSelectElement("//data/verification_code");
38+
var expires = root.XPathSelectElement("//data/expires");
39+
var messageText = root.XPathSelectElement("//data/message_text");
40+
jsonReader.Close();
41+
if (ok == null || status == null || message == null)
42+
{
43+
Ok = false;
44+
Status = 500;
45+
Message = "Server error. ";
46+
Data = new TextFlowVerifyPhoneData();
47+
return;
48+
}
49+
Ok = bool.Parse(ok.Value.ToString());
50+
Status = int.Parse(status.Value);
51+
Message = message.Value;
52+
if (Ok)
53+
{
54+
if(verificationCode==null || expires==null || message==null)
55+
{
56+
Ok = false;
57+
Status = 500;
58+
Message = "Server error. ";
59+
Data = new TextFlowVerifyPhoneData();
60+
return;
61+
}
62+
Data = new TextFlowVerifyPhoneData(verificationCode.Value, long.Parse(expires.Value), messageText.Value);
63+
}
64+
else
65+
{
66+
Data = new TextFlowVerifyPhoneData();
67+
}
68+
return;
69+
}
70+
catch(Exception ex)
71+
{
72+
Console.WriteLine(ex.Message);
73+
}
74+
if(Json == null)
75+
{
76+
Json = "";
77+
}
78+
Ok = false;
79+
Status = 500;
80+
Message = "Server error. ";
81+
Data = new TextFlowVerifyPhoneData();
82+
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)