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 IDs.
- 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 messages programmatically using C# and .NET Core.
We are using the C# Console app to send WhatsApp text and media messages 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 the 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
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 messages.
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 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 a 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 the 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 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.
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 sent Whatsapp text messages.
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 the 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.
Now run your console app and you would receive a 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.
No comments:
Post a Comment