聊天机器人在Web仿真器中无法工作,但在本地Bot Framework仿真器中能够正常工作

9
我开发了一个与SharePoint On Premise集成的聊天机器人。当我在仿真器中调试聊天机器人时,它能够正常工作。但是,当我在Azure中使用Web仿真器和通过DirectLine在公司网站上托管网站进行调试时,它就不能正常工作。
有人知道如何解决吗?
这是我的屏幕截图。左边是Web仿真器,右边是本地Bot Framework仿真器。

enter image description here

使用源代码更新(2019年12月9日)

XmlNamespaceManager xmlnspm = new XmlNamespaceManager(new NameTable());

Uri sharepointUrl = new Uri("https://mvponduty.sharepoint.com/sites/sg/daw/");

xmlnspm.AddNamespace("atom", "http://www.w3.org/2005/Atom");
xmlnspm.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
xmlnspm.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");

NetworkCredential cred = new System.Net.NetworkCredential("engsooncheah@mvponduty.onmicrosoft.com", "Pa$$w0rd", "mvponduty.onmicrosoft.com");

HttpWebRequest listRequest = (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString() + "_api/lists/getByTitle('" + "data@work" + "')/items?$filter=Keywords%20eq%20%27bloomberg%27");
listRequest.Method = "GET";
listRequest.Accept = "application/atom+xml";
listRequest.ContentType = "application/atom+xml;type=entry";

listRequest.Credentials = cred;
//LINE 136 start from below
HttpWebResponse listResponse = (HttpWebResponse)listRequest.GetResponse();
StreamReader listReader = new StreamReader(listResponse.GetResponseStream());
XmlDocument listXml = new XmlDocument();

listXml.LoadXml(listReader.ReadToEnd());

if (listResponse.StatusCode == HttpStatusCode.OK)
{
    Console.WriteLine("Connected");
    await turnContext.SendActivityAsync("Connected");
}

// Get and display all the document titles.
XmlElement root = listXml.DocumentElement;
XmlNodeList elemList = root.GetElementsByTagName("content");
XmlNodeList elemList_title = root.GetElementsByTagName("d:Title");
XmlNodeList elemList_desc = root.GetElementsByTagName("d:Description");

//for LINK
XmlNodeList elemList_Id = root.GetElementsByTagName("d:Id");
XmlNodeList elemList_Source = root.GetElementsByTagName("d:Sources");
XmlNodeList elemList_ContentTypeId = root.GetElementsByTagName("d:ContentTypeId");

var attachments = new List<Attachment>();
for (int i = 0; i < elemList.Count; i++)
{

    string title = elemList_title[i].InnerText;
    string desc = elemList_desc[i].InnerText;

    string baseurllink = "https://mvponduty.sharepoint.com/sites/sg/daw/Lists/data/DispForm.aspx?ID=";
    string LINK = baseurllink + elemList_Id[i].InnerText + "&Source=" + elemList_Source[i].InnerText + "&ContentTypeId=" + elemList_ContentTypeId[i].InnerText;

    //// Hero Card
    var heroCard = new HeroCard(
        title: title.ToString(),
        text: desc.ToString(),

        buttons: new CardAction[]
        {
            new CardAction(ActionTypes.OpenUrl,"LINK",value:LINK)
        }
    ).ToAttachment();
    attachments.Add(heroCard);
}
var reply = MessageFactory.Carousel(attachments);
await turnContext.SendActivityAsync(reply);

2019年12月17日更新

我尝试使用嵌入和直接连接,但错误仍然相同。

机器人未托管在SharePoint上。

2020年1月6日更新 在Azure Bot Services中也无法正常工作。


通过Web模拟器,您是指Web聊天测试功能吗?还是一个完整的WebChat捆绑包/实现。这是托管在SharePoint页面/WebPart/SPFX中吗? - Dana V
@DanaV,是的。Web仿真器在Azure Web仿真器中,并且在另一个网页中使用Webchat中的直接线路。聊天机器人没有托管在SharePoint页面上。 - Eng Soon Cheah
3个回答

