我需要一个接受用户输入的机器人,将其用作某个第三方休息api呼叫的id并发回响应.我查看了Microsoft文档,但没有找到有关如何编写请求 - 响应过程的任何示例.
任何示例或有用的链接将不胜感激
添加到 Jason 的答案中,因为您想进行 REST api 调用,所以请看一下以下代码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
获得用户的输入后,您可以进行 REST 调用,然后在从 API 获得响应后,使用该context.PostAsync
方法将其发布回用户。
归档时间: |
|
查看次数: |
4372 次 |
最近记录: |