Messaging Methods in CxPerium .NET SDK
The messaging-related methods in CxPerium .NET SDK are found within the Messages object. To access this object, your class must inherit from the BaseWhatsAppDialog class.
The BaseWhatsAppDialog class provides the essential properties and methods used during the messaging flow. Once this class is inherited, you can access the Messages object to perform operations such as sending, receiving, and processing messages.
SendMessage
The this.Messages.SendMessage
method is used to send a text-based message to the user. This method has two overloaded parameters:
- First Parameter: Specifies the message content to be sent to the user.
- Second Parameter (isPreviewUrl): Optional. If set to
true
, WhatsApp will display a preview for any link present in the message.
Example Usage
First Example:
this.Messages.SendMessage(“Hello World”);
Second Example:
this.Messages.SendMessage(“Hello CXPerium https://www.cxperium.com”, true);
SendButtonMessage
If a button needs to be included in the message, the SendButtonMessage
method is used. The buttons to be sent can be created as a List. In the Button object, the Reply object specifies the text (Title) that appears on the button and the unique Id that WhatsApp will receive when the button is clicked.
Method Parameters
- Message: The message to be sent to the user.
- Header: The text displayed at the top of the message.
- Footer: The text displayed at the bottom of the message.
- Buttons: The buttons displayed at the bottom of the message. A maximum of three buttons can be added.
Example Usage
Using Header Text:
public class DemoDialog : BaseWhatsAppDialog, IWhatsAppDialog
{
public void RunDialog()
{
List buttons = new List();
buttons.Add(new Button() { IsVisible = true, Reply = new Reply() { Title = “Yes Button”, Id = “#yes_button” } });
buttons.Add(new Button() { IsVisible = true, Reply = new Reply() { Title = “No Button”, Id = “#no_button” } });
this.Messages.SendButtonMessage(“Hello World”, “Header Text”, “Footer Text”, buttons);
}
}
Using an Image or PDF:
public class DemoDialog : BaseWhatsAppDialog, IWhatsAppDialog
{
public void RunDialog()
{
List buttons = new List();
buttons.Add(new Button() { IsVisible = true, Reply = new Reply() { Title = “Yes Button”, Id = “#yes_button” } });
buttons.Add(new Button() { IsVisible = true, Reply = new Reply() { Title = “No Button”, Id = “#no_button” } });this.Messages.SendButtonMessage("Hello World", new System.Uri("https://upload.wikimedia.org/wikipedia/commons/7/70/Example.png"), "Footer Text", buttons); }
}
💡 Tip:
The Id value provided in the Reply object inside the Button object can be used in CXPerium’s dialog configuration under the Regex section. This allows defining which dialog should be triggered when a user clicks the button.
SendImageMessage
To send an image, the SendImageMessage
method has three different versions:
- SendImageMessageByFile(FileInfo file, string caption):
- Sends an image file from the local computer.
- SendImageMessageById(string mediaId, string caption):
- Sends an image using a previously uploaded media ID in WhatsApp.
- SendImageMessageByUrl(Uri url, string caption):
- Sends an image file using a URL.
Example Usage
Sending via File:
FileInfo file = new FileInfo(“C:\images\example.jpg”);
this.Messages.SendImageMessageByFile(file, “Here is an image”);
Sending via Media ID:
string mediaId = “123456789”;
this.Messages.SendImageMessageById(mediaId, “Here is an image”);
Sending via URL:
Uri url = new Uri(“https://upload.wikimedia.org/wikipedia/commons/7/70/Example.png”);
this.Messages.SendImageMessageByUrl(url, “Here is an image”);
SendLocationMessage
To send a location message to the user, use the SendLocationMessage
method. This method takes latitude, longitude, name, and address information.
Method Parameters:
- lat: Latitude of the location to be sent.
- lon: Longitude of the location to be sent.
- name: Name of the location.
- address: Address of the location.
Example Usage
this.Messages.SendLocationMessage(40.712776, -74.005974, “New York”, “New York, NY, USA”);
This example sends a location message to the user with New York’s coordinates.
SendDocumentMessage
To send a PDF or document, the SendDocumentMessage
method has three different versions:
- SendDocumentMessageByFile(FileInfo file, string caption, string fileName):
- Sends a document file from the local computer.
- SendDocumentMessageById(string mediaId, string caption, string fileName):
- Sends a document using a previously uploaded media ID in WhatsApp.
- SendDocumentMessageByUrl(Uri link, string caption, string fileName):
- Sends a document file using a URL.
Example Usage
Sending via File:
FileInfo file = new FileInfo(“C:\documents\example.pdf”);
this.Messages.SendDocumentMessageByFile(file, “Here is a document”, “example.pdf”);
Sending via Media ID:
string mediaId = “987654321”;
this.Messages.SendDocumentMessageById(mediaId, “Here is a document”, “example.pdf”);
Sending via URL:
Uri link = new Uri(“https://example.com/documents/example.pdf”);
this.Messages.SendDocumentMessageByUrl(link, “Here is a document”, “example.pdf”);
SendVideoMessage
When you want to send a video, you can use three different versions of the SendVideoMessage
method:
- SendVideoMessageByFile(FileInfo file, string caption):
- Sends a video file from your local storage.
- SendVideoMessageById(string mediaId, string caption):
- Sends a previously uploaded media file using its WhatsApp media ID.
- SendVideoMessageByUrl(Uri url, string caption):
- Sends a video file via a URL.
Example Usages
Sending a file:
FileInfo file = new FileInfo(“C:\videos\example.mp4”);
this.Messages.SendVideoMessageByFile(file, “Here is a video”);
Sending by media ID:
string mediaId = “654321987”;
this.Messages.SendVideoMessageById(mediaId, “Here is a video”);
Sending by URL:
Uri url = new Uri(“https://example.com/videos/example.mp4”);
this.Messages.SendVideoMessageByUrl(url, “Here is a video”);
SendListMessage
When you want to send a list message, you can use three different versions of the SendListMessage
method:
- SendListMessage(string message, string headerText, string footerText, string buttonText, List rows):
- Sends a list message with a header and footer. The list items are defined as rows (Row).
- SendListMessage(string message, string headerText, string footerText, string buttonText, Listsections):
- Sends a list message with a header and footer. The list items are structured as sections (Section) containing rows (Row).
- SendListMessage(string message, Uri url, string footerText, string buttonText, Listsections):
- Sends a list message with a URL-based header. The list items are structured as sections (Section) containing rows (Row).
Example Usages
Using Rows:
List rows = new List
{
new Row() { Id = “1”, Title = “Option 1”, Description = “Description for option 1” },
new Row() { Id = “2”, Title = “Option 2”, Description = “Description for option 2” }
};
this.Messages.SendListMessage(“Choose an option”, “Header Text”, “Footer Text”, “View Options”, rows);
Using Sections and Rows:
List sections = new List
{
new Section()
{
Title = “Section 1”,
Rows = new List
{
new Row() { Id = “1”, Title = “Option 1”, Description = “Description for option 1” },
new Row() { Id = “2”, Title = “Option 2”, Description = “Description for option 2” }
}
},
new Section()
{
Title = “Section 2”,
Rows = new List
{
new Row() { Id = “3”, Title = “Option 3”, Description = “Description for option 3” }
}
}
};
this.Messages.SendListMessage(“Choose an option”, “Header Text”, “Footer Text”, “View Options”, sections);
Using URL-Based Header:
Uri url = new Uri(“https://example.com/image.jpg”);
List sections = new List
{
new Section()
{
Title = “Section 1”,
Rows = new List
{
new Row() { Id = “1”, Title = “Option 1”, Description = “Description for option 1” },
new Row() { Id = “2”, Title = “Option 2”, Description = “Description for option 2” }
}
}
};
this.Messages.SendListMessage(“Choose an option”, url, “Footer Text”, “View Options”, sections);