Wednesday, 20 October 2021

How to set up an automatic "good morning" slack messenger

 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.
    1. 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
    2. In your new app, go to "App Manifest" in the settings menu on the left then click "Edit"
    3. 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
    4. Now go to "OAuth & Permissions" in the settings menu and we should now have a User OAuth Token. 
    5. 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)

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=1
set max=3
set /a selectionNum=(%RANDOM%*max/32768)+min

if %selectionNum%==1 (
set message=good morning
goto sendmsg
)
if %selectionNum%==2 (
set message=good morning :slightly_smiling_face:
goto sendmsg
)
if %selectionNum%==3 (
set message=morning!
goto sendmsg
)

:sendmsg
curl -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.
  1. Open Windows Task Scheduler and create a new task
  2. Add a name as description
  3. 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)
  4. Click the "Actions" tab and add the path to your script
  5. 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.




  Azure Trusted Signing Signtool Error SignTool Error: An unexpected internal error has occurred. Error information: "Error: SignerSign...