Web API Channel
The Web API Channel enables developers to launch their bot as service on the specified URL. This creates a Server-Client architecture where the bot along with its dialogs, intents and contexts are held at a single location and multiple clients on or outside network make requests to the Bot.
Walkthrough
In this walkthrough we'll create a simple OSCOVA based bot and host it on the address http://localhost:8086/bot
.
Creating a Console application
To create a Console application in Visual Studio following the steps given below.
- Start Visual Studio 2019 or above
- Select Create a new project and choose Console App
- Click Next
:video videos/create-console-app.mp4
- Name the project BotServer and choose OK.
Importing Syn.Bot.Channels
Now to import Syn.Bot.Channels
to your Console project follow the steps below:
- In Visual Studio, click on Tools, point to NuGet Package Manager
- Select Package Manager Console
- Type
Install-Package Syn.Bot.Channels
All of the required libraries will be imported to your Console project.
Coding
In your project solution create a new file called TestDialog.cs
. This will be our Oscova dialog for testing the Web API.
Within the TestDialog
class add a simple intent as shown below.
internal class TestDialog : Dialog
{
[Expression("Test")]
[Expression("Hello bot")]
public void TestIntent(Context context, Result result)
{
result.SendResponse("Hello Developer!");
}
}
According to the above intent, if the user says Test or Hello bot the bot will respond Hello Developer!.
Now, open the Program.cs
file and add the following code.
private static void Main()
{
var bot = new OscovaBot();
bot.Dialogs.Add(new TestDialog());
bot.Trainer.StartTraining();
var botServer = new WebApiChannel(bot,"http://localhost:8086/bot");
//In case the server generates a log, write it to console.
botServer.Logger.LogReceived += (sender, eventArgs) =>
{
Console.WriteLine(eventArgs.Log);
};
botServer.Start();
//Once the server is started you can call API url using the below format.
//http://localhost:8086/bot?userid={USER-ID}&query={USER-MESSAGE}
//Example: http://localhost:8086/bot?userid=12345&query=test
Console.WriteLine($"Bot Server is online at '{botServer.Url}'. Press any key to stop server.");
Console.ReadLine();
botServer.Stop();
}
In the above code, we first create a new instance of OscovaBot
class and add the previously created TestDialog
. Later we start the bot training.
After which we have create a new instance of WebApiChannel
the takes the OSCOVA bot instance and a URL where the Web API is to be hosted. Finally we call the Start()
method to start the server.
Starting the Web API Channel
Press F5 in Visual Studio or click the Start button. You console application will be launched and if everything goes fine your Web API based Bot server will be hosted on the aforementioned URL.
To check your bot server API after starting the Console application, open any browser be it Firefox
or Chrome
and browse to the URL:
http://localhost:8086/bot?userid=12345&query=test
Where userId
is the ID of user making the request and query
is the user message.
You should see a JSON response that is similar to the following:
{
"success": true,
"botId": "11e7dd8c-bc6e-4e6b-865f-7bfcfff5fb3a",
"userId": "12345",
"query": "test",
"response": "Hello Developer!",
"priority": "normal",
"hint": "",
"confidence": 1
}