How to relax api-call Microsoft Bot

I need a bot that introduces the user, uses it as an identifier for some third-party api call to rest, and sends a response. I looked at the Microsoft documentation, but did not find examples of how to program this request-response process.

Any examples or useful links would be appreciated.

+4
source share
2 answers

Adding the answer to Jason, since you wanted to make a rip api call, 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 input from the user, you can make a REST call, and then after receiving a response from the API, send it back to the user using the method context.PostAsync.

+2
source

, - -API, / , -API. , .


Bot Connector
API

+3

Source: https://habr.com/ru/post/1692297/


All Articles