4
根据您的描述,您可以从本地获取数据。这意味着您的代码和逻辑都没有问题。
我注意到您的SharePoint URL是:https://mvponduty.sharepoint.com/sites/sg/daw/,我试图访问它,并尝试访问您的整个请求URL:https://mvponduty.sharepoint.com/sites/sg/daw/_api/lists/getByTitle('data@work')/items?$filter=Keywords eq 'bloomberg',两者的响应都是404.
您说这是一个本地网站,所以请检查一下是否可以从公共网络访问该网站。
我假设当您在本地测试代码时,您可以访问此网站,因为您在内部网络中,能够访问本地网站。但是,当您将代码发布到Azure时,它不再是在您的内部工作环境中:它处于公共网络中,因此无法访问您的本地sharePoint站点,这导致了此错误。
我们知道,机器人代码托管在Azure应用服务上,如果此错误是由上述原因引起的,也许Azure App Service Hybrid Connections功能将有助于解决此问题。

从内部SharePoint检索数据是正确的。我应该在内部SharePoint上发布ChatBot吗? - Eng Soon Cheah
嗨@EngSoonCheah,这取决于谁将使用此机器人。如果仅供内部网络用户使用,理论上,您可以在您的SharePoint网站中发布它。如果外部用户需要访问它,则应将其发布到Azure Bot服务,并使用Azure应用程序服务(Azure Bot服务基于Azure应用程序服务)的混合连接功能将您的机器人应用程序服务连接到本地站点。我认为这份文档会很有帮助:https://azuregems.io/azure-hybrid-connections/ - Stanley Gong
仍在尝试使用Azure混合连接。 - Eng Soon Cheah
好的。感谢您的帮助。圣诞快乐,新年快乐。 - Eng Soon Cheah
嗨@EngSoonCheah,圣诞快乐!希望你在新的一年里一切顺利! - Stanley Gong
显示剩余3条评论

2

ChatBot似乎运行良好?它可以发送和接收消息。您有一些代码在本地运行与托管时的行为不同。有Xml,它是文件还是生成的?您需要检查它是否遵循与本地运行相同的逻辑并使用相同的数据。也许如果您粘贴一些(非机密)代码,在崩溃时,我们可以有更多的想法来帮助。


错误始于 DispatchBot.cs 的第136行。它是哪一行?需要查看 ProcessRSSAsync 方法的第136行。 - waleed
好的。看起来在本地,您可以正常访问SharePoint的RSS,但是在托管时它没有返回相同的XML,这意味着很可能返回了一个HTML错误页面。我搜索了您的错误并找到了这个链接:https://forums.asp.net/t/1116247.aspx?System+Xml+XmlException+The+start+tag+does+not+match+the+end+tag+HELP它建议您可能需要使用代理,请尝试该链接中的代码示例。 - waleed
错误显示:System.PlatformNotSupportedException:此平台不支持该操作。 - Eng Soon Cheah
错误发生在listXml.LoadXml(listReader.ReadToEnd());,对吗?虽然您可以搜索访问SharePoint RSS时的错误,但如果您能看到响应是什么(它很可能会告诉具体的错误),那么这更直接。您有记录器或者正在使用应用程序洞察吗?我建议JsonConvert.SerializeObject(listResponse)并将其记录或抛出,甚至将字符串作为机器人中的消息发送,以便您可以查看其内容。一旦您拥有了序列化的字符串,只需将其保存为HTML文件error.html并打开即可。 - waleed
本地主机可以从XML中读取所有项目,但问题出在我使用Directline的HTML5页面上。 - Eng Soon Cheah
显示剩余2条评论

2
当你发布你的机器人时,会有以下选项:

Image

选择编辑应用服务设置。仅添加以下细节,不要添加其他内容:
MicrosoftAppId : <xxxxx>
MicrosoftAppPassword : <xxxxx>

点击“应用”和“确定”。
确保从appsettings.json中删除“Microsoft App Id”和“Microsoft App Password”,以便在机器人仿真器中也能正常工作。
现在发布机器人。它将在两个地方都可以使用。
希望这对您有所帮助。

我只看到了预览和配置,因为我是从Azure门户下载的ChatBot源代码。 - Eng Soon Cheah

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接