MGL Group Updater using Slack
@MakeGiveLive
code:
data:privatelast updated:11 months ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 200,000+ developers using the Pipedream platform
steps.
trigger
Slack - New Message In Conversation(s)
last updated:last year
steps.
getBaseID
auth
to use OAuth tokens and API keys in code via theauths object
(auths.airtable)
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across invocations with$checkpoint.
async (event, steps, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
}
27
// Get Group Name from Slack Message
var msg = steps.trigger.event.text;
var n = msg.indexOf("* in ");
var group = msg.substr(n+5, msg.length - n+5);
//console.log('https://api.airtable.com/v0/appsEhmLOfUP2Svk5/MGL%20Groups?filterByFormula=SEARCH("'+group+'",{Group})');


const config = {
  url: `https://api.airtable.com/v0/appsEhmLOfUP2Svk5/MGL%20Groups?filterByFormula=SEARCH("${group}",{Group})`,
  
  headers: {
    Authorization: `Bearer ${auths.airtable.api_key}`,
  },
}

//steps.getGroup.$return_value.records[0].fields.baseid
var x = await require("@pipedreamhq/platform").axios(this, config);
var baseID = x.records[0].fields.baseid;
return baseID;
//return await require("@pipedreamhq/platform").axios(this, config);
//var results =  await require("@pipedreamhq/platform").axios(this, config)
//console.log(results);
//var baseID = results.records[0].fields.baseID;
//return baseID
//return steps.getGroup.$return_value.records[0].fields.baseid
steps.
getUpdateData
auth
to use OAuth tokens and API keys in code via theauths object
(auths.airtable)
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across invocations with$checkpoint.
async (event, steps, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
}
38
// You'll need to add your Airtable API key and baseId to the 

const Airtable = require('airtable')
const base = new Airtable({apiKey: auths.airtable.api_key}).base(steps.getBaseID.$return_value)

// 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 steps.trigger.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 = steps.getBaseID.$return_value
  let updates = attachment.fields.map(slackMessageField => {
    return { 
      field: slackMessageField.title, 
      newValue: slackMessageField.value,
    }
  })


  event.updates.push({
    record, baseId, tableId //, recordId, createdTime, updates
  })
}


return event.updates;

//return record;
steps.
updateArray
auth
to use OAuth tokens and API keys in code via theauths object
(auths.airtable)
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across invocations with$checkpoint.
async (event, steps, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
}
58
records = [];
updates = [];
var ab;
var status;
var notes;

for (let mglUpdates of steps.getUpdateData.$return_value) {
   if(!mglUpdates.record.fields.ab) {
      ab = '';
   } else {
      ab = mglUpdates.record.fields.ab;
   }
   if(!mglUpdates.record.fields.AutoStatus) {
      status = '';
   } else {
      status = mglUpdates.record.fields.AutoStatus;
   }
   if(!mglUpdates.record.fields.Notes) {
     notes = '';
   } else {
      notes = mglUpdates.record.fields.Notes;
   }
   if(!mglUpdates.record.fields["Date Accepted"]) {
      mglUpdates.record.fields["Date Accepted"] = null;
   }
   if(!mglUpdates.record.fields["Date Returned"]) {
      mglUpdates.record.fields["Date Returned"] = null;
   }
   if(!mglUpdates.record.fields["Date Dispatched"]) {
      mglUpdates.record.fields["Date Dispatched"] = null;
   }
   updates.push({
    fields: {
    "accepted by": ab,
    "status": status,
	 "notes from maker": notes,
    "accepted by maker": mglUpdates.record.fields["Date Accepted"], 
    "received from maker": mglUpdates.record.fields["Date Returned"],
    "sent to dispatch": mglUpdates.record.fields["Date Dispatched"]
    },
    id: mglUpdates.record.fields["source rid"]
   })
    

}
records.push.apply(records,updates);
console.log('records',records);
if (records[0].id === undefined) {
   console.log('no source id');
    $end("No records to update")
   
} else {
   console.log('source id exists')
   return records;
}
steps.
update_records
This action accepts an array of update objects. Each of these objects should have two properties: 1) fields, which contains all of your record's cell values by field name, and 2) id, the ID of the record in Airtable you'd like to update. You can include all, some, or none of the field values.
auth
(auths.airtable)
params
Records

An array of update objects. Each of these objects should have two properties: 1) fields, which contains all of your record's cell values by field name, and 2) id, the ID of the record in Airtable you'd like to update. You can include all, some, or none of the field values.

[0]:
array ·params.records
Base ID

Your Airtable Base ID

string ·params.base_id
Table Name

The name of your table in Airtable

string ·params.table_name
code
async (params, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
}
31
const chunk = require('lodash.chunk');
const BATCH_SIZE = 10; // Airtable API allows to update up to 10 rows per request.

const { records } = params
if (!records || !records.length) {
  console.log("No records found")
  return
}
const size = records.length; 
const response_records = []

async function updateRecords(data){
  const config = {
    method: "patch",
    url: `https://api.airtable.com/v0/${params.base_id}/${encodeURIComponent(params.table_name)}`,
    headers: {
      Authorization: `Bearer ${auths.airtable.api_key}`,
    },
    data
  };
  return await require("@pipedreamhq/platform").axios(this, config);
};

const records_sets = chunk(records, BATCH_SIZE);
for (const records_set of records_sets) {        
  response_records.push(await updateRecords({ records: records_set }));
}

return response_records;