在Azure上测试Echo聊天机器人时发生错误

时间:2020-04-12 01:41:19

标签: c# azure chatbot azure-bot-service

我正在关注 https://towardsdatascience.com/creating-a-serverless-python-chatbot-api-in-microsoft-azure-from-scratch-in-9-easy-steps-2f1913fc9581 创建python chatbot时,我的源代码如下,但是当我尝试运行它时,出现错误。根据存储库,错误是适配器的on_error处理程序接收到由bot的turn逻辑引发的任何异常。如果抛出异常,处理程序将删除当前对话的对话状态,以防止漫游器陷入由于状态错误而导致的错误循环。有专家可以帮助您吗?

       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("yudao.azurewebsites.net"),   //  <- 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 :(得分:1)

尝试使用正则表达式:

A2 = "12 15|||Pform|||their|||REQUIRED|||-NONE-|||0"
intval=[val for val in A2.split('|||') if bool(re.search(pattern='\d+', string=val))]

输出:

['12 15', '0']