Windows Forms: How to Receive SMS from WhatsApp in C#
By FoxLearn 7/12/2017 8:38:52 PM 8.87K
How to receive WhatsApp Message using c#.
Step 1: 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
Step 2: Download WhatsApp API. This is an API written in C# but it can be used in any .NET language. It's a fork from WhatsAPINet, which is based on Chat API
Step 3: Design your form as below
Step 4: Add code to handle your form
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(); } } }
VIDEO TUTORIALS
- Windows Forms: Advanced Filter DataGridView in C#
- Windows Forms: Progress Bar in C#
- How to create a Lucky Draw games in C#
- Windows Forms: Youtube Search with Paging in C#
- Windows Forms: How to Create a Random Password Generator in C#
- Windows Forms: Tag Control in C#
- Windows Forms: Wait Form Dialog in C#
- Windows Forms: Text to speech in C#
Categories
Popular Posts
Visual Studio: Auto increment version
03/22/2024
What is ARM architecture?
03/11/2024