How to Receive SMS from WhatsApp in C#
By FoxLearn 10/6/2024 3:19:49 AM 9.59K
How to Receive SMS from WhatsApp in C#
Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "ReceiveSmsWhatsApp" and then click OK
Download WhatsApp API. This is an API written in C# that can be utilized in any .NET language. It is a fork of WhatsAPINet and is based on Chat API.
You can use NuGet Package Manager to install a WhatsApp library.
Open the Package Manager Console and run.
1 |
Install-Package WhatsAppNET |
Drag and drop the Label, TextBox, Button controls from the Visual Studio toolbox onto your form designer, then design your form as shown below.
Here's a basic example of how to set up a listener for incoming messages.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; using WhatsAppApi; namespace ReceiveSmsWhatsApp { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private delegate void UpdateTextBox(TextBox textbox, string value); private void UpdateDataTextBox(TextBox textbox, string value) { //Update textbox textbox.Text += value; } private void btnReceive_Click( object sender, EventArgs e) { txtStatus.Clear(); //Create a new thread var thread = new Thread(t => { UpdateTextBox textbox = UpdateDataTextBox; WhatsApp wa = new WhatsApp(txtPhone.Text, txtPassword.Text, txtName.Text, true ); wa.OnConnectSuccess += () => { if (txtStatus.InvokeRequired) Invoke(textbox, txtStatus, "Connected..." ); wa.OnLoginSuccess += (phone, data) => { if (txtStatus.InvokeRequired) Invoke(textbox, txtStatus, "\r\nConnection success !" ); wa.PollMessages(); }; wa.OnGetMessage += (node, from , id, name, message, receipt_sent) => { if (txtStatus.InvokeRequired) Invoke(textbox, txtStatus, string .Format( "\r\nName = {0}, Message = {1}" , name, message)); }; wa.OnLoginFailed += (data) => { if (txtStatus.InvokeRequired) Invoke(textbox, txtStatus, string .Format( "\r\nLogin failed {0}" , data)); }; wa.Login(); }; wa.OnConnectFailed += (ex) => { if (txtStatus.InvokeRequired) Invoke(textbox, txtStatus, string .Format( "\r\nConnect failed {0}" , ex.StackTrace)); }; wa.Connect(); }) { IsBackground = true }; thread.Start(); } } } |
Receiving WhatsApp messages in C# can be achieved through libraries that facilitate interaction with the WhatsApp service.
VIDEO TUTORIAL
- How to update UI from another thread in C#
- How to Search DataGridView by using TextBox in C#
- How to read and write to text file in C#
- How to save files using SaveFileDialog in C#
- How to Print a Picture Box in C#
- How to zoom an image in C#
- How to Print DataGridView with Header & Footer with Landscape in C#
- How to Get all Forms and Open Form with Form Name in C#