2017-03-09 12 views
0

私はAzure Botプログラミング(C#)を初めて使用しています。このトピックに関する良い記事は見つかりません。Azure BotからWebAPIを呼び出す

ボットを使用してメーリングリストにユーザーが「購読する」ことを許可します。フォームダイアログとフォームフローを構築します。ユーザーに電子メールアドレスを要求します。

私は外部WebAPI(json)に投稿し、応答を得て応答を処理する必要があります。

ボットからWebAPIを呼び出す方法についていくつかの指摘をいただけますか?

public async Task Subscribe(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result) 
    { 
     context.Call(SubscribeForm.BuildFormDialog(FormOptions.PromptInStart), SubscribeFormComplete); 
    } 

    private async Task SubscribeFormComplete(IDialogContext context, IAwaitable<SubscribeForm> result) 
    { 
     try 
     { 
      var form = await result; 
      if (form != null) 
      { 
       await context.PostAsync("Thanks for subscribing! You can always remove yourself by typing unsubscribe."); 
      } 
      else 
      { 
       await context.PostAsync("Form returned empty response!"); 
      } 
     } 
     catch (OperationCanceledException) 
     { 
      await context.PostAsync("I am sorry you decided not to subscribe! If you change your mind just type 'subscribe' again."); 
     } 

     context.Wait(this.MessageReceived); 
    } 


     [Serializable] 
public class SubscribeForm 
{ 
    [Prompt("What is your email address?")] 
    [Required()] 
    [DataType(DataType.EmailAddress)] 
    public string EmailAddress { get; set; } 

    public static IForm<SubscribeForm> BuildForm() 
    { 
     // Builds an IForm<T> based on BasicForm 
     return new FormBuilder<SubscribeForm>() 
      .Message("We often send out updates on new features. We don't spam. You can type 'quit' to cancel this.") 
      .Build(); 
    } 

    public static IFormDialog<SubscribeForm> BuildFormDialog(FormOptions options = FormOptions.PromptInStart) 
    { 
     // Generated a new FormDialog<T> based on IForm<BasicForm> 
     return FormDialog.FromForm(BuildForm, options); 
    } 
} 
+0

なぜそれが違うのでしょうか?あなたは通常どおりリクエストを行うことができます。 – stuartd

+0

ボットとフォームフローには、「スタック」とそれらに親子のニュアンスがあります。 WebAPI呼び出しのコードを配置する場所に関する提案を探してください。例えば、formcompletedメソッドや他の場所で、そして非同期とコールバックをどう扱うか。 – user3637002

答えて

2

これは私がMicrosoftの認知サービスLUIS呼び出す方法の例ですが、私は静的クラスのメソッドを持っており、この方法は非同期です。

方法:

public static class CognitiveHelper 
    { 
     private const string UrlLuis = "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/a267a797-9584-41a5-83f3-xxxxxxxxx?subscription-key=xxxxxxxxxxxxxxxxxxxxxxx&q="; 
    public static async Task<LuisObjects> GetLuisAnswer(string textToEvaluate) 
      { 
       if (string.IsNullOrWhiteSpace(textToEvaluate)) throw new ArgumentException("Null argument"); 

       textToEvaluate= HttpUtility.UrlEncode(textToEvaluate); 
       var urlLuisWithRequest = UrlLuis + textToEvaluate; 

       var client = new HttpClient(); 
       var body = new { }; 

       var serializedBody = new JavaScriptSerializer().Serialize(body); 
       byte[] bodyByte = Encoding.UTF8.GetBytes(serializedBody); 

       using (var content = new ByteArrayContent(bodyByte)) 
       { 
        content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); 
        var response= await client.GetAsync(urlLuisWithRequest); 
        var responseContent= await respuesta.Content.ReadAsStringAsync(); 

        var javaScriptSerializer = new JavaScriptSerializer(); 
        var resultTextAnalysis= javaScriptSerializer.Deserialize<LuisResult>(responseContent); 
        return new LuisObjects() 
        { 
         Entities= resultTextAnalysis.entities.ToList(), 
         TopScoringIntent = resultTextAnalysis.topScoringIntent 
        }; 
       } 
      } 
} 

コール:ボットから行うとき

var luisResponse = await CognitiveHelper.GetLuisAnswer(activity.Text); 
関連する問題