81 lines
3.1 KiB
C#
81 lines
3.1 KiB
C#
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using tsUtilidades.Extensiones;
|
|
namespace itsm
|
|
{
|
|
public class sms
|
|
{
|
|
public static bool IsUnicodeSms(string message)
|
|
{
|
|
string ms2 = Regex.Replace(message, @"[^\u0000-\u007F]", string.Empty);
|
|
return ms2 != message;
|
|
}
|
|
|
|
public static async Task<string> Enviarsms(string tlfDestino, string mensaje, string apiKey)
|
|
{
|
|
try
|
|
{
|
|
// Validación del teléfono (asumiendo que tienes el método de extensión)
|
|
if (!tlfDestino.EsNumeroTelefonoMovilEspañolValido())
|
|
throw new Exception("TELEFONO_INVALIDO");
|
|
|
|
int icodingscheme = 0;
|
|
|
|
if (mensaje.Length < 71)
|
|
{
|
|
icodingscheme = 8;
|
|
}
|
|
else
|
|
{
|
|
// Limpieza de caracteres para mensajes largos
|
|
mensaje = mensaje.Replace("á", "a")
|
|
.Replace("Á", "A")
|
|
.Replace("é", "e")
|
|
.Replace("É", "E")
|
|
.Replace("í", "i")
|
|
.Replace("Í", "I")
|
|
.Replace("ó", "o")
|
|
.Replace("Ó", "O")
|
|
.Replace("ú", "u")
|
|
.Replace("Ú", "U")
|
|
.Replace("Ü", "U")
|
|
.Replace("ü", "u")
|
|
.Replace("Ñ", "N")
|
|
.Replace("ñ", "n")
|
|
.Replace("€", "Eur.");
|
|
|
|
if (IsUnicodeSms(mensaje))
|
|
throw new Exception("El mensaje tiene más de 70 caracteres y es unicode");
|
|
}
|
|
|
|
if (mensaje.Length > 160)
|
|
throw new Exception("El mensaje tiene más de 160 caracteres");
|
|
|
|
string url = $"https://sms.itsoft.es/SIC/cxfservices/httpapi/Send?text=" + mensaje+"&to=" +tlfDestino;
|
|
|
|
|
|
var client = new HttpClient();
|
|
var credenciales = Convert.ToBase64String(Encoding.UTF8.GetBytes(apiKey));
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credenciales);
|
|
|
|
// Realizamos la petición GET de forma asíncrona
|
|
var response = await client.GetAsync(url);
|
|
var result = await response.Content.ReadAsStringAsync();
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
throw new Exception($"Error Desconocido. Respuesta del servicio: {result}");
|
|
}
|
|
|
|
return result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// En C# Throw; mantiene el stack trace original si solo quieres relanzar
|
|
throw new Exception(ex.Message, ex);
|
|
}
|
|
}
|
|
}
|
|
}
|