-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmsNotificationService.cs
More file actions
305 lines (269 loc) · 11.5 KB
/
SmsNotificationService.cs
File metadata and controls
305 lines (269 loc) · 11.5 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
using System;
using System.Configuration;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;
using System.Collections.Generic;
namespace Library_Final
{
/// <summary>
/// SMS Notification Service using Semaphore API (Philippine SMS provider)
/// Sign up at: https://semaphore.co/
/// </summary>
public static class SmsNotificationService
{
private static string connectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;Initial Catalog=LibraryDB;Integrated Security=True;Encrypt=True;Trust Server Certificate=True;";
// Add to App.config: <add key="SemaphoreApiKey" value="YOUR_API_KEY_HERE"/>
private static string apiKey = ConfigurationManager.AppSettings["SemaphoreApiKey"];
private static string senderName = "CGC Library"; // Max 11 characters
/// <summary>
/// Initialize SMS tracking table
/// </summary>
public static void InitializeSmsTracking()
{
try
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
string createTable = @"
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='SmsNotificationLog' AND xtype='U')
CREATE TABLE SmsNotificationLog (
LogID INT IDENTITY(1,1) PRIMARY KEY,
ClientID INT NOT NULL,
PhoneNumber NVARCHAR(20),
RecipientName NVARCHAR(255),
NotificationType NVARCHAR(100),
Message NVARCHAR(500),
SentDate DATETIME DEFAULT GETDATE(),
Status NVARCHAR(50),
ErrorMessage NVARCHAR(MAX) NULL,
ApiResponse NVARCHAR(MAX) NULL
)";
using (SqlCommand cmd = new SqlCommand(createTable, con))
{
cmd.ExecuteNonQuery();
}
// Add phone number column to AddStudentAcc if not exists
string addPhoneColumn = @"
IF NOT EXISTS (SELECT * FROM sys.columns
WHERE object_id = OBJECT_ID('AddStudentAcc')
AND name = 'PhoneNumber')
BEGIN
ALTER TABLE AddStudentAcc ADD PhoneNumber NVARCHAR(20) NULL
END";
using (SqlCommand cmd = new SqlCommand(addPhoneColumn, con))
{
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error initializing SMS tracking: {ex.Message}");
}
}
/// <summary>
/// Send SMS via Semaphore API
/// </summary>
public static async Task<bool> SendSmsAsync(string phoneNumber, string message)
{
if (string.IsNullOrEmpty(apiKey))
{
Console.WriteLine("⚠️ Semaphore API Key not configured. Add it to App.config");
return false;
}
try
{
using (HttpClient client = new HttpClient())
{
var values = new Dictionary<string, string>
{
{ "apikey", apiKey },
{ "number", FormatPhoneNumber(phoneNumber) },
{ "message", message },
{ "sendername", senderName }
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("https://api.semaphore.co/api/v4/messages", content);
string responseString = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"✅ SMS sent to {phoneNumber}");
return true;
}
else
{
Console.WriteLine($"❌ SMS failed: {responseString}");
return false;
}
}
}
catch (Exception ex)
{
Console.WriteLine($"❌ SMS error: {ex.Message}");
return false;
}
}
/// <summary>
/// Format phone number for Philippine format
/// </summary>
private static string FormatPhoneNumber(string phone)
{
// Remove spaces, dashes, parentheses
phone = phone.Replace(" ", "").Replace("-", "").Replace("(", "").Replace(")", "");
// Convert to international format
if (phone.StartsWith("09"))
phone = "+63" + phone.Substring(1);
else if (phone.StartsWith("9"))
phone = "+63" + phone;
else if (!phone.StartsWith("+63"))
phone = "+63" + phone;
return phone;
}
/// <summary>
/// Log SMS notification
/// </summary>
private static void LogSmsNotification(
int clientID,
string phoneNumber,
string name,
string notificationType,
string message,
string status,
string errorMessage = null,
string apiResponse = null)
{
try
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
string query = @"
INSERT INTO SmsNotificationLog
(ClientID, PhoneNumber, RecipientName, NotificationType, Message, Status, ErrorMessage, ApiResponse)
VALUES
(@ClientID, @Phone, @Name, @Type, @Message, @Status, @Error, @Response)";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@ClientID", clientID);
cmd.Parameters.AddWithValue("@Phone", phoneNumber ?? "");
cmd.Parameters.AddWithValue("@Name", name ?? "");
cmd.Parameters.AddWithValue("@Type", notificationType ?? "");
cmd.Parameters.AddWithValue("@Message", message ?? "");
cmd.Parameters.AddWithValue("@Status", status ?? "");
cmd.Parameters.AddWithValue("@Error", (object)errorMessage ?? DBNull.Value);
cmd.Parameters.AddWithValue("@Response", (object)apiResponse ?? DBNull.Value);
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error logging SMS: {ex.Message}");
}
}
/// <summary>
/// Check if SMS was already sent today
/// </summary>
private static bool WasSmsSentToday(int clientID, string notificationType)
{
try
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
string query = @"
SELECT COUNT(*)
FROM SmsNotificationLog
WHERE ClientID = @ClientID
AND NotificationType = @Type
AND CAST(SentDate AS DATE) = CAST(GETDATE() AS DATE)
AND Status = 'Success'";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@ClientID", clientID);
cmd.Parameters.AddWithValue("@Type", notificationType);
int count = (int)cmd.ExecuteScalar();
return count > 0;
}
}
}
catch
{
return false;
}
}
/// <summary>
/// Send SMS for overdue books
/// </summary>
public static async Task<int> SendOverdueSmsNotificationsAsync()
{
int successCount = 0;
try
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
string query = @"
SELECT
i.ClientID,
i.StudentName,
i.BookTitle,
i.Penalty,
a.PhoneNumber
FROM IssueBooks i
INNER JOIN AddStudentAcc a ON i.ClientID = a.ClientID
WHERE
i.Status = 'Overdue'
AND i.OverdueDays = 1
AND a.PhoneNumber IS NOT NULL
AND a.PhoneNumber != ''";
using (SqlCommand cmd = new SqlCommand(query, con))
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
int clientID = Convert.ToInt32(reader["ClientID"]);
string phone = reader["PhoneNumber"].ToString();
string name = reader["StudentName"].ToString();
string bookTitle = reader["BookTitle"].ToString();
decimal penalty = Convert.ToDecimal(reader["Penalty"]);
// Check if already sent today
if (WasSmsSentToday(clientID, "Overdue SMS"))
continue;
// SMS must be max 160 characters for single message
string message = $"CGC Library Alert: '{bookTitle}' is OVERDUE. Penalty: P{penalty:N2}. Return ASAP to avoid more charges.";
bool sent = await SendSmsAsync(phone, message);
LogSmsNotification(
clientID,
phone,
name,
"Overdue SMS",
message,
sent ? "Success" : "Failed"
);
if (sent) successCount++;
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error sending overdue SMS: {ex.Message}");
}
return successCount;
}
/// <summary>
/// Send all SMS notifications
/// </summary>
public static async Task CheckAndSendAllSmsAsync()
{
Console.WriteLine($"[{DateTime.Now}] Running SMS notification checks...");
InitializeSmsTracking();
int overdueCount = await SendOverdueSmsNotificationsAsync();
Console.WriteLine($"[{DateTime.Now}] Completed: {overdueCount} overdue SMS sent");
}
}
}