Réécriture de l'envoi de SMS (OVH) + Ajout d'une fonction d'import depuis des fichiers Excel + Ajout d'une donnée pour le numéro d'appartement.

This commit is contained in:
2020-12-28 00:24:12 +01:00
parent 22de04c0c9
commit f4aac126eb
24 changed files with 778 additions and 193 deletions

View File

@@ -1,11 +1,13 @@
using Hermes.Model;
using IO.Swagger.Api;
using IO.Swagger.Client;
using IO.Swagger.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Documents;
@@ -13,6 +15,20 @@ namespace Hermes {
public partial class SmsWindow : Window {
private ObservableCollection<SmsSendingStatus> status = new ObservableCollection<SmsSendingStatus>();
private bool sending = false;
private Regex numPrefixRgx = new Regex(@"^0");
public static string HashSHA1(string input) {
SHA1 sha = SHA1.Create();
byte[] bHash = sha.ComputeHash(Encoding.UTF8.GetBytes(input));
StringBuilder sBuilder = new StringBuilder();
for(int i = 0; i < bHash.Length; i++) {
sBuilder.Append(bHash[i].ToString("x2"));
}
return sBuilder.ToString();
}
public SmsWindow(Window parent, List<Citoyen> rcps) {
InitializeComponent();
@@ -22,7 +38,7 @@ namespace Hermes {
SmsSendingStatus s = new SmsSendingStatus();
s.Nom = rcp.Nom;
s.Prenom = rcp.Prenom;
s.Mobile = rcp.TelPort.Replace(" ", "");
s.Mobile = numPrefixRgx.Replace(rcp.TelPort.Replace(" ", ""), "+33");
s.Status = "En attente";
status.Add(s);
}
@@ -41,10 +57,12 @@ namespace Hermes {
ModelContext dbContext = ModelContext.Getinstance();
bool error = false;
string message = new TextRange(messageTextBox.Document.ContentStart, messageTextBox.Document.ContentEnd).Text;
string apiKey = dbContext.Preferences.Local[0].SmsApiKey;
Preferences pref = dbContext.Preferences.Local[0];
if(String.IsNullOrWhiteSpace(apiKey)) {
MessageBox.Show("Erreur lors de la tentative d'envoi : Clé ISendPro invalide.", "Envoi de SMS", MessageBoxButton.OK, MessageBoxImage.Error);
if(String.IsNullOrWhiteSpace(pref.ovhSmsServiceName) || String.IsNullOrWhiteSpace(pref.ovhSmsApplicationName)
|| String.IsNullOrWhiteSpace(pref.ovhSmsApplicationKey) || String.IsNullOrWhiteSpace(pref.ovhSmsConsumerKey)) {
MessageBox.Show("Erreur lors de la tentative d'envoi : Clés d'accès invalides.", "Envoi de SMS", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
@@ -58,44 +76,57 @@ namespace Hermes {
messageTextBox.IsEnabled = false;
sending = true;
ApiClient apiClient = new ApiClient();
SmsApi smsApi = new SmsApi(apiClient);
string query = $"https://eu.api.ovh.com/1.0/sms/{pref.ovhSmsServiceName}/jobs";
foreach(SmsSendingStatus stat in status) {
if(!stat.Status.Equals("Envoyé")) {
SmsUniqueRequest req = new SmsUniqueRequest();
req.Keyid = apiKey;
req.Sms = message;
req.Smslong = "999";
req.Num = stat.Mobile;
try {
SMSReponse resp = smsApi.SendSms(req);
if(resp != null && resp.Etat != null && resp.Etat.Etat != null && resp.Etat.Etat.Count > 0) {
SMSReponseEtatEtat eta = resp.Etat.Etat[0];
if(eta.Code != null && eta.Code == 0 && eta.Tel != null) {
stat.Status = "Envoyé";
} else {
error = true;
stat.Status = eta.Message;
}
} else {
error = true;
stat.Status = "Erreur technique";
OVHQueryBody body = new OVHQueryBody();
body.charset = "UTF-8";
body.receivers = new string[] { stat.Mobile };
body.message = message;
body.priority = "high";
body.senderForResponse = true;
string bodyStr = JsonConvert.SerializeObject(body);
string ts = ((Int32) DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds).ToString();
string signature = "$1$" + HashSHA1(pref.ovhSmsApplicationKey + "+" + pref.ovhSmsConsumerKey + "+" + "POST" + "+" + query + "+" + bodyStr + "+" + ts);
HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create(query);
req.Method = "POST";
req.ContentType = "application/json";
req.Headers.Add("X-Ovh-Application:" + pref.ovhSmsApplicationName);
req.Headers.Add("X-Ovh-Consumer:" + pref.ovhSmsConsumerKey);
req.Headers.Add("X-Ovh-Signature:" + signature);
req.Headers.Add("X-Ovh-Timestamp:" + ts);
using(System.IO.Stream s = req.GetRequestStream()) {
using(System.IO.StreamWriter sw = new System.IO.StreamWriter(s)) {
sw.Write(bodyStr);
}
} catch(ApiException ex) {
error = true;
SMSReponse resp = JsonConvert.DeserializeObject<SMSReponse>((String) ex.ErrorContent);
if(resp != null && resp.Etat != null && resp.Etat.Etat != null && resp.Etat.Etat.Count > 0) {
SMSReponseEtatEtat eta = resp.Etat.Etat[0];
if(eta.Code != null && eta.Code == 0 && eta.Tel != null) {
}
try {
HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
using(var stream = resp.GetResponseStream()) {
var reader = new StreamReader(stream);
OVHResponseBody rb = JsonConvert.DeserializeObject<OVHResponseBody>(reader.ReadToEnd().Trim());
if(rb.validReceivers.Length > 0 && rb.validReceivers[0] != null && rb.validReceivers[0].Equals(stat.Mobile)) {
stat.Status = "Envoyé";
} else {
error = true;
stat.Status = eta.Message;
stat.Status = "Numéro de mobile invalide";
}
} else {
error = true;
stat.Status = "Erreur technique";
}
resp.Close();
} catch(WebException ex) {
error = true;
WebResponse resp = ex.Response;
using(var stream = resp.GetResponseStream()) {
var reader = new StreamReader(stream);
String result = reader.ReadToEnd().Trim();
stat.Status = result;
}
}
}