WebHooks, escuchando Mandrill en asp.net

 

Lo primero, la web que esta a la escucha de los webhooks:

Public Class getMandrill
    Inherits System.Web.UI.Page
    Dim mandrill_events As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        mandrill_events = Request.Form("mandrill_events")
        Dim mandrillEvent As New cMandrillEvent(mandrill_events, "DSN=Caicoya")

    End Sub

End Class

Ahora la clase cMandrillEvent que se encarga de deserializar el json, crea un objeto con todo el envio desde el webhook, luego podeis hacer lo que querais con el, añadir base de datos, poner por pantalla, etc.

Imports Newtonsoft.Json.Linq
Imports System.Web.Script.Serialization
Imports System.Web.HttpContext
Public Class cMandrillEvent
    Dim jsonString As String
    Dim prueba As String
    Dim arrayEventos As List(Of MEvento)
    'esta es una clase mia para manipular BD, podeis hacerlo como esteis acostumbrados
    Dim c As cConex

    Public Sub New(json As String)
        'inicializamos conexion a base de datos
        c = New cConex("dsn vuestro")
        Me.jsonString = json
        Dim jss As New JavaScriptSerializer()
        Try
            arrayEventos = jss.Deserialize(Of List(Of MEvento))(json)
        Catch ex As Exception
            Current.Response.Write("<br><br><span style='color:red'>Error en la aplicacion importando json </span><br>: " & jsonString & "<br><br>" & ex.Message & "<br><br>" & ex.ToString)
        End Try
        Try
            saveDB()
        Catch ex As Exception
            Current.Response.Write("<br><br><span style='color:red'>Error en la aplicacion guardando DB json </span><br>: " & jsonString & "<br><br>" & ex.Message & "<br><br>" & ex.ToString)
        End Try
    End Sub

    Public Sub saveDB()
        For Each e As MEvento In arrayEventos
            e.saveDB(c)
        Next
    End Sub

    Public Class MEvento
        Public Property ts() As Integer = 0
        Public Property [event]() As String = ""
        Public Property url() As String = ""
        Public Property ip() As String = ""
        Public Property user_agent() As String = ""
        Public Property location() As MLocation
        Public Property user_agent_parsed As MUser_agent_parsed
        Public Property msg As Mmsg

        Public Function saveDB(c As cConex) As String
            'codigo guardar
        End Function

        Public Overrides Function toString() As String
            'codigo mostrar
        End Function
    End Class

    Public Class Mmsg
        Public Property _id() As String = ""
        Public Property ts() As Integer = 0
        Public Property email() As String = ""
        Public Property sender() As String = ""
        Public Property subject() As String = ""
        Public Property opens() As Integer()
        Public Property clicks() As Integer()
        Public Property tags() As String()
        Public Property metadata() As MMetadata
        Public Property state() As String = ""
        Public Property diag() As String = ""
        Public Property bounce_description() As String = ""

        Public Function saveDB(c As cConex) As String
            'codigo para guardar
        End Function

        Public Overrides Function toString() As String
            'codigo para mostrar
        End Function
    End Class

    Public Class MLocation
        Public Property country_short() As String = ""
        Public Property country_long() As String = ""
        Public Property region() As String = ""
        Public Property city() As String = ""
        Public Property postal_code() As String = ""
        Public Property timezone() As String = ""
        Public Property latitude() As Double = 0
        Public Property longitude() As Double = 0

        Public Function saveDB(c As cConex) As Integer
            'codigo para guardar
        End Function

        Public Overrides Function toString() As String
            'codigo para mostrar
        End Function

    End Class

    Public Class MUser_agent_parsed
        Public Property mobile() As Boolean
        Public Property os_company() As String = ""
        Public Property os_company_url() As String = ""
        Public Property os_family() As String = ""
        Public Property os_icon() As String = ""
        Public Property os_name() As String = ""
        Public Property os_url() As String = ""
        Public Property type() As String = ""
        Public Property ua_company() As String = ""
        Public Property ua_company_url() As String = ""
        Public Property ua_family() As String = ""
        Public Property ua_icon() As String = ""
        Public Property ua_name() As String = ""
        Public Property ua_url() As String = ""
        Public Property ua_version() As String = ""

        Public Function saveDB(c As cConex) As Integer
            ' codigo mpara guardar lo que querais
        End Function

        Public Overrides Function toString() As String
            'codigo para mostrar
        End Function

    End Class

    Public Class MMetadata
        Public Property user_id() As Integer = 0
        Public Overrides Function toString() As String
            'codigo para mostrar
        End Function
    End Class

End Class

Espero que os ayude a los que esten atascados con esto, atenderé cualquier consulta, saludos.

5 pensamientos en “WebHooks, escuchando Mandrill en asp.net”

  1. Hiya! I know this is kinda off topic however I’d figured I’d ask.
    Would you be interested in exchanging links or maybe guest authoring a blog post or vice-versa?
    My site goes over a lot of the same subjects as yours
    and I think we could greatly benefit from each other.
    If you happen to be interested feel free to shoot me an email.
    I look forward to hearing from you! Terrific blog
    by the way!

  2. Hello there, I discovered your site by way of Google even as searching
    for a similar matter, your website came up, it seems to be
    good. I have bookmarked it in my google bookmarks.
    I’m no longer certain the place you’re getting your information, however great topic.
    I needs to spend some time studying much more or figuring out more about WebHooks,
    escuchando Mandrill en asp.net. Thank you for great info I used to
    be looking for this information for my mission.

Deja un comentario