使用显示模板显示随机图像

问题描述 投票:2回答:1

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

现在,下面的代码适用于显示一个图像,但如果我添加另一行代码与不同的图像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个回答
2
投票

编辑:

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

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添加额外属性时,请确保在其后添加另一个逗号。 I.E.

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

注意第一行末尾的逗号。

© www.soinside.com 2019 - 2024. All rights reserved.