C# Microsoft Bot Builder activity.text将REST作为JSON负载发布到webhook
我想使用Microsoft bot Builder创建一个bot,它将接收到的消息发送到webhook端点。我试着结合搜索中发现的一些例子,但没有效果。我需要一些帮助来将用户输入文本作为json有效负载发送到webhook,其值为text,下面是我现在拥有的代码:C# Microsoft Bot Builder activity.text将REST作为JSON负载发布到webhook,c#,json,rest,bots,botframework,C#,Json,Rest,Bots,Botframework,我想使用Microsoft bot Builder创建一个bot,它将接收到的消息发送到webhook端点。我试着结合搜索中发现的一些例子,但没有效果。我需要一些帮助来将用户输入文本作为json有效负载发送到webhook,其值为text,下面是我现在拥有的代码: using System; using System.Net; using System.IO; using System.Threading.Tasks; using Microsoft.Bot.Builder.Dialogs; u
using System;
using System.Net;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using System.Web.Script.Serialization;
namespace Bot_Application1.Dialogs
{
[Serializable]
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;
// calculate something for us to return
int length = (activity.Text ?? string.Empty).Length;
// return our reply to the user
await context.PostAsync($"You sent {activity.Text} which was {length} characters");
//
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://outlook.office.com/webhook/.../IncomingWebhook/.../...");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Accept = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
text = "\"" + activity.Text + "\""
});
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
//
context.Wait(MessageReceivedAsync);
}
}
}
我感谢你的帮助 看起来您最后并没有提出请求。将代码更改为:
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;
// calculate something for us to return
int length = (activity.Text ?? string.Empty).Length;
// return our reply to the user
await context.PostAsync($"You sent {activity.Text} which was {length} characters");
//
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://outlook.office.com/webhook/.../IncomingWebhook/.../...");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Accept = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
text = "\"" + activity.Text + "\""
});
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
//Make the actual request
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
//Get the output
var result = streamReader.ReadToEnd();
}
//
context.Wait(MessageReceivedAsync);
}
您正在形成请求,但没有在最后提出请求。代码缺少HttpWebResponsehttpWebRequest.GetResponse;。请看下面的答案。
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;
// calculate something for us to return
int length = (activity.Text ?? string.Empty).Length;
// return our reply to the user
await context.PostAsync($"You sent {activity.Text} which was {length} characters");
//
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://outlook.office.com/webhook/.../IncomingWebhook/.../...");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Accept = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
text = "\"" + activity.Text + "\""
});
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
//Make the actual request
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
//Get the output
var result = streamReader.ReadToEnd();
}
//
context.Wait(MessageReceivedAsync);
}