Use display template to display random image - javascript

I am attempting to use the display templates to show a random image.
Right now the code below works for displaying one image, but if I add an additional line of code with a different image url source, it does not work.
Any ideas on what I could be doing wrong? Thanks so much!
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;
}
}

EDIT:
Okay, this might work... At the top of the post, add an array of your image sources, like so:
const sourcesList = [
"https://www.example.com/image.jpg",
"https://www.example.com/image2.jpg",
"https://www.example.com/image3.jpg"
]
Then, where it asks you for a url over at images:
"image": {
"contentDescription": "",
"sources": [
{
"url": sourcesList[Math.floor(Math.random() * sourcesList.length)],
That should work. If it doesn't, try adding this at the bottom of your file:
(no guarantee that this will work either)
setInterval(function(){
handlers['Get Fact']();
), 10000);//second value is the amount of time it takes to change image
Did you include a file type with your new image?
When you add an extra attribute to the JSON, make sure to add another comma after it. I.E.
"url": "anotherURL.jpg",
"url": "anotheranotherURL.jpg"
Notice the comma at the end of line one.

Related

Unsplash API triggering download

I'm following triggering guidelines in Unsplash documentation. So the endpoint is:
GET /photos/:id/download
This is the photo's example response:
{
"id": "LBI7cgq3pbM",
"width": 5245,
"height": 3497,
"color": "#60544D",
"urls": { ... },
"user": { ... },
"links": {
"self": "https://api.unsplash.com/photos/LBI7cgq3pbM",
"html": "https://unsplash.com/photos/LBI7cgq3pbM",
"download": "https://unsplash.com/photos/LBI7cgq3pbM/download", // don't use this property
"download_location": "https://api.unsplash.com/photos/LBI7cgq3pbM/download?ixid=MnwxMTc4ODl8MHwxfHNlYXJjaHwxfHxwdXBweXxlbnwwfHx8fDE2MTc3NTA2MTM" // use this one ;)
}
}
"Be sure to include any query parameters included in the URL (like the ixid)."
So my question is what is value od ixid=MnwxMTc4ODl8MHwxfHNlYXJjaHwxfHxwdXBweXxlbnwwfHx8fDE2MTc3NTA2MTM and how to get it?
Try the following
const ixid = new URLSearchParams(
new URL("https://api.unsplash.com/photos/LBI7cgq3pbM/download?ixid=MnwxMTc4ODl8MHwxfHNlYXJjaHwxfHxwdXBweXxlbnwwfHx8fDE2MTc3NTA2MTM").search
).get("ixid");
console.log(ixid);

Adaptive Cards Input Values in Submit

Hi I'm using the Adaptive card SDK in a web page, using a Sample Card like this:
var card = {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "TextBlock",
"text": "Present a form and submit it back to the originator"
},
{
"type": "Input.Text",
"id": "firstName",
"placeholder": "What is your first name?"
},
{
"type": "Input.Text",
"id": "lastName",
"placeholder": "What is your last name?"
}
],
"actions": [
{
"type": "Action.Submit",
"title": "Action.Submit"
}
]};
and rendering using the usual rubric. I'd like to get the inputs back with the submit so I tried this
// Set the adaptive card's event handlers. onExecuteAction is invoked
// whenever an action is clicked in the card
adaptiveCard.onExecuteAction = function (action) { console.log(action.toJSON()) }
which just gives me:
Object title: "Action.Submit" type: "Action.Submit" __proto__: Object
How do I get the values of the input fields on the submit action?
TIA for any comments, advice and answers
You can use the data property of the action object just like this:
adaptiveCard.onExecuteAction = function (action) {
alert(`Hello ${action.data.firstName} ${action.data.lastName}`);
}
Here's a full jsfiddle.
There are many other interesting properties on the action object, but I haven't found good documentation.
However, the source code of the adaptive card visualizer contains some usage examples.

Elasticsearch: can I avoid enabling fielddata on text fields?

I'm trying to get the latest records, grouped by the field groupId, which is a String like "group_a".
I followed the accepted answer of this question, but I've got the following error message:
Fielddata is disabled on text fields by default. Set fielddata=true on [your_field_name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory.
In the Elasticsearch docs is written:
Before you enable fielddata, consider why you are using a text field for aggregations, sorting, or in a script. It usually doesn’t make sense to do so.
I'm using a text field, because groupId is a String. Does it make sense to set fielddata: true if I want to group by it?
Or are there alternatives?
Using "field": "groupId.keyword" (suggested here) didn't work for me.
Thanks in advance!
The suggest answer with .keyword is the correct one.
{
"aggs": {
"group": {
"terms": {
"field": "groupId.raw"
},
"aggs": {
"group_docs": {
"top_hits": {
"size": 1,
"sort": [
{
"timestamp (or wathever you want to sort)": {
"order": "desc"
}
}
]
}
}
}
}
}
}
with a mapping like that:
"groupId": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
}

How to escape commas in JSON for read by highchart

I have the following JSON, I want add commas in between the numbers, but when ever I do that the JSON fails. My workign version of the code can be seen in the fiddle link below
FIDDLE
I want to change this 11488897 to 11,488,897
Is this possible to do? How can this be done?
thanks for your help
[{
"name": "",
"data": ["Totals","Total1 ","Total 2","total 3" ]
}, {
"name": "Amount1",
"data": [48353330,38079817,37130929,1957317]
}, {
"name": "Amount2",
"data": [11488897,8902674,8814629,497369]
}]
If you want to preserve commas, you just need to use strings:
"data": ["48,353,330","38,079,817","37,130,929","1,957,317"]
Whether that's a good idea or not is another story. Typically you'd want your JSON returned by the server to include raw (i.e., integer) data, and then just format it however you want when you're actually using it. That way the same RPC endpoint can be used to fetch data for use in a chart or for any other purpose that might arise later on.
try this:
var data = [{
"name": "",
"data": ["Totals","Total1 ","Total 2","total 3" ]
}, {
"name": "Amount1",
"data": [48353330,38079817,37130929,1957317]
}, {
"name": "Amount2",
"data": [11488897,8902674,8814629,497369]
}];
data.forEach(function(obj) {
obj.data = obj.data.map(function(item) {
if (typeof item == 'number') {
return item.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} else {
return item;
}
});
});
alert(JSON.stringify(data, true, 4));
I don't know if it's cross-browser but if you do this
var number = 11488897;
number = number.toLocaleString('en');
You'll get the number (string) with commas on decimals

How to get a list of document's fields from couchDB using node/cradle?

First of all, i want to let you know that i am a novice with node and couchDB and i have this project where i need to add some functionality to the existing application.
So, i have javascript/node/express web application and i want to get a specific document from a remote couchDB, using cradle, and then get a list of its fields and their values. Later on i would need to display those fields/values in some html.
I don't know which fields the document has because they are dynamically added/removed by a third party.
I was able to get the document i wanted, but i don't know how to iterate through its fields. What would be the best way to do that?
Here is a simpificated sample of the document:
{
"_id": "1.1.5",
"_rev": "5-56ebac233e7f56a14a4534c6902727f7",
"1.1.5.39": {
"Project": {
"Project1": {
"files": "...",
"status": "NEW",
"id": 2
},
"Project2": {
"files": "...",
"status": "ASSIGNED",
"id": 3
}
}
}
"1.1.5.23": {
"Project": {
"Project3": {
"files": "...",
"status": "NEW",
"id": 4
},
"Project4": {
"files": "...",
"status": "NEW",
"id": 5
}
}
}
}
I would need to get the fields '1.1.5.39' and '1.1.5.23', and also the 'status' values. These fields represent some versions of a software. The problem is also fields' format: numbers and dots, so i can't just use 'Object.attribute' notation...
First part is getting the document via cradle:
// get the Connection
var connection = new(cradle.Connection)('http://living-room.couch', 5984, {
cache: true,
raw: false
});
// get the DB
var db = connection.db('yourDB');
// get the document
var docID = "1.1.5";
db.get('docID', function (err, doc) {
displayDoc(doc);
});
function displayDoc(doc) {
// Do your display handling here
}
The second part what you need is displaying arbitrary objects. Here I refer you to my answer to this question.
Good Luck.

Categories

Resources