Post a message from Slack to Twitter with Zapier

( Originally posted in https://dev.to/hamatti/slack-to-twitter-with-zapier-3id0. )

I was chatting with friend on a Slack community last Friday, and he had a feature he'd love to have. When people post new job ads into #jobs channel, he'd love to automatically post them to Twitter for larger reach. I liked the idea!

As a big fan of automation and Zapier, I told I could take a look over the weekend and so I did.

Purely Zapier-run option

If you are not familiar with Zapier, it's an app that allows you to connect web apps to each other. You specify a trigger (new post in Slack, new tweet, new signup for email list, new form submission, you name it), any intermediate steps (filtering, text transformation, logging to Google Sheets, etc) and finally an action like posting to Twitter.

After bit of testing things out, here's a Zap I created for posting wanted job ads to Twitter.

1. Trigger from Slack

Screenshot from Zapier showing Slack selected as a Trigger and action New Message Posted to Channel selected

Screenshot from Zapier showing Jobs channel selected as a trigger channel and bot option selected as No

Select Slack as your source and in Edit options section selected the wanted channel and if you want to trigger with bots or only humans.

2. Filter by content

I only wanted to include some posts with a certain hashtag (for example, #twittershare). This way users could decide if they want their post to be tweeted and we avoid awkward moments where someone answers a question and ends up in Twitter.

Screenshot from Zapier showing Filter action selected in app selection screen

Use Filter app by Zapier to filter by values.

Screenshot from Zapier showing Only continue if... option selected for Filter

We want to only continue if the message from Slack matches our filter.

Screenshot from Zapier showing we are using Text Contains #twittershare filtering

Filtering by text is easy.

3. Remove #twittershare from message

We don't actually want to show our keyword in the tweet, it's only for managing the Zap. So let's remove it by using Formatter action:

Screenshot from Zapier showing we have selected Formatter app

Screenshot from Zapier showing we have selected text formatter

Screenshot from Zapier showing how to remove text with replace function

4. Let's tweet!

Posting a tweet with Zapier is easy as pie.

Screenshot from Zapier showing we have selected Twitter app

Screenshot from Zapier showing we have selected the Create tweet action

Screenshot from Zapier showing how to use templates to build a tweet

With Twitter app, you can use templating to build a message. You can add text, use values from Slack post (like timestamp, poster name, text, etc).

Conclusion

After you have done these steps, every time someone posts a message with keyword #twittershare in the message to #jobs channel, our account will send a tweet to Twitter.

This is this easiest way to make things happen and I highly recommend using this.

Slack-GoogleSheets-Twitter option

Zapier's free tier only allows for two-step Zaps to be created. So you cannot filter, you cannot replace text, etc. The cheapest paid tier is $25/month and if you only need one Zap like the one described above, you might be hesitant on paying.

Good thing is that we can basically chain Zaps by creating input to one service in one Zap and reading from it in another Zap. Add some Google Script programming and we can build ours in two Zaps and 23 lines of Javascript.

By reading through steps in our Zapier-only option, you can see how to select apps and go through setup so I will only show the important sections from this point on.

Zap 1: Slack to Google Sheets

1. Trigger from Slack

This option is exactly the same as above, so I won't repeat it here.

2. Create Google Sheet

Log into your Google Drive and create a new Spreadsheet. I named mine Jobs for Zapier connection. Create two sheets inside it, named "source" and "target". For both of these, add headers "Timestamp", "User" and "Content" into cells A1 through C1. We are only logging timestamp and user for debugging purposes.

3. Write to source sheet

Screenshot from Zapier showing choosing action Create Spreadsheet Row

Use Create Spreadsheet Row as your action.

Screenshot from Zapier showing how to add data

Choose your spreadsheet and source sheet inside it. Choose which values to write to columns and you're good to go.

After this, we'll have a Google Spreadsheet that automatically adds every post to Slack #jobs channel into a source sheet. Next, we need to do little bit of programming in Google Scripts to duplicate only the ones we want from source into target.

Google Script

In Google Spreadsheet, go to menu Tools->Script editor. This will open a new script editor, you might have to give it permission to your Google Drive.

Give your script a name, I called mine Conditional Copy. Write the following code into the Code.gs file:

function conditionalNewRow() {
var SPREADSHEET_ID = ""; // This one you can get from the url of your spreadsheet.
var TIMESTAMP = 0;
var USER = 1;
var CONTENT = 2;

var ss = SpreadsheetApp.openById(SPREADSHEET_ID);

var sourceSheet = ss.getSheetByName("source");
var targetSheet = ss.getSheetByName("target");

var lastRowIdx = sourceSheet.getLastRow();
var range = sourceSheet.getRange(2, 1, lastRowIdx - 1, 3);

var lastRow = range.getValues().splice(-1)[0];

var newContent = lastRow[CONTENT];

if (newContent.indexOf("#twittershare") > -1) {
var stripped = newContent.replace("#twittershare", "");
var newRow = [lastRow[TIMESTAMP], lastRow[USER], stripped];
targetSheet.appendRow(newRow);
}
}

After you have saved the file, go to menu Edit->Current project's triggers. Select Add trigger. You need to choose your function conditionalNewRow and event type On change. This will cause the function to be run every time your source sheet updates through Zapier.

You can test it to function by posting a new post to your Slack channel with and without the keyword and see that both of them should appear in source sheet and only the one with keyword should appear in target. If that works correctly, continue to the next step. If it fails, make sure your Zap 1 is switched on.

Please note that this function happens on every change and always reads the last line from source. If you do any manual changes to the file, it might re-send your latest tweet.

Zap 2: Sheets to Twitter

This is similar to Slack to Twitter from our Zapier-only route. Select Google Sheets as an app for trigger and New Spreadsheet Row an action. Choose target as your sheet this time.

Twitter side of the action is same as in Zapier-only route.

Tada, you're done!

It took a few extra steps, bit of programming and added a bunch of variables that can cause this to fail. For my testing, it seemed to work really nicely and I got in tweets just as I liked them. And it works on a free-tier with Zapier.