将Azure Chat Bot连接到服务器较少功能的问题

时间:2020-05-04 16:30:31

标签: c# chatbot azure-web-app-service serverless azure-bot-service

我正在尝试按照本指南在Microsoft Azure中创建无服务器Python Chatbot API: https://towardsdatascience.com/creating-a-serverless-python-chatbot-api-in-microsoft-azure-from-scratch-in-9-easy-steps-2f1913fc9581 但是,在第7阶段(将BOT连接到无服务器功能)上,我将原始EchoBot.cs代码替换为以下代码(如指南中所述),聊天机器人停止工作。我不确定下面的代码缺少什么,有人可以启发我吗?谢谢

 using System.Collections.Generic;
 using System.Threading;
 using System.Threading.Tasks;
 using Microsoft.Bot.Builder;
 using Microsoft.Bot.Schema;
 using System.Net;
 using System;
 using System.Net.Http;
 using System.Text;  // for class Encoding
 using System.IO;  
 using Newtonsoft.Json;
 using Newtonsoft.Json.Serialization;

namespace Microsoft.BotBuilderSamples.Bots
{
public class EchoBot : ActivityHandler
{
    public class FlaskRequestModel
    {
        [JsonProperty("text")]
        public string Text {get; set;}            

    }

    protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
    {           
        var replyText = $"{turnContext.Activity.Text}";
        //if (!replyText.ToLower().Contains("Hey Bot")){  # Optional bit of code that only sends the sends the message to the back end if it contains a particular keyword
        //    return;
        //}
        var replyTextModel = new FlaskRequestModel()
        {
            Text = replyText 
        };

        var jsonObject = JsonConvert.SerializeObject(replyTextModel);

        var request = new HttpRequestMessage()
        {

            Content = new StringContent(jsonObject, Encoding.UTF8, "application/json"),
            Method = HttpMethod.Post,
            RequestUri = new Uri("https://linkedinexample.azurewebsites.net/api/Serveless_Function"),   //  <- Replace the URL with the the URL for your function app
        };            

        var httpClient = new HttpClient();
        // httpClient.DefaultRequestHeaders.Add("API-Key","your API-key");  <- required if your HTTP trigger authorization was set to something other than Anonymous
        var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead);      

        if (response.IsSuccessStatusCode)
        {
            var responseString = await response.Content.ReadAsStringAsync();
            await turnContext.SendActivityAsync(MessageFactory.Text(responseString, responseString), cancellationToken);
        }
        else
        {
            await turnContext.SendActivityAsync(MessageFactory.Text("failure", "failure"), cancellationToken);
            var responseString = await response.Content.ReadAsStringAsync();
            await turnContext.SendActivityAsync(MessageFactory.Text(responseString, responseString), cancellationToken);   
        }
    }
    }
    }

1 个答案:

答案 0 :(得分:2)

您可以简单地使用 for 循环遍历嵌套在列表 A 中的每个列表。

A = [
    ['I', 'love', 'apple.'],
    ['Today', 'is', 'Sunday.'],
    ['How', 'are', 'you'],
    ['What', 'are', 'you', 'doing']
]

for row in A:
    print(' '.join(row))