botframework-sdk
botframework-sdk copied to clipboard
[Question] Image not shown using builder.CardImage and Promises?
- SDK Platform: Node.js
Issue Description
I want to show the image of a Foursquare venue, using a HeroCard and particularly of course builder.cardImage(session,link). The link is a promise, but i want the returning(resolving value) of it.All i get in the end in the emulator is an [object Promise]
Code Example
//Function returning the Promise
function getPhotoUrl(venue_id,venue_location,venue_query){
return new Promise((resolve,reject) =>
{
request({
url: 'https://api.foursquare.com/v2/venues/'+venue_id+'/photos',
json:true,
method: 'GET',
qs: {
client_id: 'my_id',
client_secret: 'my_secret',
near:venue_location,
query: venue_query,
v: '20171114',
limit: 1
}
}, function(err, res, body) {
var result="";
if (err) {
reject(err);
}
result=body.response.photos.items[0].prefix+"300x500"+body.response.photos.items[0].suffix;
resolve(result);
});
})
//
//The async function which awaits the promise returned from the above function
async function showImage(venue_id,venue_location,venue_query){
let res=await getPhotoUrl(venue_id,venue_location,venue_query).then(function(data){
return data;
});
console.log(res);
}
//invoking the function
new builder.HeroCard(session)
.title(places[0].name)
.subtitle(places[0].location.address+"-"+places[0].location.city+"-Τηλ:"+places[0].contact.phone)
.buttons([builder.CardAction.openUrl(session,"https://foursquare.com/v/"+places[0].name+"/"+places[0].id,"Μια καλύτερη ματιά?")])
.images([ **builder.CardImage.create(session,""+showImage(places[0].id,session.dialogData.topothesia,session.dialogData.foodtype)+"")])
**
Expected Behavior
To get the image shown in the emulator like the below paradigm.

Actual Results


@KwstasBol This is a general asynchronous programming issue, and not specific to BotBuilder SDK.
Short answer: you need to resolve your promise with the URL data before sending the card.
For more information see:
- https://www.google.com/search?q=node.js+async+await
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
- https://stackoverflow.com/search?q=%5Bnode.js%5D+promise