-- metodo para whatsapp

This commit is contained in:
2024-12-30 06:43:25 -07:00
parent dd73698395
commit b2b4d3b48e

View File

@@ -818,6 +818,80 @@ Namespace Extensiones
Return Retorno
End Function
Public Function EnviarNotificacionWhatsAppPlantilla(NumeroTelefono As String, NombrePlantilla As String, ParametrosBody As ArrayList, Optional LigaBoton As String = "") As Boolean
Dim RetornoObj As String = "{}"
Dim Retorno As Boolean = False
If NumeroTelefono = "" OrElse NombrePlantilla = "" Then Return False
Try
Dim TokenAutorizacion As String = $"Bearer {WhatsAppToken}"
Using httpClient = New HttpClient()
Using request = New HttpRequestMessage(HttpMethod.Post, New Uri("https://graph.facebook.com/v20.0/425866980604901/messages"))
request.Headers.Add("Authorization", TokenAutorizacion)
Dim buttonComponent As Newtonsoft.Json.Linq.JObject = Nothing
' Crear el array para "parameters"
Dim ArrayBody As Newtonsoft.Json.Linq.JArray = New Newtonsoft.Json.Linq.JArray()
If ParametrosBody.Count > 0 Then
For Each valor In ParametrosBody
Dim parametro As Newtonsoft.Json.Linq.JObject = New Newtonsoft.Json.Linq.JObject(
New Newtonsoft.Json.Linq.JProperty("type", "text"),
New Newtonsoft.Json.Linq.JProperty("text", valor)
)
ArrayBody.Add(parametro)
Next
End If
If LigaBoton <> "" Then
' Crear los parámetros del componente "button"
Dim buttonParameters As Newtonsoft.Json.Linq.JArray = New Newtonsoft.Json.Linq.JArray(
New Newtonsoft.Json.Linq.JObject(
New Newtonsoft.Json.Linq.JProperty("type", "text"),
New Newtonsoft.Json.Linq.JProperty("text", "https://sig.adssistemas.mx.com/Clientes/Clientes.aspx?CveCliente=1")
)
)
' Crear el componente "button"
buttonComponent = New Newtonsoft.Json.Linq.JObject(
New Newtonsoft.Json.Linq.JProperty("type", "button"),
New Newtonsoft.Json.Linq.JProperty("sub_type", "url"),
New Newtonsoft.Json.Linq.JProperty("index", 0),
New Newtonsoft.Json.Linq.JProperty("parameters", buttonParameters)
)
End If
' Crear el componente "body" usando el array
Dim bodyComponent As Newtonsoft.Json.Linq.JObject = New Newtonsoft.Json.Linq.JObject(
New Newtonsoft.Json.Linq.JProperty("type", "body"),
New Newtonsoft.Json.Linq.JProperty("parameters", ArrayBody) ' Usar el JArray directamente
)
' Construir el JSON principal
Dim bodyJson As Newtonsoft.Json.Linq.JObject = New Newtonsoft.Json.Linq.JObject(
New Newtonsoft.Json.Linq.JProperty("messaging_product", "whatsapp"),
New Newtonsoft.Json.Linq.JProperty("to", NumeroTelefono),
New Newtonsoft.Json.Linq.JProperty("type", "template"),
New Newtonsoft.Json.Linq.JProperty("template", New Newtonsoft.Json.Linq.JObject(
New Newtonsoft.Json.Linq.JProperty("name", NombrePlantilla),
New Newtonsoft.Json.Linq.JProperty("language", New Newtonsoft.Json.Linq.JObject(New Newtonsoft.Json.Linq.JProperty("code", "es_MX"))),
New Newtonsoft.Json.Linq.JProperty("components", New Newtonsoft.Json.Linq.JArray(bodyComponent, buttonComponent))
))
)
' Agregar el contenido a la solicitud
Using content = New StringContent(bodyJson.ToString(), Encoding.UTF8, "application/json")
request.Content = content
Dim response = httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).Result
If response.StatusCode = Net.HttpStatusCode.OK Then
RetornoObj = response.Content.ReadAsStringAsync().Result
Retorno = True
End If
End Using
End Using
End Using
Catch ex As Exception
RetornoObj = New Newtonsoft.Json.Linq.JObject(New Newtonsoft.Json.Linq.JProperty("Error", ex.ToString())).ToString()
GuardaLog("Ocurrió un error al enviar la información por WhatsApp: " & ex.Message)
End Try
Return Retorno
End Function
#End Region