Enter Search Query:

[VB] Porteo de la implementación del api de WhatsApi a VB.NET

[VB] Porteo de la implementación del api de WhatsApi a VB.NET

En este post no se explicará que es WhatsAPI, aclaro que no se usará WhatsAPI sino WhatsAPI.Net, tampoco se enseñará paso a paso; y menos se darán indicaciones sobre el uso del lenguaje VB.Net.

Que vamos a necesitar:

Si es posible el último VisualStudio, sino cualquier otro servirá.

WART (descargar el Exe)

WhatsAppAPI.dll (Les dejo la última versión a la fecha subida a Mediafire, pero intenten bajar una que esté actualizada o compílenla del source en github).
Y por último de ser posible una línea de teléfono en la que no utilicemos WhatsApp (así no se nos complican las cosas, el celular de la abuela servirá).

Empezamos:

Abrimos el WART, Colocamos nuestro numero (Sin + y sin ceros al principio, por ejemplo un número para Argentina sería 5492235352914 o 542235352914 [Nunca supe la diferencia entre con nueve y sin nueve]) y apretamos el botón «Request Code»

 

[sociallocker]

Una vez hecho esto, te llegará un SMS o una llamada dándote un código. Colócalo en Code y presiona «Confirm Code», luego nos dará una contraseña. (en caso de perder la contraseña, como ya están registrados solo tienen que poner el número y clickear en «Check Existing».)

La contraseña que nos dio la tenemos que copiar y guardar en un archivo de texto llamado «WAPASS.txt» en la carpeta donde se ejecute nuestra aplicación, para testeos o debug; obviamente será en «ruta_al_proyecto/bin/debug/WAPASS.txt».

[/sociallocker]

 

Después vamos a hacer un proyecto nuevo y vamos a agregar un modulo «mWhatsApp».

Module mWhatsApp
Dim WAPass As String
Const WANum As String = "5492236685519"
Dim wa As WhatsApp
Public Sub InitWA(ByVal NickName As String, Optional ByVal debug As Boolean = False)
WAPass = File.ReadAllText(My.Application.Info.DirectoryPath & "\WAPASS.txt")
wa = New WhatsApp(WANum, WAPass, NickName, debug)
AddHandler wa.OnLoginSuccess, AddressOf wa_OnLoginSuccess
AddHandler wa.OnLoginFailed, AddressOf wa_OnLoginFailed
AddHandler wa.OnGetMessage, AddressOf wa_OnGetMessage
AddHandler wa.OnGetMessageReceivedClient, AddressOf wa_OnGetMessageReceivedClient
AddHandler wa.OnGetMessageReceivedServer, AddressOf wa_OnGetMessageReceivedServer
AddHandler wa.OnNotificationPicture, AddressOf wa_OnNotificationPicture
AddHandler wa.OnGetPresence, AddressOf wa_OnGetPresence
AddHandler wa.OnGetGroupParticipants, AddressOf wa_OnGetGroupParticipants
AddHandler wa.OnGetLastSeen, AddressOf wa_OnGetLastSeen
AddHandler wa.OnGetTyping, AddressOf wa_OnGetTyping
AddHandler wa.OnGetPaused, AddressOf wa_OnGetPaused
AddHandler wa.OnGetMessageImage, AddressOf wa_OnGetMessageImage
AddHandler wa.OnGetMessageAudio, AddressOf wa_OnGetMessageAudio
AddHandler wa.OnGetMessageVideo, AddressOf wa_OnGetMessageVideo
AddHandler wa.OnGetMessageLocation, AddressOf wa_OnGetMessageLocation
AddHandler wa.OnGetMessageVcard, AddressOf wa_OnGetMessageVcard
AddHandler wa.OnGetPhoto, AddressOf wa_OnGetPhoto
AddHandler wa.OnGetPhotoPreview, AddressOf wa_OnGetPhotoPreview
AddHandler wa.OnGetGroups, AddressOf wa_OnGetGroups
AddHandler wa.OnGetSyncResult, AddressOf wa_OnGetSyncResult
AddHandler wa.OnGetStatus, AddressOf wa_OnGetStatus
AddHandler wa.OnGetPrivacySettings, AddressOf wa_OnGetPrivacySettings
AddHandler WhatsAppApi.Helper.DebugAdapter.Instance.OnPrintDebug, AddressOf Instance_OnPrintDebug
wa.Connect()
Dim datFile As String = getDatFileName(WANum)
Dim nextChallenge() As Byte
If (File.Exists(datFile)) Then
Dim foo As String = File.ReadAllText(datFile)
nextChallenge = Convert.FromBase64String(foo)
End If
wa.Login(nextChallenge)
ProcessChat(wa)
End Sub
Public Function SendWA(ByVal MSG As String, Num As String) As Boolean
Dim usrMan As New WhatsUserManager()
Dim tmpUser = usrMan.CreateUser(Num, "User")
wa.SendMessage(Num, MSG)

