-
Notifications
You must be signed in to change notification settings - Fork 72
Adding previews to links #53
Comments
Your code appends link preview to the bottom of message list. I had to look it up, but something like this should do the job: https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentElement |
Ah OK, that seems like a good call. Any idea how I'd get a reference to that message's element? I've tried a couple of things, but to no avail. I think there also might be an issue if there's a number of links. Is there any way I can put an artificial delay in place while the preview is being fetched? |
|
Ah, never mind. I've solved this by using promises and getting the preview first, then rendering the response using the template: addLinkPreview: function(message) {
return new Promise(function(resolve, reject) {
if (message.files !== undefined && message.files.length > 0) {
var url;
for (var i = 0; i < message.files.length; i++) {
url = message.files[i].url
that.getLinkPreview(url, function(err, response) {
if (err) { throw err; }
message.link_preview = {
url: url,
data: response
}
resolve();
});
}
} else {
resolve();
}
})
} that.on('message', function(message) {
that.addLinkPreview(message).then(function() {
that.renderMessage(message);
});
});
|
Pretty cool! Is there something here that could be contributed back to the project? |
Quite possibly, I (mis)used the files field for this, but I'll see what I can tidy up and push up 👍 |
I'm trying to adapt the starter kit to show previews of links (a la Facebook messenger, WhatsApp etc). I think the overall concept is sound, but as there's a delay getting the metadata from webpages, the next message will often show before the preview. Is there any way to stop the bot from processing while the metadata is fetched?
I've added an Express endpoint to my bot to get previews:
Then I have a function that calls the endpoint in client.js:
And finally render the actual output when the
message
event is called:I imagine I'm doing something wrong at the last point, but not sure where to start. Any help is appreciated!
The text was updated successfully, but these errors were encountered: