Sending WhatsApp Text And Media Messages Using C# Console App

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 Text Messages Using C#

WhatsApp Media

WhatsApp Media Messages Using C#

Sending WhatsApp Text/Media Messages Using C#

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.

C# 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.

Twilio WhatsApp Sandbox

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

https://www.twilio.com/

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 https://www.twilio.com/console/sms/WhatsApp/learn 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.

https://www.twilio.com/console

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

Import Twilio libraries in our code:

using System;  
using Twilio;  
using Twilio.Types;  
using Twilio.Rest.Api.V2010.Account;  
using System.Linq;

All the below code in our Main function

{  
    const string accountSid = "specify your ssid here";  
    const string authToken = "specify auth token here";  
    TwilioClient.Init(accountSid, authToken);  
    var message = MessageResource.Create(from: new Twilio.Types.PhoneNumber("WhatsApp: from number in E.164 format "), body: "Good morning Prasad", to: new Twilio.Types.PhoneNumber("WhatsApp:TO number in E.164 format "));  
    Console.WriteLine(message.Sid);  
}

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 here.

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.

https://www.twilio.com/console

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

Import Twilio libraries in our code:

using System;  
using Twilio;  
using Twilio.Types;  
using Twilio.Rest.Api.V2010.Account;  
using System.Linq;  
namespace WhatsApp {  
    class Program {  
        static void Main(string[] args)
        {  
            const string accountSid = "AC11c88816fac8ad6f08aae6b673140a94";  
            const string authToken = "6b80260460bd0fa8ff2f3d17a378c38f";  
            TwilioClient.Init(accountSid, authToken);  
            var mediaUrl = new [] {  
                new Uri("Media Url")  
            }.ToList();  
            var message = MessageResource.Create(mediaUrl: mediaUrl, from: new Twilio.Types.PhoneNumber("WhatsApp:+14155238886"), body: "Profile", to: new Twilio.Types.PhoneNumber("WhatsApp:+919819025927"));  
            Console.WriteLine(message.Sid);  
        }  
    }  
}  

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 E.164 format.

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!


Similar Articles