Skip to content

Instantly share code, notes, and snippets.

@marks
Last active January 25, 2023 12:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marks/6b58af98e7641cae52f8d572e6c00444 to your computer and use it in GitHub Desktop.
Save marks/6b58af98e7641cae52f8d572e6c00444 to your computer and use it in GitHub Desktop.
Airtable => Vestaboard integration
// Click the "gear" icon in the top right to view settings
let config = input.config({
title: 'Send field value to Vestaboard',
description: 'A full description goes here :) ',
items: [
input.config.table('selectedTable', {
label: 'Table to use',
}),
input.config.field('selectedField', {
label: 'Field inside the above table with value you want to send',
parentTable: 'selectedTable',
}),
input.config.text('vestaboardApiKey', {
label: 'Vestaboard API Key',
}),
input.config.text('vestaboardApiSecret', {
label: 'Vestaboard API Secret'
})
]
})
// output.text("Hi, I'm working on your request")
// Load values from the input config
const table = config.selectedTable
const field = config.selectedField
const vestaboardApiKey = config.vestaboardApiKey
const vestaboardApiSecret = config.vestaboardApiSecret
// This input will be hidden if run from the table's button
const record = await input.recordAsync('Select a record to send to your Vestaboard', table);
// console.debug(`Record w/ name of ${record.name} selected`)
const stringToSend = await record.getCellValueAsString(field)
// console.debug(`String to send (record's '${field.name}' field value): ${stringToSend}`)
// Get a list of Vestaboard Subscriptions
const vestaboardSubscriptions = await getVestaboardSubscriptions()
let vestaboardSubscriptionsToChooseFrom = vestaboardSubscriptions.subscriptions.map((x) => {
const createdAt = new Date(parseInt(x._created));
return {value: x._id, label: `${x._id}\n(created at ${createdAt.toDateString()})`}
})
// Ask the user which Vestaboard subscription to publish to
const vestaboardToPublishTo = await input.buttonsAsync(
'Which Vestaboard subscription would you like to send this to?',
vestaboardSubscriptionsToChooseFrom,
)
// Publish the message
// console.log(`About to publish the following to Vestaboard subscription ${vestaboardToPublishTo}:`)
// console.log(stringToSend)
await publishMessageToVestaboard(stringToSend)
output.text("All done!")
// Function to call the Vestaboard API
async function vestaboardApiCall(url, method, data = {}) {
const requestOptions = {
method,
headers: {
'X-Vestaboard-Api-Key': vestaboardApiKey,
'X-Vestaboard-Api-Secret': vestaboardApiSecret
}
}
if(method == 'POST'){
requestOptions.body = JSON.stringify(data)
}
const response = await fetch(url, requestOptions)
return response.json()
}
// Function to publish message to Vestaboard
async function publishMessageToVestaboard(text){
// console.debug("Calling POST /subscriptions/SUBSCRIPTION_ID/message")
await vestaboardApiCall(
`https://platform.vestaboard.com/subscriptions/${vestaboardToPublishTo}/message`,
'POST',
{text}
)
.then(async data => {
// console.debug("Received: ", data)
})
}
// Function to get a list of Vestaboard subscriptions the API key has access to
async function getVestaboardSubscriptions(){
// console.debug("Calling GET /subscriptions")
return await vestaboardApiCall('https://platform.vestaboard.com/subscriptions', 'GET')
.then(async data => {
// console.debug("Received: ", data)
return data
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment