How to Receive SMS from WhatsApp in C#

By FoxLearn 10/6/2024 3:19:49 AM   9.07K
To receive WhatsApp messages using C#, you typically need to interact with the WhatsApp Business API or a third-party library, as the official WhatsApp API does not allow direct message reading without proper setup.

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.

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.

receive sms whatsapp in c#

Here's a basic example of how to set up a listener for incoming messages.

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