in order to use EF7 commands
This commit is contained in:
140
Yavsc/Services/MessageServices.cs
Executable file
140
Yavsc/Services/MessageServices.cs
Executable file
@ -0,0 +1,140 @@
|
||||
|
||||
using System.Threading.Tasks;
|
||||
using MailKit.Net.Smtp;
|
||||
using MimeKit;
|
||||
using MailKit.Security;
|
||||
using System;
|
||||
using Yavsc.Models.Messaging;
|
||||
using Microsoft.AspNet.Identity;
|
||||
using Yavsc.Models;
|
||||
using Yavsc.Models.Google.Messaging;
|
||||
using System.Collections.Generic;
|
||||
using Yavsc.Models.Haircut;
|
||||
using Yavsc.Interfaces.Workflow;
|
||||
using System.Linq;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Yavsc.Server.Helpers;
|
||||
|
||||
namespace Yavsc.Services
|
||||
{
|
||||
// This class is used by the application to send Email and SMS
|
||||
// when you turn on two-factor authentication in ASP.NET Identity.
|
||||
// For more details see this link http://go.microsoft.com/fwlink/?LinkID=532713
|
||||
public class MessageSender : IEmailSender, IGoogleCloudMessageSender
|
||||
{
|
||||
private ILogger _logger;
|
||||
|
||||
public MessageSender(ILoggerFactory loggerFactory)
|
||||
{
|
||||
_logger = loggerFactory.CreateLogger<MessageSender>();
|
||||
}
|
||||
public async Task <MessageWithPayloadResponse> NotifyEvent<Event>
|
||||
( GoogleAuthSettings googleSettings, IEnumerable<string> regids, Event ev)
|
||||
where Event : IEvent
|
||||
{
|
||||
if (ev == null)
|
||||
throw new Exception("Spécifier un évènement");
|
||||
|
||||
if (ev.Sender == null)
|
||||
throw new Exception("Spécifier un expéditeur");
|
||||
|
||||
if (regids == null )
|
||||
throw new NotImplementedException("Notify & No GCM reg ids");
|
||||
var raa = regids.ToArray();
|
||||
if (raa.Length<1)
|
||||
throw new NotImplementedException("No GCM reg ids");
|
||||
var msg = new MessageWithPayload<Event>()
|
||||
{
|
||||
data = ev,
|
||||
registration_ids = regids.ToArray()
|
||||
};
|
||||
_logger.LogInformation("Sendding to Google : "+JsonConvert.SerializeObject(msg));
|
||||
try {
|
||||
using (var m = new SimpleJsonPostMethod("https://gcm-http.googleapis.com/gcm/send",$"key={googleSettings.ApiKey}")) {
|
||||
return await m.Invoke<MessageWithPayloadResponse>(msg);
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new Exception ("Quelque chose s'est mal passé à l'envoi",ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="googleSettings"></param>
|
||||
/// <param name="registrationId"></param>
|
||||
/// <param name="ev"></param>
|
||||
/// <returns>a MessageWithPayloadResponse,
|
||||
/// <c>bool somethingsent = (response.failure == 0 && response.success > 0)</c>
|
||||
/// </returns>
|
||||
public async Task<MessageWithPayloadResponse> NotifyBookQueryAsync(GoogleAuthSettings googleSettings, IEnumerable<string> registrationIds, RdvQueryEvent ev)
|
||||
{
|
||||
return await NotifyEvent<RdvQueryEvent>(googleSettings,registrationIds, ev);
|
||||
}
|
||||
|
||||
public async Task<MessageWithPayloadResponse> NotifyEstimateAsync(GoogleAuthSettings googleSettings, IEnumerable<string> registrationIds, EstimationEvent ev)
|
||||
{
|
||||
return await NotifyEvent<EstimationEvent>(googleSettings,registrationIds, ev);
|
||||
}
|
||||
|
||||
public async Task<MessageWithPayloadResponse> NotifyHairCutQueryAsync(GoogleAuthSettings googleSettings,
|
||||
IEnumerable<string> registrationIds, HairCutQueryEvent ev)
|
||||
{
|
||||
return await NotifyEvent<HairCutQueryEvent>(googleSettings, registrationIds, ev);
|
||||
}
|
||||
|
||||
public Task<bool> SendEmailAsync(SiteSettings siteSettings, SmtpSettings smtpSettings, string username, string email, string subject, string message)
|
||||
{
|
||||
try
|
||||
{
|
||||
MimeMessage msg = new MimeMessage();
|
||||
msg.From.Add(new MailboxAddress(
|
||||
siteSettings.Owner.Name,
|
||||
siteSettings.Owner.EMail));
|
||||
msg.To.Add(new MailboxAddress(username, email));
|
||||
msg.Body = new TextPart("plain")
|
||||
{
|
||||
Text = message
|
||||
};
|
||||
msg.Subject = subject;
|
||||
using (SmtpClient sc = new SmtpClient())
|
||||
{
|
||||
sc.Connect(
|
||||
smtpSettings.Host,
|
||||
smtpSettings.Port,
|
||||
SecureSocketOptions.None);
|
||||
sc.Send(msg);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
|
||||
public Task<bool> ValidateAsync(string purpose, string token, UserManager<ApplicationUser> manager, ApplicationUser user)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<MessageWithPayloadResponse> NotifyAsync(GoogleAuthSettings _googleSettings, IEnumerable<string> regids, IEvent yaev)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
/* SMS with Twilio:
|
||||
public Task SendSmsAsync(TwilioSettings twilioSettigns, string number, string message)
|
||||
{
|
||||
var Twilio = new TwilioRestClient(twilioSettigns.AccountSID, twilioSettigns.Token);
|
||||
var result = Twilio.SendMessage( twilioSettigns.SMSAccountFrom, number, message);
|
||||
// Status is one of Queued, Sending, Sent, Failed or null if the number is not valid
|
||||
Trace.TraceInformation(result.Status);
|
||||
// Twilio doesn't currently have an async API, so return success.
|
||||
|
||||
return Task.FromResult(result.Status != "Failed");
|
||||
|
||||
} */
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user