How to make a WhatsApp Messenger in VB.NET
By FoxLearn 12/1/2024 1:56:36 PM 29.8K
This article demonstrates how to use the WhatsAppApi
library in VB.NET to create a basic desktop application for sending and receiving messages via WhatsApp.
How to make a WhatsApp Messenger in VB.NET?
Open Visual Studio, then click New Project, then select Visual Basic on the left, then Windows and then select Windows Forms Application. Name your project "WhatsAppVB.Net" and then click OK
Download WhatsApp API. This API, originally written in C#, is compatible with any .NET language. It is a fork of WhatsAPINet, which itself is based on Chat API.
Drag and drop the Label, TextBox and Button controls from the Visual Toolbox onto your form designer, then design your form as shown below.
Add a click event handler to your form as shown below.
Imports System.Threading Imports WhatsAppApi Imports WhatsAppApi.ApiBase Imports WhatsAppApi.Helper Public Class Form1 Private wa As WhatsApp Private Delegate Sub UpdateTextBox(ByVal textbox As TextBox, ByVal value As String) Public Sub UpdateDataTextBox(ByVal textbox As TextBox, ByVal value As String) Dim box As TextBox = textbox box.Text = (box.Text & value) End Sub Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click If ((Not String.IsNullOrEmpty(Me.txtMessage.Text) AndAlso (Not Me.wa Is Nothing)) AndAlso (Me.wa.ConnectionStatus = CONNECTION_STATUS.LOGGEDIN)) Then Me.wa.SendMessage(Me.txtTo.Text, Me.txtMessage.Text) Dim txtStatus As TextBox = Me.txtStatus txtStatus.Text = (txtStatus.Text & String.Format(Environment.NewLine & "{0}:{1}", txtName.Text, Me.txtMessage.Text)) Me.txtMessage.Clear() Me.txtMessage.Focus() End If End Sub Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click Dim thread = New Thread(AddressOf ThreadTask) thread.IsBackground = True thread.Start() End Sub Private Sub ThreadTask() Dim textbox As UpdateTextBox = New UpdateTextBox(AddressOf Me.UpdateDataTextBox) Me.wa = New WhatsApp(Me.txtPhoneNumber.Text, Me.txtPassword.Text, Me.txtName.Text, True, False) AddHandler Me.wa.OnConnectSuccess, Sub() If Me.txtStatus.InvokeRequired Then Dim objArray1 As Object() = New Object() {Me.txtStatus, "Connected..."} Me.Invoke(textbox, objArray1) End If AddHandler Me.wa.OnLoginSuccess, Sub(ByVal phone As String, ByVal data As Byte()) If Me.txtStatus.InvokeRequired Then Dim args As Object() = New Object() {Me.txtStatus, Environment.NewLine & "Login success !"} Me.Invoke(textbox, args) End If While Me.wa IsNot Nothing Me.wa.PollMessages() Thread.Sleep(200) Continue While End While End Sub AddHandler Me.wa.OnLoginFailed, Sub(ByVal data As String) If Me.txtStatus.InvokeRequired Then Dim args As Object() = New Object() {Me.txtStatus, String.Format(Environment.NewLine & "Login failed: {0}", data)} Me.Invoke(textbox, args) End If End Sub AddHandler Me.wa.OnGetMessage, Sub(ByVal node As ProtocolTreeNode, ByVal from As String, ByVal id As String, ByVal name As String, ByVal message As String, ByVal receipt_sent As Boolean) If Me.txtStatus.InvokeRequired Then Dim args As Object() = New Object() {Me.txtStatus, String.Format(Environment.NewLine & "{0}:{1}", name, message)} Me.Invoke(textbox, args) End If End Sub Me.wa.Login(Nothing) End Sub AddHandler Me.wa.OnConnectFailed, Sub(ByVal ex As Exception) If Me.txtStatus.InvokeRequired Then Dim args As Object() = New Object() {Me.txtStatus, String.Format(Environment.NewLine & "Connect failed: {0}", ex.StackTrace)} Me.Invoke(textbox, args) End If End Sub Me.wa.Connect() End Sub End Class
This VB.NET application demonstrates a basic implementation of a WhatsApp messaging client using the WhatsAppApi
library.
VIDEO TUTORIAL