在发布具有连续部署的漫游器后,图像不会显示

时间:2019-05-17 07:11:32

标签: github continuous-integration botframework azure-web-app-service wwwroot

我使用Bot Framework开发了一个机器人。当我在Bot Framework Emulator上本地运行它时,它会显示图像。但是在通过创建私有GitHub存储库将其发布在Azure上之后,没有显示任何图像。我正在使用Direct-Line API 3.0。可能是什么原因?

就像,这是我的项目的树状视图。 images文件夹位于Content Root中。

Project Tree View

我正在使用以下静态方法来检索图像路径。

public static string CreateImagePath(string filename)
    {
        return Path.Combine(TempData.HostingEnvironment?.ContentRootPath, "images", $"{filename}");
    }

然后将其转换为Uri,并将其分配给Adaptive Card的映像。这是代码段。

(AdaptiveCardFactory.CreateAdaptiveElement(card, "image") as AdaptiveImage).Url =
            new Uri(PathFactory.CreateImagePath("welcome_card_image.png"));

CreateAdaptiveElement定义为:

public static AdaptiveElement CreateAdaptiveElement(AdaptiveCard card, string adaptiveElementId)
    {
        return card.Body.Find(ae => ae.Id == adaptiveElementId);
    }

这是我在本地运行机器人时返回的Uri。它工作正常。

file:///C:/Users/Husai/source/repos/PanjaSahibBot/images/welcome_card_image.png

这是发布后返回的内容。

file:///D:/home/site/wwwroot/images/welcome_card_image.png

而且,它不起作用。

这是最新的命令日志。

    Command: "D:\home\site\deployments\tools\deploy.cmd"
Handling ASP.NET Core Web Application deployment.
  Restoring packages for D:\home\site\repository\GurdwaraBot.csproj...
  Generating MSBuild file D:\home\site\repository\obj\GurdwaraBot.csproj.nuget.g.props.
  Generating MSBuild file D:\home\site\repository\obj\GurdwaraBot.csproj.nuget.g.targets.
  Restore completed in 5.02 sec for D:\home\site\repository\GurdwaraBot.csproj.
Microsoft (R) Build Engine version 15.9.20+g88f5fadfbe for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 1.41 sec for D:\home\site\repository\GurdwaraBot.csproj.
  GurdwaraBot -> D:\home\site\repository\bin\Release\netcoreapp2.2\GurdwaraBot.dll
  GurdwaraBot -> D:\local\Temp\8d6da8df6d390f3\
Creating app_offline.htm
KuduSync.NET from: 'D:\local\Temp\8d6da8df6d390f3' to: 'D:\home\site\wwwroot'
Copying file: 'GurdwaraBot.deps.json'
Copying file: 'GurdwaraBot.dll'
Copying file: 'GurdwaraBot.pdb'
Copying file: 'GurdwaraBot.runtimeconfig.json'
Copying file: 'images\01d.png'
Copying file: 'images\01n.png'
Copying file: 'images\02d.png'
Copying file: 'images\02n.png'
Copying file: 'images\03d.png'
Copying file: 'images\03n.png'
Copying file: 'images\04d.png'
Copying file: 'images\04n.png'
Copying file: 'images\09d.png'
Copying file: 'images\09n.png'
Copying file: 'images\1.jpg'
Copying file: 'images\10.jpg'
Copying file: 'images\10d.png'
Copying file: 'images\10n.png'
Copying file: 'images\11d.png'
Copying file: 'images\11n.png'
Copying file: 'images\13d.png'
Copying file: 'images\13n.png'
Copying file: 'images\2.jpg'
Copying file: 'images\3.jpg'
Copying file: 'images\4.jpg'
Copying file: 'images\5.jpg'
Copying file: 'images\50d.png'
Copying file: 'images\50n.png'
Copying file: 'images\6.jpg'
Copying file: 'images\7.jpg'
Copying file: 'images\8.jpg'
Copying file: 'images\9.jpg'
Copying file: 'images\bill.png'
Copying file: 'images\feedback.png'
Copying file: 'images\Festivals.jpg'
Copying file: 'images\festivals_bandi_chhor_divas.jpg'
Copying file: 'images\festivals_gurpurab.jpeg'
Copying file: 'images\festivals_hola_mohalla.jpg'
Copying file: 'images\festivals_maghi.jpg'
Copying file: 'images\festivals_martyrdom_of_guru_arjan.jpg'
Copying file: 'images\festivals_parkash_utsav_dasveh_patshah.jpg'
Copying file: 'images\festivals_vaisakhi.jpg'
Copying file: 'images\menu_card_image.png'
Copying file: 'images\panja_thumbnail_1.png'
Copying file: 'images\panja_thumbnail_2.png'
Copying file: 'images\panja_thumbnail_3.png'
Copying file: 'images\pof_guest_hotel.jpg'
Copying file: 'images\question.png'
Omitting next output lines...
Finished successfully.

