Skip to content

Latest commit

 

History

History

Bot.Builder.Community.Adapters.Infobip.Viber

Infobip Viber Adapter for Bot Builder v4 .NET SDK - PREVIEW

Build status

Branch Status Recommended NuGet package version
master Build status Preview available via MyGet (version 1.0.0-alpha3)

Description

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

Installation

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

Usage

Prerequisites

You need to contact Infobip support which will you help with Viber approvall procedure. More details available here.

Set the Infobip Viber options

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": ""
}

Wiring up the Infobip Viber adapter in your bot

After you completed the configuration of your Infobip Viber adapter, you need to wire up the Infobip Viber adapter into your bot.

Install the Infobip Viber adapter NuGet package

Install into your project using the following command in the package manager;

PM> Install-Package Bot.Builder.Community.Adapters.Infobip.Viber

Create an Infobip Viber adapter class

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;

Create a new controller for handling Infobip requests

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;

Inject Infobip Viber Adapter In Your Bot Startup.cs

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;

Incoming Viber message requests to Bot Framework Activity mapping

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.

Outgoing Bot Framework Activity to Infobip Viber message mapping

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);

Callback data

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>>();

Useful links