Branch | Status | Recommended NuGet package version |
---|---|---|
master | Preview available via MyGet (version 1.0.0-alpha3) |
This is part of the Bot Builder Community project which contains Bot Framework Components and other projects / packages for use with Bot Framework Composer and the Bot Builder .NET SDK v4.
The Infobip Viber adapter enables receiving and sending Viber messages. The Infobip Viber adapter allows you to add an additional endpoint to your bot for receiving Viber messages. The Infobip endpoint can be used in conjunction with other channels meaning, for example, you can have a bot exposed on out of the box channels such as Facebook and Teams, but also via an Infobip (as well as side by side with the Google / Twitter Adapters also available from the Bot Builder Community Project).
Incoming Viber message requests are transformed, by the adapter, into Bot Framework Activites and then when your bot sends outgoing activities, the adapter transforms the outgoing Activity into an Infobip OMNI failover messages.
The adapter currently supports the following scenarios:
- Send/receive text messages
- Send rich messages (text, image and button) - More details about rich messages over Viber can be found here
- Verification of incoming Infobip requests
- Receive delivery reports
- Callback data - You can add some data in every message and that data will be returned to bot in delivery report for that message
- Full incoming request from Infobip is added to the incoming activity as ChannelData
Available via NuGet package Bot.Builder.Community.Adapters.Infobip.Viber
Install into your project using the following command in the package manager;
PM> Install-Package Bot.Builder.Community.Adapters.Infobip.Viber
- Prerequisites
- Set the Infobip Viber options
- Wiring up the Infobip Viber adapter in your bot
- Incoming Viber message requests to Bot Framework Activity mapping - Learn how incoming request types are handled by the adapter and the activities received by your bot.
- Outgoing Bot Framework Activity to Infobip Viber message mapping - Learn how outgoing Bot Framework activities are handled by the adapter.
- Callback data - The following section will demonstrate how to use callback data
- Useful links
You need to contact Infobip support which will you help with Viber approvall procedure. More details available here.
On the end of process you will get following parameters:
- Api key - will be used for requests authentication and authorization
- Base URL - endpoint on which messages will be sent
- Viber sender - will be used as sender name from which will be sent Viber outgoing messages
- Viber scenario key - we are using OMNI failover API which can have multiple scenarios. This one will be used for sending Viber messages. For more details you can check here.
Also you will need to provide:
- App secret - will be used for incoming request authentication
- Bot URL - Infobip will forward all incoming Viber messages on this endpoint
To authenticate the requests, you'll need to configure the Adapter with the Base URL, API key, App secret, Viber sender, Viber scenario key.
You could create in the project an appsettings.json
file to set the Infobip options as follows:
{
"InfobipApiBaseUrl": "",
"InfobipApiKey": "",
"InfobipAppSecret": "",
"InfobipViberSender": "",
"InfobipViberScenarioKey": ""
}
After you completed the configuration of your Infobip Viber adapter, you need to wire up the Infobip Viber adapter into your bot.
Install into your project using the following command in the package manager;
PM> Install-Package Bot.Builder.Community.Adapters.Infobip.Viber
Create a new class that inherits from the InfobipViberAdapter class. This class will act as our adapter for the Infobip Viber channel. It includes error handling capabilities (much like the BotFrameworkAdapterWithErrorHandler class already in the sample, used for handling requests from Azure Bot Service).
public class InfobipViberAdapterWithErrorHandler: InfobipViberAdapter
{
public InfobipViberAdapterWithErrorHandler(InfobipViberAdapterOptions infobipOptions, IInfobipViberClient infobipViberClient, ILogger<InfobipViberAdapterWithErrorHandler> logger)
: base(infobipViberOptions, infobipViberClient, logger)
{
OnTurnError = async (turnContext, exception) =>
{
OnTurnError = async (turnContext, exception) =>
{
// Log any leaked exception from the application.
logger.LogError($"Exception caught : {exception.Message}");
// Send a catch-all apology to the user.
await turnContext.SendActivityAsync("Sorry, it looks like something went wrong.");
};
};
}
}
You will also need to add the following using statements.
using Bot.Builder.Community.Adapters.Infobip.Viber;
using Microsoft.Extensions.Logging;
You now need to create a new controller which will handle requests for incoming Viber messages and delivery reports, on a new endpoint 'api/infobip/viber' instead of the default 'api/messages' used for requests from Azure Bot Service Channels. By adding an additional endpoint to your bot, you can accept requests from Bot Service channels (or additional adapters), as well as from Infobip, using the same bot.
[Route("api/infobip/viber")]
[ApiController]
public class InfobipController : ControllerBase
{
private readonly InfobipViberAdapter Adapter;
private readonly IBot Bot;
public InfobipController(InfobipViberAdapter adapter, IBot bot)
{
Adapter = adapter;
Bot = bot;
}
[HttpPost]
public async Task PostAsync()
{
// Delegate the processing of the HTTP POST to the adapter.
// The adapter will invoke the bot.
await Adapter.ProcessAsync(Request, Response, Bot);
}
}
You will also need to add the following using statements.
using Bot.Builder.Community.Adapters.Infobip.Viber;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using System.Threading.Tasks;
Add the following line into the ConfigureServices method within your Startup.cs file, which will register your Infobip adapter with two dependencies and make it available for your new controller class. The configuration settings, described in the previous step, will be automatically used by the adapter.
//Add dependencies for Infobip Viber Adapter
services.AddSingleton<InfobipViberAdapterOptions>();
services.AddSingleton<IInfobipViberClient, InfobipViberClient>();
// Add Infobip Viber Adapter with error handler
services.AddSingleton<InfobipViberAdapter, InfobipViberAdapterWithErrorHandler>();
Once added, your ConfigureServices method shold look like this.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// Create the Bot Framework Adapter with error handling enabled.
services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();
//Add dependencies for Infobip Viber Adapter
services.AddSingleton<InfobipViberAdapterOptions>();
services.AddSingleton<IInfobipViberClient, InfobipViberClient>();
// Add Infobip Viber Adapter with error handler
services.AddSingleton<InfobipViberAdapter, InfobipViberAdapterWithErrorHandler>();
// Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
services.AddTransient<IBot, EchoBot>();
}
You will also need to add the following using statement, in addition to those already present in the Startup.cs file.
using Bot.Builder.Community.Adapters.Infobip.Viber;
All messages sent by end-user Infobip will forward to your endpoint. End-users can send to your bot several different message types. You can read about the different message types in the Infobip developer documentation.
Here are details about common details for all Activity types:
- All activities will have end user subscriber number as ConversationId
- All Infobip request messages will be available in ChannelData
- ActivityId will be Infobip messageId
- All activites will have ChannelId property value equal to InfobipViberConstants.ChannelName
Only incoming supported messages are text messages. Here are how the adapter handles Viber incoming message.
- Viber Message Requests -> Message Activity Text property will contain content of message. TextFormat will be always plain.
activity.Text = response.Message.Text;
activity.TextFormat = TextFormatTypes.Plain;
- Delivery report -> Event Activity For each message that you send out, we will send you a delivery report on Infobip endpoint as Event Activity. Activity name will be "DELIVERY". That payload will be in ChannelData property. More details about payload and statuses are available here. You can use this to check is message delivered to end-user. You can use this to see is your message seen by user.
Each Activity can have multiple attachments and it is converted to Viber messages.
- Message Activity -> Viber text Message If you want send Viber text message add message content to Text property of Activity. Infobip adapter will just pass that message without modification.
var message = "Some dummy text";
activity.Text = message;
//or
activity = MessageFactory.Text(message);
- Message Activity -> Viber rich Message If you want send rich message you should add InfobipOmniFailoverMessage to your activity. For Viber rich messages any combination of text, image or buttons is allowed. The only constraint when sending the button that the buttonUrl and buttonText are mandatory! Image URL should be available for Infobip.
var message = new InfobipOmniViberMessage
{
Text = "Hi John Doe!",
ImageUrl = "some-valid-image-url",
ButtonText = "Button text",
ButtonUrl = "Button url"
};
activity.AddInfobipViberMessage(message);
For each message you can add some custom data and that data will be returned to bot in delivery report for that message.
Send callback data:
var callbackData = new Dictionary<string, string>
{
{"data1", "true"},
{"data2", "12"}
};
activity.AddInfobipCallbackData(callbackData);
Get callback data from delivery report:
var callbackData = activity.Entities
.First(x => x.Type == InfobipEntityType.CallbackData);
var sentData = callbackData.GetAs<Dictionary<string, string>>();