Return True
End Function
Public Sub Instance_OnPrintDebug(value As Object)
Debug.Print(value)
End Sub
Public Sub wa_OnGetPrivacySettings(settings As Dictionary(Of WhatsApp.VisibilityCategory, WhatsApp.VisibilitySetting))

End Sub
Public Sub wa_OnGetStatus(form As String, type As String, name As String, status As String)

End Sub
Public Function getDatFileName(pn As String) As String
Dim filename As String = String.Format("{0}.next.dat", pn)
Return Path.Combine(Directory.GetCurrentDirectory(), filename)
End Function
Public Sub wa_OnGetSyncResult(index As Integer, sid As String, existingUsers As Dictionary(Of String, String), failedNumbers As String())

End Sub
Public Sub wa_OnGetGroups(groups As WaGroupInfo())

End Sub
Public Sub wa_OnGetPhotoPreview(from As String, id As String, data() As Byte)
File.WriteAllBytes(String.Format("preview_{0}.jpg", from), data)
End Sub
Public Sub wa_OnGetPhoto(from As String, id As String, data() As Byte)
File.WriteAllBytes(String.Format("{0}.jpg", from), Data)
End Sub
Public Sub wa_OnGetMessageVcard(from As String, id As String, name As String, data() As Byte)
File.WriteAllBytes(String.Format("{0}.vcf", name), Data)
End Sub
Public Sub wa_OnGetMessageLocation(from As String, id As String, lon As Double, lat As Double, url As String, name As String, preview() As Byte)
File.WriteAllBytes(String.Format("{0}{1end sub.jpg", lat, lon), preview)
End Sub
Public Sub wa_OnGetMessageVideo(from As String, id As String, filename As String, fileSize As Integer, url As String, preview() As Byte)
OnGetMedia(filename, url, preview)
End Sub
Public Sub OnGetMedia(file As String, url As String, data() As Byte)
My.Computer.FileSystem.WriteAllBytes(String.Format("preview_{0}.jpg", file), data, False)
Dim WA_WC As New WebClient
WA_WC.DownloadFileAsync(New Uri(url), file, 0)
End Sub
Public Sub wa_OnGetMessageAudio(from As String, id As String, filename As String, filesize As Integer, url As String, preview() As Byte)
OnGetMedia(filename, url, preview)
End Sub
Public Sub wa_OnGetMessageImage(from As String, id As String, filename As String, size As Integer, url As String, preview() As Byte)
OnGetMedia(filename, url, preview)
End Sub
Public Sub wa_OnGetPaused(from As String)

End Sub
Public Sub wa_OnGetTyping(from As String)

End Sub
Public Sub wa_OnGetLastSeen(from As String, lastseen As DateTime)

End Sub
Public Sub wa_OnGetMessageReceivedServer(from As String, id As String)

End Sub
Public Sub wa_OnGetMessageReceivedClient(from As String, id As String)

End Sub
Public Sub wa_OnGetGroupParticipants(gjid As String, jids() As String)

End Sub
Public Sub wa_OnGetPresence(from As String, type As String)

End Sub
Public Sub wa_OnNotificationPicture(type As String, jid As String, id As String)

End Sub
Public Sub wa_OnGetMessage(node As ProtocolTreeNode, from As String, id As String, name As String, message As String, receipt_sent As Boolean)
Dim Number As String = Split(from, "@")(0)

End Sub
Private Sub wa_OnLoginFailed(data As String)
End
End Sub
Private Sub wa_OnLoginSuccess(phoneNumber As String, data() As Byte)
' next password
Dim sdata As String = Convert.ToBase64String(data)
My.Computer.FileSystem.WriteAllText(getDatFileName(WANum), sdata, False)
End Sub
Private Sub ProcessChat(wa As WhatsApp)
Dim thRecv = New Thread(AddressOf ProcessChatT) : thRecv.IsBackground = True
thRecv.Start()
End Sub
Sub ProcessChatT(t)
Try
While wa IsNot Nothing
wa.PollMessages()
Thread.Sleep(100)
Continue While
End While
Catch generatedExceptionName As ThreadAbortException
End Try

End Sub
End Module

Una vez agregado este código vamos a agregar código al form principal para terminar con este ejemplito. (Mi from se llama Principal, ojo)

Private Sub Principal_Load(sender As Object, e As EventArgs) Handles MyBase.Load
InitWA("Tutorial By 79137913")
Call SendWA("Prueba 1", "ACAPONEELNUMEROALQUEQUIERASMANDARELMENSAJE")
End Sub

En InitWA pones el Nick que quieras y en SendWa pones el mensaje seguido del número al que quieras enviar un mensaje.

Nota: para que no sea spam ambas personas deben estar en sus respectivas listas de contactos, por ende agrega al sistema a tu lista de contactos antes de enviar mensajes para que no se bloquee el usuario.

 

Autor: 79137913

Comentarios

Comentarios

27 abril, 2016

Posts Relacionados

0 comentarios

Comentarios en: [VB] Porteo de la implementación del api de WhatsApi a VB.NET

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *