Easy Slack bot from Google Apps Script

This article explains how to create a slack bot which returns a gif url from giphy with Google Apps Script. (Easy and Free way to make a bot!)

STEP 1: Create Google App Script (GAS)

Create Google Apps Script from here.
Write this code.
Deploy as web app.
Change ‘Who has access to the app to “Anyone, even anonymous”’. Save the web app URL.

STEP 2: Get Giphy api key and Set it in GAS

From here, ‘Create an App’ and then enter the name and description whatever you want.
You can find the Giphy Api Key here!
Back to GAS and set the key to Script properties.
Currently you may have only ‘GIPHY_API_KEY’. You will get ‘SLACK_ACCESS_TOKEN’ next step.

STEP 3: Get Slack bot token add it to GAS property.

From here, click ‘Create New App’. Then enter the name and select which workplace to invite this bot to.
You can change basic information.
You need to add three features.

1. Add a Bot User

2. Enable Events (to receive POST request to GAS from slack when someone say something on slack)

Add URL of GAS web apps of STEP 2. This checks if gas receive a post request and return a appropriate answer. (The code we wrote return it.)
Choose when to get post request. (Our bot needs it when someone send messages.)

3. Add permissions (the bot to send messages to slack)

Our bot needs to send message and unfurl the gif from link.

Finally. Install app and set the authentication code which enables GAS to send messages to Slack.

Install it to your workspace!
Then you can get the token from ‘OAuth & Permissions’. (Same page as that we added permissions.)
Yes, add it to GAS.
And change the code. Comment out the line 16 (This line is used only for the check).
And deploy as web app. we need the project version to be set ‘New’.

That’s All!

When you said the word ’dog’ in the channel which the Bot can hear it, it returns some gif!

If you want to know more about the code, please read this story.