Skip to content

Instantly share code, notes, and snippets.

@hevans90
Last active June 10, 2021 12:32
Show Gist options
  • Save hevans90/e88e60ad9ede0222bac98ae84a9b9cb7 to your computer and use it in GitHub Desktop.
Save hevans90/e88e60ad9ede0222bac98ae84a9b9cb7 to your computer and use it in GitHub Desktop.
something
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Data.SqlClient;
using System.Net.Http;
using System.Threading.Tasks;
using Dapper;
using DigitalCoders.Domain;
using DigitalCoders.CustomRepository;
using DigitalCoders.WhatsAppProviderAPI.Domain;
namespace DigitalCoders.InnovationFactory.Configuration
{
public class DigitalCodersCustomInnovationFactoryConfigurationRepository : ICustomConfigurationRepository
{
/* skip these */
public bool GetBoolParameter(Parameters param)
{
throw new NotImplementedException();
}
public int GetIntParameter(Parameters param)
{
throw new NotImplementedException();
}
public string GetStringParameter(Parameters param)
{
throw new NotImplementedException();
}
public TimeSpan GetTimeSpanParameter(Parameters param)
{
throw new NotImplementedException();
}
/* skip these */
}
}
namespace DigitalCoders.Domain
{
public interface IWhatsAppProviderApi
{
Task<Response> SendWhatsApp(string text, string mobileNumber, int? WhatsApp);
}
public enum WhatsAppState : byte
{
ToSend = 0,
Sent = 1,
SendError = 2,
RetrySend = 3
}
}
namespace DigitalCoders.CustomRepository
{
public enum Parameters
{
WhatsAppProviderEndpoint,
WhatsAppProviderApikey,
WhatsAppProviderPartnerID,
WhatsAppProviderShortcode
}
public interface ICustomConfigurationRepository
{
string GetStringParameter(Parameters param);
bool GetBoolParameter(Parameters param);
int GetIntParameter(Parameters param);
TimeSpan GetTimeSpanParameter(Parameters param);
}
}
namespace DigitalCoders.WhatsAppProviderAPI.Domain
{
public class Request
{
public string Apikey { get; set; }
public string PartnerID { get; set; }
public string Message { get; set; }
public string Shortcode { get; set; }
public string Mobile { get; set; }
}
public class Response
{
[JsonProperty("response-code")]
public string Code { get; set; }
[JsonProperty("response-description")]
public string Description { get; set; }
public string Mobile { get; set; }
public string Messageid { get; set; }
public string Networkid { get; set; }
public Error Errors { get; set; }
public List<Response> Responses { get; set; }
}
public class Error
{
public string Apikey = "";
public string PartnerID = "";
public string Message = "";
public string Shortcode = "";
public string Mobile = "";
}
}
namespace DigitalCoders.WhatsAppProviderAPI
{
public class WhatsAppProviderApi : IWhatsAppProviderApi
{
private readonly string WhatsAppProviderEndpoint;
private readonly string WhatsAppProviderApikey;
private readonly string WhatsAppProviderPartnerID;
private readonly string WhatsAppProviderShortcode;
private readonly HttpClient _client;
private readonly string Connectionstring;
public WhatsAppProviderApi(ICustomConfigurationRepository configurationRepository, HttpClient client, string connectionString)
{
WhatsAppProviderEndpoint = configurationRepository.GetStringParameter(Parameters.WhatsAppProviderEndpoint);
WhatsAppProviderApikey = configurationRepository.GetStringParameter(Parameters.WhatsAppProviderApikey);
WhatsAppProviderPartnerID = configurationRepository.GetStringParameter(Parameters.WhatsAppProviderPartnerID);
WhatsAppProviderShortcode = configurationRepository.GetStringParameter(Parameters.WhatsAppProviderShortcode);
Connectionstring = connectionString;
_client = client;
}
public async Task<Response> SendWhatsApp(string text, string mobileNumber, int? WhatsAppId)
{
if (WhatsAppId != null)
{
try
{
var req = new Request()
{
Apikey = WhatsAppProviderApikey,
PartnerID = WhatsAppProviderPartnerID,
Message = text,
Shortcode = WhatsAppProviderShortcode,
Mobile = mobileNumber
};
StringContent content = new StringContent(JsonConvert.SerializeObject(req, new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver() }),
Encoding.UTF8, "application/json");
using (HttpResponseMessage response = await _client.PostAsync(WhatsAppProviderEndpoint, content))
{
var result = JsonConvert.DeserializeObject<Response>(await response.Content.ReadAsStringAsync());
if (result.Responses?.Any() == true)
await UpdateWhatsAppState(WhatsAppId.Value, WhatsAppState.Sent);
else
{
await UpdateWhatsAppState(WhatsAppId.Value, WhatsAppState.SendError);
}
return result;
}
}
catch (Exception ex)
{
await UpdateWhatsAppState(WhatsAppId.Value, WhatsAppState.SendError);
return new Response() { Code = "400" };
}
}
return new Response() { Code = "400", Description = "WhatsAppID is null" };
}
public async Task UpdateWhatsAppState(int WhatsAppID, WhatsAppState newState)
{
using (SqlConnection connection = new SqlConnection(Connectionstring))
{
await connection.ExecuteAsync(@"UPDATE Messages.RecipientsWhatsApp SET IdSendState = @newState WHERE IdWhatsApp = @WhatsAppID", new { WhatsAppID, newState });
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment