I have a list of files that I'm retrieving from Drive:
const list = await drive.files.list({
q: `parents in '${process.env.DRIVE_DIR}' and name contains 'test_'`,
spaces: 'drive',
fields: 'files(id, mimeType, webViewLink)'
})
After getting the files I want to send embed messages with the image from Drive:
const embed = new MessageEmbed()
.setColor('#6adada')
.setTitle('Test')
.setImage(list.data.files[index].webViewLink)
message.channel.send({embeds: [embed]})
I checked all permission on Google Drive and they are correct, if I access the link as a guest I can see the image in the browser. Is there something that I'm missing? Thanks in advance.
I solved this issue using FS to download the file from drive, after that attaching it to the embed message using the attachment path in the setImage function. So after getting the list of files, I download it (in my case it will be always one file):
const { data } = await drive.files.get(
{
fileId: list.data.files[0].id,
alt: "media"
},
{ responseType: "stream" }
)
const fileName = fID + "." + mimeType.split("/")[1]
const file = fs.createWriteStream(fileName)
data.on("end", () => {
const embed = new MessageEmbed()
.setColor('#6adada')
.setTitle('Test Image')
.setImage('attachment://' + fileName)
message.channel.send({
embeds: [embed],
files: [fileName]
})
.then(() => {
file.end()
fs.unlinkSync(fileName)
})
})
.on("error", error => message.reply(" Error: " + error.message))
.pipe(file)
You need to send the webViewLink to discord, where you change the usp query parameter in the URL from drivesdk to sharing.
For reference, this is the discord incoming webhook API: https://discord.com/developers/docs/resources/webhook#execute-webhook
the google drive rest API: https://developers.google.com/drive/api/v3/reference/files#resource
Check out a simple implementation of it here:
| Example Implementation | Run in Fusebit |
|---|
const { Integration } = require('@fusebit-int/framework');
const superagent = require('superagent')
const integration = new Integration();
const router = integration.router;
const googleConnector = 'googleConnector';
const discordConnector = 'discordConnector';
router.post('/api/tenant/:tenantId/test', async (ctx) => {
try {
const googleClient = await integration.tenant.getSdkByTenant(ctx, googleConnector, ctx.params.tenantId);
const files = await googleClient
.drive({
version: 'v3',
})
.files.list({fields: 'files(id, mimeType, webViewLink)'});
const file = files.data.files[0]
file.webViewLink = file.webViewLink.replace('drivesdk', 'sharing')
await discordSendMessage(ctx, file.webViewLink)
ctx.body = {
message: `Message with embed sent to discord.`,
};
} catch (e) {console.log(e)}
});
async function discordSendMessage(ctx, content) {
const discordClient = await integration.tenant.getSdkByTenant(
ctx,
discordConnector,
ctx.params.tenantId
);
const webhook = discordClient?.fusebit?.credentials?.webhook;
if (!webhook) {
ctx.throw(404, `The installation does not have a default webhook channel.`);
}
try {
return await superagent.post(webhook.url).send({ content })} catch (e) {console.log(e)}
}
module.exports = integration;