My boss wants us to greet the team every morning but I'm a busy woman so here's how I automated a "good morning" message to slack every day from my PC.
- Step 1: Generate a user token
We need a user token to authorise messages send to the slack API, these are tokens that start with "xoxp". If you don't already have one, we'll have to create a slack app, install it to our slack workspace, and give it "user:chat" permissions to generate a token.
- Go to https://api.slack.com/apps/ to create a new app. It will need to be made on the same workspace you're "good morning" messages will be posted to
- In your new app, go to "App Manifest" in the settings menu on the left then click "Edit"
- In the manifest add a "user" to the oauth_config scopes and give it "char:write" permissions e.g.:
oauth_config:
scopes:
user:
- chat:write - Now go to "OAuth & Permissions" in the settings menu and we should now have a User OAuth Token.
- Install/reinstall your app to the workspace. There should be a prompt asking you to do so (You'll need to do that every time you make any change to the app)
- Step 2: Write a script to post your message using postMessage in the API
You'll need to provide these arguments:
- token (your user token)
- channel (the channel to post the message)
- as_user (set to true)
There are multiple ways to do this, check out the API link above for more info. But I'm going to use a batch script and http for simplicity.
I've also randomised my message slightly for variety.
set min=1set max=3set /a selectionNum=(%RANDOM%*max/32768)+minif %selectionNum%==1 (set message=good morninggoto sendmsg)if %selectionNum%==2 (set message=good morning :slightly_smiling_face:goto sendmsg)if %selectionNum%==3 (set message=morning!goto sendmsg):sendmsgcurl -d "text=%message%" -d "channel=general" -d "as_user=true" -H "Authorization: Bearer xoxp-xxxxxx-xxxxxx-xxxxxx-xxxxxx" -X POST https://slack.com/api/chat.postMessage
- Step 3: Trigger your script in the morning
On Linux or Mac you can set up a cron job, I'm on Windows so I'm going to use the Task Scheduler.
- Open Windows Task Scheduler and create a new task
- Add a name as description
- Click the "Triggers" tag and set the job to run every day at whatever time (chose an slightly off time like 9:08 so the messages look more natural. I've set mine to 9:17 because everyone knows I'm not an early bird)
- Click the "Actions" tab and add the path to your script
- Save your task
- Step 4: Carry on with your life!
Your script will be social for you. As long as your computer is on the script will be able to send the message.