Skip to content

Instantly share code, notes, and snippets.

@thiagosouza
Created January 20, 2021 00:07
Show Gist options
  • Save thiagosouza/8d004273d1c335e638f50947e8cb1efc to your computer and use it in GitHub Desktop.
Save thiagosouza/8d004273d1c335e638f50947e8cb1efc to your computer and use it in GitHub Desktop.
[Airtable API] Airtable API call #airtable
/**
* This code bellow is an Airtable implementation and should be placed there with the proper params for tables and fields
*/
let table = base.getTable("users");
let query = await table.selectRecordsAsync();
let recordsToUpdate = query.records.filter(record => {
let notified = (record.getCellValue("notified") !== null && record.getCellValue("notified").name == "true") ? true : false;
return !notified
})
output.table(recordsToUpdate);
let field = table.getField("notified");
// let notificationsToBeSend = []
let shouldReplace = await input.buttonsAsync(`There are ${recordsToUpdate.length} leads to be sent to admin. Send now?`, [
{ label: 'Save', variant: 'primary' },
{ label: 'Cancel' },
]);
if (shouldReplace === 'Save') {
for (let record of recordsToUpdate) {
let email = record.getCellValueAsString("Email")
let fullName = record.getCellValueAsString("Nome")
let metadata = record.getCellValueAsString("Metadata")
//Params based on https://sendgrid.api-docs.io/v3.0/mail-send
let params = {
"type": "EmailSendGridWithText",
"to": email,
"subject": "email x",
"templateId": "d-xxxxxx0a6fe24fc2b683a58640xxxxxx",
"dynamicTemplateData": {
"name": "Thiago",
"email": email,
"created": record.getCellValueAsString("Criação"),
"json": metadata
}
};
// let paramsForSendingHTML = {
// "type": "EmailSendGridWithText",
// "to": email,
// "html": "<b>bold</b> and not bold",
// "subject": "subject"
// };
// let paramsForSendingText = {
// "type": "EmailSendGridWithText",
// "to": email,
// "text": "pure text",
// "subject": "subject"
// };
let response = await sendEmail(params)
await table.updateRecordAsync(record, { [field.id]: { name: `${response.status}` } });
output.text(`updating record ${record.id} to ${response.status}`)
}
}
output.text("Finishing!")
async function sendEmail(params) {
let response = await fetch('https://api.site.com/', {
method: 'POST',
body: JSON.stringify(params),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer TOKEN'
},
});
return response.json();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment