auths
objectreturn
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps) => {
}
// Exit early if this message doesn't have any attachments
if (!("event" in $event.body) || !('attachments' in $event.body.event)) {
$end("No attachments in this Slack message — not an Airtable update.")
}
// ... or if the attachments don't actually contain Airtable data.
// This filters out any other messages from getting processed through
// the rest of the pipeline.
if (!($event.body.event.attachments[0].title_link.match(/https:\/\/airtable\.com.*/))) {
$end("No Airtable title_link in this Slack message — not an Airtable update.")
}
auths
objectreturn
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps) => {
}
// You'll need to add your Airtable API key and baseId to the
if (process.env.AIRTABLE_API_KEY === undefined) {
$end("Please create an environment variable named AIRTABLE_API_KEY under your Pipedream settings")
}
if (process.env.AIRTABLE_BASE_ID === undefined) {
$end("Please create an environment variable named AIRTABLE_BASE_ID under your Pipedream settings")
}
const Airtable = require('airtable')
const base = new Airtable({apiKey: process.env.AIRTABLE_API_KEY}).base(process.env.AIRTABLE_BASE_ID)
// Used for extracting the baseId and recordId from the Airtable URL
// included in each record update sent to Slack
const titleRegex = /https:\/\/airtable\.com\/(?<tableId>.*)\/(?<recordId>.*)/
// Airtable sends a batch of updates in a single message, but includes
// updates to individual records in Slack message attachments.
// For each of those attachments (record updates), we construct a JavaScript
// object that looks closer to what you'd expect to receive from a webhook directly.
$event.updates = []
for (let attachment of $event.body.event.attachments) {
let { tableId, recordId } = attachment.title_link.match(titleRegex).groups
console.log(`tableId: ${tableId}, recordId: ${recordId}`)
let record = await base(tableId).find(recordId)
let createdTime = record._rawJson.createdTime
let baseId = process.env.AIRTABLE_BASE_ID
let updates = attachment.fields.map(slackMessageField => {
return {
field: slackMessageField.title,
newValue: slackMessageField.value,
}
})
$event.updates.push({
baseId, tableId, recordId, createdTime, updates
})
}