来自MS Bot Framework中聊天机器人的C#HTTP Post请求

最后发布: 2019-03-22 14:48:35


问题

我是C#的新手,所以请不要对我这么辛苦

我试图从MSBOT Framwork聊天机器人向MSFLOW发帖。 post请求触发流向chatbot用户管理器发送电子邮件。

我只是不知道从哪里开始。 我有一个来自Az的基本BOT模板,但我应该如何以及在何处提出请求

非常感谢

c# botframework
回答

使用HttpWebRequest

这是一个使用Http POST请求的示例。

using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestPostExample
    {
        public static void Main()
        {
            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create("http://www.example.com/post");
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            string postData = "This is a test that posts this string to a Web server.";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Console.WriteLine(responseFromServer);
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();
        }
    }
}


回答

首先,Flow需要一个请求 - 当收到HTTP请求时触发: https//docs.microsoft.com/en-us/azure/connectors/connectors-native-reqres触发器可以通过任何休息调用触发客户: https//flow.microsoft.com/fr-fr/blog/call-flow-restapi/

最有可能的是,当机器人被用户发送特定信息时,机器人将调用HTTP请求触发器。 请注意, BasicBot.cs文件具有OnTurnAsync方法。 在这里,您可以检查活动的.Text属性,如果是“发送电子邮件”,则调用Flow Trigger:

if (activity.Type == ActivityTypes.Message)
{
    if(active.Text == "send email")
    {
       await SendEmail();
    }
    else
    {
        //  other BasicBot.cs code
    }
...

如M.zK所示,您可以在C#中使用WebRequest来进行Flow Trigger调用。 您还可以使用HttpClient:

using (var request = new HttpRequestMessage(HttpMethod.Post, "https://prod-13.westus.logic.azure.com:443/workflows/etc"))
{
    var content = new { Properties = new { Property1 = "property 1 value", Property2 = "Property 2 value" } };
    var json = JsonConvert.SerializeObject(content);
    request.Content = new StringContent(json, Encoding.UTF8, "application/json");
    using (var client = new HttpClient())
    {
        await client.SendAsync(request).ConfigureAwait(false);
    }
}