【问题标题】:Use display template to display random image使用显示模板显示随机图像
【发布时间】:2018-01-01 22:09:25
【问题描述】:

我正在尝试使用显示模板来显示随机图像。

现在下面的代码适用于显示一个图像,但是如果我添加一行具有不同图像 url 源的附加代码,它就不起作用了。

关于我可能做错的任何想法?非常感谢!

function supportsDisplay() {
  var hasDisplay =
    this.event.context &&
    this.event.context.System &&
    this.event.context.System.device &&
    this.event.context.System.device.supportedInterfaces &&
    this.event.context.System.device.supportedInterfaces.Display

  return hasDisplay;
}

function renderTemplate (content) {

   switch(content.templateToken) {
       case "factBodyTemplate":
        
           var response = {
             "version": "1.0",
             "response": {

               "directives": [
                 {
                   "type": "Display.RenderTemplate",
                   "template": {
                     "type": "BodyTemplate7",
                     "title": content.bodyTemplateTitle,
                     "image": {
                        "contentDescription": "",
                        "sources": [
                          {
                            "url": "https://www.example.com/image.jpg",
                            "url": "https://www.example.com/image2.jpg",
                            "url": "https://www.example.com/image3.jpg"
                          }
                        ]
                      },
                    
                   }
                 }
               ],
             "sessionAttributes": content.sessionAttributes
           }
           this.context.succeed(response);
           break;
   }

}

【问题讨论】:

    标签: javascript node.js alexa alexa-skills-kit amazon-echo-show


    【解决方案1】:

    编辑:

    好的,这可能行得通...在帖子的顶部,添加一个图像源数组,如下所示:

    const sourcesList = [
        "https://www.example.com/image.jpg",
        "https://www.example.com/image2.jpg",
        "https://www.example.com/image3.jpg"
    ]
    

    然后,它会在图片上询问您的网址:

    "image": {
        "contentDescription": "",
        "sources": [
        {
            "url": sourcesList[Math.floor(Math.random() * sourcesList.length)],
    

    应该可以。如果没有,请尝试在文件底部添加以下内容: (不保证这也有效)

    setInterval(function(){
        handlers['Get Fact']();
    ), 10000);//second value is the amount of time it takes to change image
    

    您是否在新图像中包含了文件类型?

    向 JSON 添加额外属性时,请确保在其后添加另一个逗号。即。

    "url": "anotherURL.jpg",
    "url": "anotheranotherURL.jpg"
    

    注意第一行末尾的逗号。

    【讨论】:

    • 很好的问题 - 我做到了,看起来像这样,但这会导致错误(“解析错误 - 意外令牌:URL”):imgur.com/a/DYSXF
    • 在新属性后加逗号。例如,请参阅编辑后的答案
    • 很好地解决了逗号问题,这对修复解析错误非常有帮助!代码现在看起来像这样,但是 Alexa 服务模拟器每次仍然只提取最后一个图像,而不是随机选择 3 个图像中的一个:imgur.com/a/euk0E 也许我应该在代码的其他地方设置随机化行为?我还为你添加了一个赞成票,但它说少于 20 名代表的用户没有公开投票:'(
    • 是的,StackOverflow 不希望人们编写能够为自己提供代表的机器人。我没有看到您的随机化行为在原始帖子中提供的代码中定义在哪里?如果可以的话,你最好把它全部发布出来。
    • 我已更新原始帖子以包含 Alexa 技能的整个 .js 文件。非常感谢您的帮助!
    猜你喜欢
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    • 1970-01-01
    • 2021-10-14
    • 2012-06-10
    • 1970-01-01
    相关资源
    最近更新 更多