2017-12-26 21 views
0

受信したメッセージをWebhookエンドポイントに送信するMicrosoft Bot Builderを使用してボットを作成します。私は周りを探索することから見つけたいくつかの例を組み合わせようとしましたが、役に立たなかった。私はテキストの値を持つJSONペイロードとしてウェブフックへのユーザ入力テキストを送信するためにいくつかの助けを必要とし、ここで私は今持っているコードです:Microsoft Bot Builder activity.text JSONペイロードとしてRESTを使用したWebhookへのPOST

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

をベースにしていた場合I 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!" 
} 

私はあなたの助けに感謝します!

+0

要求を作成していますが、要求を最後に作成していません。コードに '(HttpWebResponse)httpWebRequest.GetResponse();'がありません。下の答えを見てください。 –

答えて

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); 
    } 
関連する問題