Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a rest api call in microsoft bot framework

I need a bot that takes users input, uses it as an id to some third party rest api call and posts back a response. I've looked through Microsoft documentation but didn't find any examples on how to program that request-response process.

Any examples or useful links would be appreciated

like image 633
J.Doe Avatar asked Jan 17 '18 08:01

J.Doe


2 Answers

As Ashwin said, A bot is just a web API and you are just sending/receiving requests as you would with any web API. Below is some documentation that should help get you started.

Basic Overview
Create a bot with the Bot Connector service
API Reference

like image 181
D4RKCIDE Avatar answered Oct 15 '22 19:10

D4RKCIDE


Adding to Jason's answer, since you wanted to make a REST api call, take a look at this code :

public class RootDialog : IDialog<object>
{
    public Task StartAsync(IDialogContext context)
    {
        context.Wait(MessageReceivedAsync);

        return Task.CompletedTask;
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {
        var activity = await result as Activity;

        // User message
        string userMessage = activity.Text;
        try
        {
            using (HttpClient client = new HttpClient())
            {
                //Assuming that the api takes the user message as a query paramater
                string RequestURI = "YOUR_THIRD_PARTY_REST_API_URL?query=" + userMessage ;
                HttpResponseMessage responsemMsg = await client.GetAsync(RequestURI);
                if (responsemMsg.IsSuccessStatusCode)
                {
                    var apiResponse = await responsemMsg.Content.ReadAsStringAsync();

                    //Post the API response to bot again
                    await context.PostAsync($"Response is {apiResponse}");

                }
            }
        }
        catch (Exception ex)
        {

        }
        context.Wait(MessageReceivedAsync);
    }
}

Once you get the input from user, you can make a REST call and then after you get the response back from API, post it back to the user using the context.PostAsync method.

like image 32
Ashwin Kumar Avatar answered Oct 15 '22 20:10

Ashwin Kumar