Sending WhatsApp Text And Media Messages Using C# Console App

Emmanuel

Administrator
Staff member
May 11, 2020
141
28
84
Introduction
  • Recently, Whatsapp has been used for many business scenarios.
  • Imagine a use case for an ISP provider.
  • You need to send monthly bills to customers registered Email ID.
  • How great would it be if the bills are generated through an automated system and programmatically send to the customer's registered mobile WhatsApp number?
  • We will be creating a sample POC app to send Whatsapp text message programmatically using C# and .NET Core.
We are using the C# Console app to send WhatsApp text and media message in less than 30 min and a few lines of code.

Output

Here is the output of my WhatsApp screen:


Whatsapp Media


Implementation

Step 1 - Create a new console app.

I am using VS 2019 IDE for my console app project.

Create a new project in .NET Core of type console app.


Give a Valid name to your project and click the create button.

Your console project is now created and we are ready to go for the next step.

Step 2 - Install and Configure Twilio Whatsapp Sandbox environment.

Navigate through Nuget package manager and Browse for Nuget packages.

Search for the Twilio package and install it in our console application.


Once the NuGet package and its dependencies are successfully installed, create your free account on Twilio using the below link

Please, Log in or Register to view URLs content!

This account would be used for our project configuration and setting up the WhatsApp sandbox environment.

Once the account has been set up successfully our next step is to set up a sandbox environment to start sending WhatsApp message.

Navigate to
Please, Log in or Register to view URLs content!
and send messages from our WhatsApp number to the number display on-screen with a sandbox name.

Complete all the 4 steps to enable 2-way communication.

Once all the steps are completed, we are set to use this account information in our C# code and send WhatsApp messages.

Step 3 - Send Whatsapp message from the Console app.

Navigate through the console settings of your app using the below URL.

Please, Log in or Register to view URLs content!

Capture account SSID and auth toke, this would be used in our C# Code to Validate user.

Import Twilio libraries in our code:

Code:
Please, Log in or Register to view codes content!
All the below code in our Main function

Code:
Please, Log in or Register to view codes content!
From Whatsapp number is the number displayed on the screen during WhatsApp sandbox environment setting.

To Whatsapp number is our Whatsapp number where we would need to send a message.

Both these numbers should be entered in E.164 format.

This is a universal format of phone numbers.

More details on E.164 format. kindly refer
Please, Log in or Register to view URLs content!
.

Now run your console app and you would receive the body text message on your WhatsApp number.

Change the text and rerun it. You would need a new text message received on your Whatsapp number.

We have successfully configured and send Whatsapp text message.

Step 4 - Send Whatsapp Media files from the Console app.

Navigate through the console settings of your app using the below URL.

Please, Log in or Register to view URLs content!

Capture account sid and auth token, this would be used in our C# Code to Validate user.

Import Twilio libraries in our code:

Code:
Please, Log in or Register to view codes content!
The Media Url file should be hosted on the server and accessible using the link.

From Whatsapp number is the number displayed on the screen during the WhatsApp sandbox environment setting.

To Whatsapp number is our Whatsapp number where we would need to send a message.

Both these numbers should be entered in
Please, Log in or Register to view URLs content!
.

Now run your console app and you would receive media file on your Whatsapp number.

We can send images or any other file types supported by Whatsapp.

We can integrate this in your real-world application and automate sending a WhatsApp notification and files as per your business needs.

Summary

Through this article, we have learned how to send simple text messages and media files on Whatsapp using C#.

This windows exe can be scheduled in any scheduler and would help us to automate customer alerts and notifications .

Thanks a lot for reading. I hope you liked this article. Please share your valuable suggestions and feedback. Write in the comment box in case you have any questions. Have a good day!