Initial Commit ...

This commit is contained in:
2024-01-25 04:51:13 +03:30
commit 2b05450746
21 changed files with 758 additions and 0 deletions
+148
View File
@@ -0,0 +1,148 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;
using xCommons.Extensions;
using xExceptions.Constants;
using xMessageService.Interfaces;
using xMessageService.Models;
namespace xMessageService.Providers {
public partial class XMailService : IXMailService {
private SmtpClient smtpClient;
private readonly string defaultProvider;
private readonly IDictionary<string, XMailConfiguration> configurations;
public XMailService (
string defaultProvider,
IDictionary<string, XMailConfiguration> configurations
) {
this.defaultProvider = defaultProvider;
this.configurations = configurations;
//
// Get instance of SMTP Client ...
PrepareSmtpClient ();
}
public Task SendMailAsync (
XMessage message,
string provider = null
) {
//
// Run Send Mail Asynchronous ...
return Task.Run (() => {
SendMail (message, provider);
});
}
#region Private ...
/// <summary>
/// Validate and Retrieve Provider Configuration for Service
/// </summary>
/// <param name="provider"></param>
/// <returns></returns>
private XMailConfiguration GetConfiguration (string provider = null) {
//
if (provider.IsNullOrEmpty ()) {
provider = defaultProvider;
}
//
if (!configurations.Keys.Contains (provider)) {
XException.MessageServiceInitialFailed.Throw ();
}
//
return configurations[provider];
}
/// <summary>
/// Prepare SMTP Client by Using Specified/Default Provider
/// </summary>
/// <param name="provider"></param>
private void PrepareSmtpClient (string provider = null) {
//
var config = GetConfiguration (provider);
if (config.IsNull()) {
return;
}
//
smtpClient = new SmtpClient () {
Host = config.Host,
Port = config.Port,
EnableSsl = config.EnableSsl,
UseDefaultCredentials = false,
Credentials = new NetworkCredential (
config.Username,
config.Password)
};
}
/// <summary>
/// Prepare Mail Message With Specified/Default Provider
/// </summary>
/// <param name="message"></param>
/// <param name="provider"></param>
/// <returns></returns>
private MailMessage PrepareMailMessage (
XMessage message,
string provider = null
) {
//
if (message == null ||
message.Message.IsNullOrEmpty () ||
message.Recievers.Count == 0) {
XException.InvalidMessage.Throw ();
}
//
var config = GetConfiguration (provider);
var mailMessage = new MailMessage () {
From = new MailAddress (config.Username),
Subject = config.Title,
Body = message.Message
};
//
foreach (var reciever in message.Recievers) {
mailMessage.To.Add (reciever);
}
//
return mailMessage;
}
/// <summary>
/// Send Mail Using Specified/Default Provider
/// </summary>
/// <param name="message"></param>
/// <param name="provider"></param>
private void SendMail (
XMessage message,
string provider = null
) {
//
if (smtpClient.IsNull()) {
//
Console.WriteLine ($"SMTP Error: MessageServiceInitialFailed ...");
XException.MessageServiceInitialFailed.Throw ();
}
//
var mailMessage = PrepareMailMessage (message, provider);
//
try {
smtpClient.Send (mailMessage);
} catch (Exception ex) {
//
Console.WriteLine ($"SMTP Error: {ex.Message}");
XException.MessageServiceInitialFailed.Throw ();
}
}
#endregion
}
}
+118
View File
@@ -0,0 +1,118 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using xCommons.Extensions;
using xExceptions.Constants;
using xMessageService.Interfaces;
using xMessageService.Models;
namespace xMessageService.Providers {
public partial class XMessageProvider : IXMessageProvider {
private readonly XMessageConfiguration configuration;
private readonly ILogger<XMessageProvider> logger;
private readonly IXSmsService smsService;
private readonly IXMailService mailService;
public XMessageProvider (
XMessageConfiguration configuration,
ILogger<XMessageProvider> logger,
IXSmsService smsService,
IXMailService mailService
) {
this.configuration = configuration;
this.logger = logger;
this.smsService = smsService;
this.mailService = mailService;
}
/// <summary>
/// Send a Mail Message
/// </summary>
/// <param name="message"></param>
/// <param name="provider"></param>
/// <param name="throwException"></param>
/// <param name="exception"></param>
/// <returns></returns>
public async Task SendMailAsync (
XMessage message,
string provider = null,
bool throwException = true,
Exception exception = null
) {
//
if (exception.IsNull ()) {
exception = XException.ActionFailed.ToException ();
}
//
try {
await mailService.SendMailAsync (message, provider);
} catch (Exception ex) {
//
logger.LogError ($"Exeption: {ex.Message}");
//
if (throwException) {
throw exception;
}
}
}
/// <summary>
/// Send SMS Message
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public async Task SendSmsAsync (
XMessage message,
string provider = null,
bool throwException = true,
Exception exception = null
) {
//
if (exception.IsNull ()) {
exception = XException.ActionFailed.ToException ();
}
//
try {
await smsService.SendSmsAsync (message, provider);
} catch {
if (throwException) {
throw exception;
}
}
}
/// <summary>
/// Check Message Provider is Ready or not
/// </summary>
/// <param name="checkMailService"></param>
/// <param name="checkSmsService"></param>
/// <returns></returns>
public bool IsReady (
bool? checkMailService = false,
bool? checkSmsService = false
) {
//
var result = true;
//
// Check Mail Service ...
if (checkMailService.HasValue &&
checkMailService.GetValueOrDefault ()) {
result = result && mailService != null;
}
//
// Check Sms Service ...
if (checkSmsService.HasValue &&
checkSmsService.GetValueOrDefault ()) {
result = result && smsService != null;
}
//
return result;
}
}
}
+181
View File
@@ -0,0 +1,181 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using PhoneNumbers;
using RestSharp;
using xCommons.Extensions;
using xExceptions.Constants;
using xMessageService.Interfaces;
using xMessageService.Models;
namespace xMessageService.Providers {
public partial class XSmsService : IXSmsService {
private string serviceUrl;
private readonly string defaultProvider;
private readonly IDictionary<string, XSmsConfiguration> configurations;
public XSmsService (
string defaultProvider,
IDictionary<string, XSmsConfiguration> configurations
) {
this.defaultProvider = defaultProvider;
this.configurations = configurations;
//
PrepareServiceUrl ();
}
public Task SendSmsAsync (
XMessage message,
string provider = null
) {
//
// Run Send SMS Asynchronous ...
return Task.Run (() => {
SendSMS (message, provider);
});
}
#region Private ...
/// <summary>
/// Prepare Service Url for Specified/Default Provider
/// </summary>
/// <param name="provider"></param>
private void PrepareServiceUrl (string provider = null) {
//
if (provider.IsNullOrEmpty ()) {
provider = defaultProvider;
}
//
var config = GetConfiguration (provider);
if (config.IsNull ()) {
return;
}
//
serviceUrl = config.ServiceUrl;
serviceUrl += "&username=" + config.Username + "&password=" + config.Password + "&";
}
/// <summary>
/// Prepare Service url for Integrating XMessage by
/// using Specified/Default Provider Configurations
/// </summary>
/// <param name="message"></param>
/// <param name="provider"></param>
private void PrepareSmsMessage (
XMessage message,
string provider = null
) {
//
if (message == null ||
message.Message.IsNullOrEmpty () ||
message.Recievers.Count == 0) {
XException.InvalidMessage.Throw ();
}
//
// Validating Mobile Numbers ...
var phoneNumberUtil = PhoneNumbers.PhoneNumberUtil.GetInstance ();
var recieversList = "";
//
PrepareServiceUrl (provider);
//
foreach (var reciever in message.Recievers) {
try {
//
var mPhoneNumber = phoneNumberUtil.Parse (reciever, null);
if (phoneNumberUtil.IsValidNumber (mPhoneNumber)) {
//
// Format Phone Numbers to the '+989120000000' pattern ...
var recieverPhone = phoneNumberUtil.Format (mPhoneNumber, PhoneNumberFormat.E164);
if (recieversList.Length > 0) {
recieversList += ",";
}
recieversList += reciever;
}
} catch { }
}
//
var config = GetConfiguration ();
//
// Preparing All Content and Configurations for Sending Messages ...
serviceUrl += "to=" +
recieversList +
"&message=" +
message.Message +
"&from=" +
config.LineNumber +
"&flash=" +
false;
}
/// <summary>
/// Send a Message as SMS by Specified/Default Provider
/// </summary>
/// <param name="message"></param>
/// <param name="provider"></param>
private void SendSMS (
XMessage message,
string provider = null
) {
//
if (serviceUrl.IsNullOrEmpty ()) {
//
Console.WriteLine ($"SMS Error: MessageServiceInitialFailed ...");
XException.MessageServiceInitialFailed.Throw ();
}
//
try {
//
// preparing Service Url based on message data ...
PrepareSmsMessage (message, provider);
//
// Implementing Service Calling Using RestSharp Library ...
// We Select RestSharp because it supports Sync/Async Calls ...
var client = new RestClient (serviceUrl);
var request = new RestRequest (Method.POST);
//
// Send Request ...
var response = client.Execute (request);
} catch (Exception ex) {
Console.WriteLine ($"SMS Error: {ex.Message}");
XException.MessageServiceInitialFailed.Throw ();
}
}
/// <summary>
/// Validate and Retrieve Provider Configuration for Service
/// </summary>
/// <param name="provider"></param>
/// <returns></returns>
private XSmsConfiguration GetConfiguration (string provider = null) {
//
if (
provider.IsNullOrEmpty () ||
provider.ToNormalString () == "empty"
) {
//
provider = defaultProvider;
return null;
}
//
if (!configurations.Keys.Contains (provider)) {
XException.MessageServiceInitialFailed.Throw ();
}
//
return configurations[provider];
}
#endregion
}
}