Rich Messages
From version 4.5, OSCOVA supports new formats of responses in the form of rich message elements. The following are some of the new rich message elements available at the time of writing this article.
Message Type | Description |
---|---|
Quick Replies Message | Contains a list of quick replies the user can choose from. |
Text Message | Provides a list of texts from which a random response can be selected |
Video Message | Provides a link or file path to a video. |
Image Message | Provides a link or file path to an image. |
Audio Message | Provides a link or file path to an audio. |
Basic Card Message | Contains information like Title , an Image url and a Description |
Html Message | HTML Content |
Markdown Message | Markdown content |
The above message types are detected by checking the value of the Type
property on messages.
Note
The application handling the messages is solely responsible as to how to render the above message types.
Example rich message responses
The following codes and Workspace graph just show how to create a response with a particular message type.
Note
A single response from the Bot can have multiple messages attached to it.
Quick Replies Message
[Expression("What pizza sizes are available?")]
public void PizzaSizeCommit(Context context, Result result)
{
var response = new Response();
response.Messages.Add(
new QuickReplyMessage
{
Title = "We have the following sizes available.",
Replies = new List<string>
{
"Small",
"Medium",
"Large"
}
});
result.SendResponse(response);
}
Basic Card Message
[Expression("I need more details about the product")]
public void ExpandInfo(Context context, Result result)
{
var response = new Response();
response.Messages.Add(
new BasicCardMessage()
{
Title = "Awesome Product 2000",
ImageUrl = "https://example.com/icon.png",
Description = "This product is just amazing for your needs."
});
result.SendResponse(response);
}
Creating a custom Message
type
To create a custom message type, derive a class from the Message
class type, specify a string that gives the message type value and then add relevant or required properties to it as shown below.
public class VideoCardMessage : Message
{
public VideoCardMessage() : base("Video-Card-Message") { }
public string Title { get; set; }
public string VideoUrl { get; set; }
public string Description { get; set; }
public string ThumbnailUrl { get; set; }
}
The Message
class also exposes a Platform
property that can be used to specify the name of the particular platform for which the response was generated.
Process Rich Messages
It is important to note that each Message
type have to be rendered differently on various UIs and platforms. For this, it up to the developer to check for certain message types and choose how they are displayed to the user. The following code shows how to check if a message type exists.
var bot = new OscovaBot();
bot.Dialogs.Add(new OrderDialog());
bot.Trainer.StartTraining();
var newUser = bot.CreateUser();
var request = newUser.CreateRequest("I would like to place an order");
//Any new user created.
newUser.ResponseReceived += (sender, args) =>
{
var response = args.Response;
//Iterate and check for message types.
foreach (var message in response.Messages)
{
if (message is TextMessage textMessage)
{
//Do Something
}
else if (message is QuickReplyMessage quickReplyMessage)
{
//Do Something
}
//And so on with other message types...
}
};