Generation

generate functionTue, 23 May 2023

using InConcertSMSProxy.Model; using InConcertSMSProxy.Model.SMS; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Microsoft.IdentityModel.Protocols; using RestSharp; using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Net; namespace InConcertSMSProxy.Controllers { [ApiController] [Route("api/v1/sms")] public class SmsController : Controller { private readonly IConfiguration _configuration; private readonly ILogger<SmsController> _logger; public SmsController(ILogger<SmsController> logger, IConfiguration configuration) { _logger = logger; _configuration = configuration; } [HttpPost] public IActionResult Post([FromBody] SmsRequest body) { var smsResponse = new SmsResponse(); try { // validaciones if (body.addresses.Count == 0) throw new Exception("El Teléfono es requerido"); if (body.message == null || body.message.text == "") throw new Exception("El Mensaje es requerido"); string sms_authorization = _configuration.GetValue<string>("sms_authorization"); string sms_bulkName = _configuration.GetValue<string>("sms_bulkName"); //string sms_src = _configuration.GetValue<string>("sms_src"); // desde aquí empieza el código personalizado para cada cliente /* var client = new RestClient("https://websms.smsvirtual.es/api_php/server.php"); var request = new RestRequest(Method.POST); request.AddHeader("postman-token", "53f219ca-1413-b246-0169-ea127ec719a6"); request.AddHeader("cache-control", "no-cache"); request.AddHeader("soapaction", "urn:websms.smsvirtual.es/api_php/server.php/sendSMS"); request.AddHeader("content-type", "text/xml; charset=utf-8"); request.AddParameter("text/xml; charset=utf-8", "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:sms=\"urn:websms.smsvirtual.es/api_php/smsvirtual.wsdl\"><soapenv:Header/><soapenv:Body><sms:sendSMS soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><user xsi:type=\"xsd:string\">" + sms_user + "</user><pass xsi:type=\"xsd:string\">" + sms_pass + "</pass><src xsi:type=\"xsd:string\">" + sms_src + "</src><dst xsi:type=\"xsd:string\">" + body.addresses[0] + "</dst><msg xsi:type=\"xsd:string\">" + body.message.text + "</msg><date xsi:type=\"xsd:string\"></date><name xsi:type=\"xsd:string\"></name></sms:sendSMS></soapenv:Body></soapenv:Envelope>", ParameterType.RequestBody); IRestResponse response = client.Execute(request); */ var client = new RestClient("https://sms.lanube.cl/services/rest/send"); client.Timeout = 10000; var request = new RestRequest(Method.POST); request.AddHeader("Authorization", sms_authorization); request.AddHeader("Content-Type", "application/json"); var bodySms = new { bulkName = sms_bulkName, message = body.message.text, message_details = new[] { new { destination = body.addresses[0], field = "" } }, isCommercial = false }; request.AddJsonBody(bodySms); IRestResponse response = client.Execute(request); Console.WriteLine(response.Content); var smsEnviado = new SmsEnviados { telefono = body.addresses[0], // Número de teléfono al que se envió el mensaje mensaje = body.message.text, // Mensaje enviado estadoEnvio = true, // Envío exitoso (true) o erróneo (false) tmsTmp = DateTime.Now, // Fecha y hora actual observaciones = string.Empty // Observaciones (puedes agregar algún valor si es necesario) }; InsertarSmsEnviado(smsEnviado.telefono, smsEnviado.mensaje, smsEnviado.estadoEnvio, smsEnviado.observaciones); //************************************************************************************************************** hasta aqui var uniqueId = Guid.NewGuid().ToString(); smsResponse.status = "true"; smsResponse.reason = "SENT"; smsResponse.addresses = " "; smsResponse.addresses += "\"" + body.addresses[0] + "\":{\"id\":\"" + uniqueId + "\",\"status\":\"" + "SENT" + "\",\"reason\":\"" + "" + "\",\"phone\":\"" + body.addresses[0] + "\"},"; smsResponse.addresses = smsResponse.addresses.Substring(0, smsResponse.addresses.Length - 1); var serializedResult = "{\"status\":" + smsResponse.status + ",\"reason\":\"" + smsResponse.reason + "\",\"addresses\":{" + smsResponse.addresses + "}}"; return Ok(serializedResult); } catch (Exception ex) { smsResponse.status = "false"; smsResponse.reason = ex.Message; _logger.LogError("[ERROR]: " + ex.Message + ", " + ex.InnerException, null); return Ok(smsResponse); } } private void InsertarSmsEnviado(string telefono, string mensaje, bool estadoEnvio, string observaciones) { //string IPServerBD = ConfigurationManager.AppSettings["IPServerBD"]; //string BDCatalog = ConfigurationManager.AppSettings["BDCatalog"]; string IPServerBD = _configuration.GetValue<string>("IPServerBD"); string BDCatalog = _configuration.GetValue<string>("BDCatalog"); using (SqlConnection con = new SqlConnection("Data Source=" + IPServerBD + ";Initial Catalog=" + BDCatalog + ";User ID=UsrAccMw;Password=inc2001")) { try { con.Open(); using (SqlCommand cmd = new SqlCommand("INSERT INTO SmsEnviados (telefono, mensaje, estadoEnvio, tmsTmp, observaciones) VALUES (@telefono, @mensaje, @estadoEnvio, @observaciones)", con)) { cmd.Parameters.AddWithValue("@telefono", telefono); cmd.Parameters.AddWithValue("@mensaje", mensaje); cmd.Parameters.AddWithValue("@estadoEnvio", estadoEnvio); cmd.Parameters.AddWithValue("@observaciones", observaciones); cmd.ExecuteNonQuery(); } } catch (Exception ex) { // Manejo de errores _logger.LogError("[ERROR]: " + ex.Message + " - " + ex.StackTrace); } } } } }

function that: using InConcertSMSProxy.Model; using InConcertSMSProxy.Model.SMS; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Microsoft.IdentityModel.Protocols; using RestSharp; using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Net; namespace InConcertSMSProxy.Controllers { [ApiController] [Route("api/v1/sms")] public class SmsController : Controller { private readonly IConfiguration _configuration; private readonly ILogger<SmsController> _logger; public SmsController(ILogger<SmsController> logger, IConfiguration configuration) { _logger = logger; _configuration = configuration; } [HttpPost] public IActionResult Post([FromBody] SmsRequest body) {

Want to kickstart your project?Use the new AI Studio to create your code