This commit is contained in:
2022-03-19 22:52:06 +01:00
parent 9b001e5803
commit d9204781cf
139 changed files with 19394 additions and 3352 deletions

View File

@@ -5,6 +5,8 @@ Imports System.Data.SqlClient
Imports System.Reflection
Public Class cSMS
Dim API_USER_AUTHORIZATION = "040b08e268d340179d922f867816b8a6"
Property sms_id As Integer
Property sms_Datum As Date = Now
Property sms_Firma As Object = Nothing
@@ -32,6 +34,12 @@ Public Class cSMS
Property sms_Delivered As Object = Nothing
Property sms_Failure As Object = Nothing
Property sms_art as string= "SMS"
Property whatsApp_status As Object = Nothing
Property whatsApp_gatewayId As Object = Nothing
Property whatsApp_error As Object = Nothing
Property whatsApp_errorCode As Object = Nothing
Dim ESENDEX_USER As String = "al@verag.ag"
Dim ESENDEX_PWD As String = "Developer#2"
@@ -78,6 +86,11 @@ Public Class cSMS
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_Failure", sms_Failure))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_art", sms_art))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("whatsApp_status", whatsApp_status))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("whatsApp_gatewayId", whatsApp_gatewayId))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("whatsApp_error", whatsApp_error))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("whatsApp_errorCode", whatsApp_errorCode))
Return list
End Function
@@ -88,10 +101,10 @@ Public Class cSMS
Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM tblSMS WITH(updlock,serializable) WHERE sms_id=@sms_id) " &
" BEGIN " & getUpdateCmd() & " END " &
Dim sqlstr = " BEGIN TRAN If EXISTS(Select * FROM tblSMS With(updlock, serializable) WHERE sms_id=@sms_id) " &
" BEGIN " & getUpdateCmd() & " End " &
" Else " &
" BEGIN " & getInsertCmd() & " END " &
" BEGIN " & getInsertCmd() & " End " &
" commit tran "
sms_id = SQL.doSQLVarListID(sms_id, sqlstr, "FMZOLL", , list)
@@ -102,7 +115,7 @@ Public Class cSMS
Public Sub LOAD()
Try
Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
Using cmd As New SqlCommand("SELECT * FROM tblSMS WHERE sms_id=@sms_id ", conn)
Using cmd As New SqlCommand("Select * FROM tblSMS WHERE sms_id=@sms_id ", conn)
cmd.Parameters.AddWithValue("@sms_id", sms_id)
Dim dr = cmd.ExecuteReader()
If dr.Read Then
@@ -214,6 +227,178 @@ Public Class cSMS
End Try
End Function
Function sendWhatsApp(Originator As String) As Boolean
Try
VERAG_PROG_ALLGEMEIN.cChilkat_Helper.UnlockCilkat()
Dim API_STRING = "https://connect.esendex.com"
Dim rest As New Chilkat.Rest
Dim success As Boolean
Dim bTls As Boolean = True
Dim port As Integer = 443
Dim bAutoReconnect As Boolean = True
success = rest.Connect(API_STRING, port, bTls, bAutoReconnect)
If (success <> True) Then
Debug.WriteLine("ConnectFailReason: " & rest.ConnectFailReason)
Debug.WriteLine(rest.LastErrorText)
Return False
End If
rest.AddHeader("Content-Type", "application/json")
rest.AddHeader("Authorization", API_USER_AUTHORIZATION)
'rest.AddHeader("Accept", "application/vnd.hmrc.1.0+json")
'Dim sbRequestBody As New Chilkat.StringBuilder
'json.EmitSb(sbRequestBody)
Me.sms_art = "WHATSAPP"
Dim absenderFirma As String = Me.sms_Firma
Dim HandyNr As String = Me.sms_Handynummer
If HandyNr.ToString.StartsWith("00") Then HandyNr = "+" & HandyNr.Substring(2)
If Originator = "" Then Originator = absenderFirma
If Originator.Length > 11 Then Originator = Originator.Substring(0, 11)
Dim json As New Chilkat.JsonObject
Dim successJsonLoad As Boolean = json.Load("
{
""accountReference"": """ & Me.sms_Account & """,
""channels"": [
""WhatsApp""
],
""metadata"":{
""key"": ""value""
},
""recipient"": {
""address"": {
""msisdn"": """ & HandyNr & """
}
},
""content"": {
""body"": {
""text"": {
""value"": """ & Me.sms_Nachricht & """
}
}
},
""channelSettings"": {
""sms"": {
""originator"": """ & Originator & """,
""characterSet"": ""GSM""
}
},
""expiry"": ""30""
}")
If (successJsonLoad <> True) Then
MsgBox(json.LastErrorText)
Return False
End If
Dim sbRequestBody As New Chilkat.StringBuilder
json.EmitSb(sbRequestBody)
' MsgBox(sbRequestBody.ToString)
Dim sbResponseBody As New Chilkat.StringBuilder
success = rest.FullRequestSb("POST", "/richcontent/v3/send", sbRequestBody, sbResponseBody)
If (success <> True) Then
Debug.WriteLine(rest.LastErrorText)
Return False
End If
Dim respStatusCode As Integer = rest.ResponseStatusCode
Me.whatsApp_status = respStatusCode ' 202 = OK - Gesendet
If (respStatusCode <> 202) Then
MsgBox(sbResponseBody.GetAsString())
Return False
End If
Dim jsonResp = New Chilkat.JsonObject()
jsonResp.LoadSb(sbResponseBody)
Dim gatewayId = jsonResp.StringOf("gatewayId")
Dim Resp_error = jsonResp.StringOf("error")
Dim Resp_errorCode = jsonResp.StringOf("errorCode")
Me.whatsApp_gatewayId = gatewayId
If Resp_error <> "" Then Me.whatsApp_error = Resp_error
If Resp_errorCode <> "" Then Me.whatsApp_errorCode = Resp_errorCode
' MsgBox(gatewayId)
If Not Me.SAVE() Then
MsgBox("Fehler: SMS Daten wurden nicht gespeichert!")
End If
Return True
'productTitle = json.StringOf("product.title");
'productBody_html = json.StringOf("product.body_html");
'productVendor = json.StringOf("product.vendor");
'productProduct_type = json.StringOf("product.product_type");
'productCreated_at = json.StringOf("product.created_at");
'productHandle = json.StringOf("product.handle");
'productUpdated_at = json.StringOf("product.updated_at");
'productPublished_at = json.IsNullOf("product.published_at");
'productTemplate_suffix = json.IsNullOf("product.template_suffix");
'productPublished_scope = json.StringOf("product.published_scope");
'productTags = json.StringOf("product.tags");
'productImage = json.IsNullOf("product.image");
'i = 0;
'count_i = json.SizeOfArray("product.variants");
'while (i < count_i) {
' json.I = i;
' id = json.IntOf("product.variants[i].id");
' product_id = json.IntOf("product.variants[i].product_id");
' title = json.StringOf("product.variants[i].title");
' price = json.StringOf("product.variants[i].price");
' sku = json.StringOf("product.variants[i].sku");
' position = json.IntOf("product.variants[i].position");
' grams = json.IntOf("product.variants[i].grams");
' inventory_policy = json.StringOf("product.variants[i].inventory_policy");
' compare_at_price = json.IsNullOf("product.variants[i].compare_at_price");
' fulfillment_service = json.StringOf("product.variants[i].fulfillment_service");
' inventory_management = json.IsNullOf("product.variants[i].inventory_management");
' option1 = json.StringOf("product.variants[i].option1");
' option2 = json.IsNullOf("product.variants[i].option2");
' option3 = json.IsNullOf("product.variants[i].option3");
' created_at = json.StringOf("product.variants[i].created_at");
' updated_at = json.StringOf("product.variants[i].updated_at");
' taxable = json.BoolOf("product.variants[i].taxable");
' barcode = json.IsNullOf("product.variants[i].barcode");
' image_id = json.IsNullOf("product.variants[i].image_id");
' inventory_quantity = json.IntOf("product.variants[i].inventory_quantity");
' weight = json.IntOf("product.variants[i].weight");
' weight_unit = json.StringOf("product.variants[i].weight_unit");
' old_inventory_quantity = json.IntOf("product.variants[i].old_inventory_quantity");
' requires_shipping = json.BoolOf("product.variants[i].requires_shipping");
' i = i + 1;
'}
Return True
Catch ex As Exception
MsgBox("Fehler beim Senden der WhatsApp-SMS!" & vbNewLine & ex.Message & ex.StackTrace)
Return False
End Try
End Function
Function checkSMSStatus(Optional silent = False, Optional insertVermerkAviso = True) As String
Try
@@ -279,11 +464,152 @@ Public Class cSMS
End Function
Function checkWhatsAppStatus(Optional silent = False, Optional insertVermerkAviso = True) As String
Try
Select Case Me.sms_Status
Case MessageStatus.Submitted, MessageStatus.Sent, MessageStatus.Scheduled, MessageStatus.Connecting, -1
'Weiter...
Case Else
' Bereits finaler Status, keine Abfrage nötig
Return Me.sms_Status
End Select
If If(Me.whatsApp_gatewayId, "") = "" Then
If Not silent Then MsgBox("Keine MessageId gefunden!")
Return Me.sms_Status ' Keine MsgId
End If
Dim API_STRING = "https://connect.esendex.com"
Dim rest As New Chilkat.Rest
Dim success As Boolean
Dim bTls As Boolean = True
Dim port As Integer = 443
Dim bAutoReconnect As Boolean = True
success = rest.Connect(API_STRING, port, bTls, bAutoReconnect)
If (success <> True) Then
If Not silent Then MsgBox("ConnectFailReason: " & rest.ConnectFailReason & vbNewLine & rest.LastErrorText)
Return Me.sms_Status
End If
rest.AddHeader("Content-Type", "application/json")
rest.AddHeader("Authorization", API_USER_AUTHORIZATION)
Dim sbRequestBody As New Chilkat.StringBuilder
' MsgBox(sbRequestBody.ToString)
Dim sbResponseBody As New Chilkat.StringBuilder
success = rest.FullRequestSb("GET", "/richcontent/v3/accounts/" & Me.sms_Account & "/messages/" & Me.whatsApp_gatewayId, sbRequestBody, sbResponseBody)
If (success <> True) Then
If Not silent Then MsgBox(rest.LastErrorText)
Return Me.sms_Status
End If
Dim respStatusCode As Integer = rest.ResponseStatusCode
Me.whatsApp_status = respStatusCode ' 200 Abfrage hat funktioniert
' MsgBox(respStatusCode)
If (respStatusCode <> 200) Then
'Fehler --> --> FALLBACK SMS!!!
If DateDiff(DateInterval.Minute, Me.sms_Datum, Now) > 10 Then
Me.FALLBACK_SMS
End If
' MsgBox(sbResponseBody.GetAsString())
If Not silent Then MsgBox("Error: " & respStatusCode)
Return Me.sms_Status
End If
Dim jsonResp = New Chilkat.JsonObject()
jsonResp.LoadSb(sbResponseBody)
' MsgBox(sbResponseBody.ToString)
Dim allowVermerk = True
Dim lastUpdatedUtc = jsonResp.StringOf("lastUpdatedUtc")
Dim StatusResp = jsonResp.StringOf("status")
Select Case StatusResp
Case "Submitted"
Me.sms_Status = MessageStatus.Submitted
If DateDiff(DateInterval.Minute, Me.sms_Datum, Now) > 10 Then
'--> FALLBACK SMS!!!
allowVermerk = False
Me.FALLBACK_SMS()
End If
Case "Delivered"
Me.sms_Status = MessageStatus.Delivered
Me.sms_Delivered = lastUpdatedUtc
Case "Failed"
Me.sms_Status = MessageStatus.Failed
Dim reason = jsonResp.StringOf("reason")
Me.sms_Failure = reason
'--> FALLBACK SMS!!!
allowVermerk = False
FALLBACK_SMS()
End Select
Me.SAVE()
If allowVermerk Then
Try
If Me.sms_AvisoId IsNot Nothing AndAlso IsNumeric(Me.sms_AvisoId) Then
Select Case StatusResp
Case "Failed"
'Keine Übermittlung.
Dim MaId As Integer = SQL.DLookup("LetzterMitarbeiterId", "Aviso", "AvisoId='" & Me.sms_AvisoId & "' ", "AVISO", -1)
If MaId > 0 Then
VERAG_PROG_ALLGEMEIN.cAvisoBenachrichtigungen.INSERT_BENACHRICHTIGUNG(Me.sms_AvisoId, Nothing, 3, "B", MaId, "*WhatsApp Fehler*", 0, "", "Fehler bei der WhatsApp Übermittlung: " & If(Me.sms_Failure, ""))
End If
End Select
End If
Catch ex As Exception
VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodBase.GetCurrentMethod.Name)
End Try
End If
Return Me.sms_Status
Catch ex As Exception
If Not silent Then MsgBox("Fehler beim Prüfen des SMS Status!" & vbNewLine & ex.Message & ex.StackTrace)
End Try
Return ""
End Function
Shared Function SEND_StatusSMS(AVISOId As Integer) As Boolean
Dim AVISO = cAviso.getAvisoById(AVISOId, "")
Return SEND_StatusSMS(AVISO)
End Function
Sub FALLBACK_SMS()
'--> FALLBACK SMS!!!
Me.sms_art = "SMS"
Me.sms_Status = -1
Me.sendSMS()
Me.SAVE()
End Sub
Shared Function SEND_StatusSMS(AVISO As cAviso) As Boolean
If If(AVISO.FahrerHandy, "") <> "" AndAlso IsNumeric(AVISO.FahrerHandy.trim) Then
@@ -339,3 +665,4 @@ Public Class cSMS
End Class