Microsoft Bot Builder activity.text POST到webhook,REST作为JSON有效负载

问题描述 投票:0回答:1

我想使用Microsoft Bot Builder创建一个bot,它将收到的消息发送到webhook端点。我尝试结合搜索周围的一些例子,但无济于事。我需要一些帮助来将用户输入文本作为带有文本值的json有效负载发送到webhook,这是我现在拥有的代码:

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);
        }
    }
}

这是基于https://docs.microsoft.com/en-us/bot-framework/dotnet/bot-builder-dotnet-quickstart

当我在使用Fiddler进行监控时测试它时,我可以看到机器人和客户端之间的通信,但从未发现任何REST http请求。我不确定这是最好的方法,并且会喜欢一些反馈。

对此端点的成功POST如下所示:

POST /webhook/.../IncomingWebhook/.../... HTTP/1.1
Host: outlook.office.com
User-Agent: insomnia/5.12.4
Content-Type: application/json
Accept: application/json
Content-Length: 26
{
    "text":"Hello world!"
}

我感谢您的帮助!

c# json rest bots botframework
1个回答
1
投票

看起来你实际上并没有在最后提出请求。将代码更改为:

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);
    }
© www.soinside.com 2019 - 2024. All rights reserved.