yavsc/Yavsc/Services/MessageServices.cs

140 lines
5.5 KiB
C#
Raw Normal View History

2016-05-17 18:22:18 +02:00
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;
2016-05-31 15:59:36 +02:00
using System.Collections.Generic;
2017-02-27 19:28:32 +01:00
using Yavsc.Models.Haircut;
2018-02-03 20:17:29 +01:00
using Yavsc.Interfaces.Workflow;
using System.Linq;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
2018-03-12 23:49:05 +01:00
using Yavsc.Server.Helpers;
2016-05-17 18:22:18 +02:00
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
2018-03-09 04:25:52 +01:00
public class MessageSender : IEmailSender, IGoogleCloudMessageSender
2016-05-17 18:22:18 +02:00
{
2018-02-03 20:17:29 +01:00
private ILogger _logger;
2018-03-09 04:25:52 +01:00
public MessageSender(ILoggerFactory loggerFactory)
2018-02-03 20:17:29 +01:00
{
2018-03-09 04:25:52 +01:00
_logger = loggerFactory.CreateLogger<MessageSender>();
2018-02-03 20:17:29 +01:00
}
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);
}
}
2016-05-17 18:22:18 +02:00
/// <summary>
///
/// </summary>
/// <param name="googleSettings"></param>
/// <param name="registrationId"></param>
/// <param name="ev"></param>
/// <returns>a MessageWithPayloadResponse,
/// <c>bool somethingsent = (response.failure == 0 &amp;&amp; response.success > 0)</c>
/// </returns>
2017-02-27 19:28:32 +01:00
public async Task<MessageWithPayloadResponse> NotifyBookQueryAsync(GoogleAuthSettings googleSettings, IEnumerable<string> registrationIds, RdvQueryEvent ev)
2016-05-17 18:22:18 +02:00
{
2018-02-03 20:17:29 +01:00
return await NotifyEvent<RdvQueryEvent>(googleSettings,registrationIds, ev);
2016-05-17 18:22:18 +02:00
}
public async Task<MessageWithPayloadResponse> NotifyEstimateAsync(GoogleAuthSettings googleSettings, IEnumerable<string> registrationIds, EstimationEvent ev)
{
2018-02-03 20:17:29 +01:00
return await NotifyEvent<EstimationEvent>(googleSettings,registrationIds, ev);
}
2017-02-27 19:28:32 +01:00
public async Task<MessageWithPayloadResponse> NotifyHairCutQueryAsync(GoogleAuthSettings googleSettings,
IEnumerable<string> registrationIds, HairCutQueryEvent ev)
{
2018-02-03 20:17:29 +01:00
return await NotifyEvent<HairCutQueryEvent>(googleSettings, registrationIds, ev);
2017-02-27 19:28:32 +01:00
}
public Task<bool> SendEmailAsync(SiteSettings siteSettings, SmtpSettings smtpSettings, string username, string email, string subject, string message)
2016-05-17 18:22:18 +02:00
{
2016-07-21 12:25:13 +02:00
try
{
2016-05-17 18:22:18 +02:00
MimeMessage msg = new MimeMessage();
msg.From.Add(new MailboxAddress(
siteSettings.Owner.Name,
2017-06-08 17:09:06 +02:00
siteSettings.Owner.EMail));
msg.To.Add(new MailboxAddress(username, email));
2016-05-17 18:22:18 +02:00
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);
}
}
2016-07-21 12:25:13 +02:00
catch (Exception)
{
2016-05-17 18:22:18 +02:00
return Task.FromResult(false);
}
return Task.FromResult(true);
}
public Task<bool> ValidateAsync(string purpose, string token, UserManager<ApplicationUser> manager, ApplicationUser user)
{
throw new NotImplementedException();
}
2018-02-03 20:17:29 +01:00
public Task<MessageWithPayloadResponse> NotifyAsync(GoogleAuthSettings _googleSettings, IEnumerable<string> regids, IEvent yaev)
{
throw new NotImplementedException();
}
2016-05-17 18:22:18 +02:00
/* SMS with Twilio:
public Task SendSmsAsync(TwilioSettings twilioSettigns, string number, string message)
{
2018-02-03 20:17:29 +01:00
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.
2016-05-17 18:22:18 +02:00
2018-02-03 20:17:29 +01:00
return Task.FromResult(result.Status != "Failed");
2016-05-17 18:22:18 +02:00
} */
}
}