这是我所期望的。 Locally executed 而且,这就是我通过DirectLine得到的东西! Actual result

这是我的welcome_card.json的内容

    {
  "type": "AdaptiveCard",
  "body": [
    {
      "type": "Image",
      "id": "image",
      "horizontalAlignment": "Center",
      "style": "Person",
      "url": "",
      "size": "Medium"
    },
    {
      "type": "TextBlock",
      "horizontalAlignment": "Center",
      "size": "Medium",
      "weight": "Bolder",
      "color": "Accent",
      "text": "Welcome to Gurdwara Bot",
      "wrap": true
    },
    {
      "type": "TextBlock",
      "horizontalAlignment": "Left",
      "text": "Hi there! I'm the Sri Panja Sahib's support bot. What can I help you with today? Just so you know, you can enter **MENU** anytime you want to go back to the options below, or just go ahead type your query.",
      "wrap": true
    }
  ],
  "actions": [
    {
      "type": "Action.Submit",
      "title": "Get an Overview",
      "data": {
        "dataId": "overview"
      }
    },
    {
      "type": "Action.Submit",
      "title": "Ask a Question",
      "data": {
        "dataId": "question"
      }
    },
    {
      "type": "Action.Submit",
      "title": "Book a Room",
      "data": {
        "dataId": "room"
      }
    },
    {
      "type": "Action.Submit",
      "title": "Give a Feedback",
      "data": {
        "dataId": "feedback"
      }
    }
  ],
  "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
  "version": "1.0",
  "speak": "Hi there! I'm the Sri Panja Sahib's support bot. What can I help you with today? Just so you know, you can enter **MENU** anytime you want to go back to the options below, or just go ahead type your query."
}

我已经定义了AdaptiveCardFactory以使用卡进行操作。

public class AdaptiveCardFactory
{
    public static AdaptiveCard CreateAdaptiveCard(string path)
    {
        try
        {
            var adaptiveCardJson = File.ReadAllText(path);
            AdaptiveCardParseResult result = AdaptiveCard.FromJson(adaptiveCardJson);
             return result.Card;
        }
        catch (AdaptiveSerializationException)
        {
            throw;
        }
    }

    public static Attachment CreateAdaptiveCardAttachment(AdaptiveCard card)
    {
        return new Attachment
        {
            ContentType = "application/vnd.microsoft.card.adaptive",
            Content = card,
        };
    }

    public static AdaptiveElement CreateAdaptiveElement(AdaptiveCard card, string adaptiveElementId)
    {
        return card.Body.Find(ae => ae.Id == adaptiveElementId);
    }
}

1 个答案:

答案 0 :(得分:2)

TL; DR;

您必须为AdaptiveCard中显示的项目提供公共URL,而不是本地URL。它与模拟器一起在本地工作,因为...您在本地,并且模拟器可以访问您的本地文件,因此它可以理解具有本地位置的文件(C:\...D:\...文件)。

因此,请更改路径,以使其使用您的子域:您可以在此处执行此操作,例如,使用配置变量代替TempData.HostingEnvironment?.ContentRootPath

public static string CreateImagePath(string filename)
{
    return Path.Combine(TempData.HostingEnvironment?.ContentRootPath, "images", $"{filename}");
}

详细信息:

AdaptiveCard项目作为bot消息中的附件传递,内部的所有内容均由客户端(渠道)呈现,因此它只能呈现从外部“可见”的内容。

在这里查看我的演示:我使用了一张具有2张图像的ImageSet的卡:一个带有Web上的URL,另一个带有类似您的URL的构建。

本地:两个图像都可以。 enter image description here

已发布:仅显示第1张图像 Published

如果您查看右侧的检查器,很明显,您必须更改构建图像网址的方式。

您还可以通过右键单击图像所在的区域来查看已部署的网络聊天,您将找到一个带有D:\...路径的链接

代码修复

首先,将images文件夹移至projet的wwwroot文件夹内。然后,您可以执行以下操作:

public string CreateImagePath(string filename)
{
    if (_hostingEnvironment.IsDevelopment())
    {
        return Path.Combine(TempData.HostingEnvironment?.ContentRootPath, "wwwroot", "images", $"{filename}");
    }
    else
    {
        var targetUri = new Uri(new Uri($"https://{Environment.ExpandEnvironmentVariables("%WEBSITE_SITE_NAME%")}.azurewebsites.net"), Path.Combine("images", $"{filename}"));
        return targetUri.ToString();
    }
}