Automated Slack lunch status

2021-05-20

Updating your Slack status doesn't really take that much time, it only takes a few clicks.

BUT as a developer I am "lazy" by nature and try to automate as much as possible in life, hence bringing in Home Assistant once again.

Slack API

This post assumes that you've already created an application in Slack and added the users.profile:write user token scope

Make sure to write down the User OAuth Token which you can find at the top of the page, we will need it later when making change requests.

The API is well documented, let me show you a small example of the POST request we will be using:

1
2
3
4
5
6
7
8
9
10
POST /api/users.profile.set
Host: slack.com
Content-type: application/json; charset=utf-8
Authorization: Bearer xoxp_secret_token
{
    "profile": {
        "status_text": "Eating some french fries from the frituur",
        "status_emoji": ":fries:"
    }
}

input_boolean

I created an input_boolean in Home Assistant called input_boolean.lunch_break. The input is also exposed to my Google Assistant using Home Assistant Cloud behind the scenes, making it controllable by voice when configured from within a routine (pardon my Dutch)

Automation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
trigger:
- platform: state
  entity_id: input_boolean.lunch_break
condition: []
action:
- choose:
  - conditions:
    - condition: state
      entity_id: input_boolean.lunch_break
      state: 'off'
    sequence:
    - service: rest_command.change_slack_status
      data:
        status: ''
        emoji: ''
  - conditions:
    - condition: state
      entity_id: input_boolean.lunch_break
      state: 'on'
    sequence:
    - service: rest_command.change_slack_status
      data:
        status: Lunch
        emoji: ':hamburger:'

The automation is triggered when the state of input_boolean.lunch_break changes. When the value is equal to on an API call is triggered using a status and an emoji of my own choice, no worries I'll show the change_slack_status service in just a bit. When the value is equal to off an API call is triggered passing empty values resulting in the Slack status being cleared:

To manually unset a user's custom status, provide empty strings to both the status_text and status_emoji attributes: "".

- https://api.slack.com/methods/users.profile.set

change_slack_status

1
2
3
4
5
6
7
8
9
rest_command:
  change_slack_status:
    url: https://slack.com/api/users.profile.set
    method: POST
    headers:
      authorization: !secret slack_auth
      accept: 'application/json, text/html'
    payload: '{"profile":{"status_text": "{{ status }}","status_emoji": "{{ emoji }}"}}'
    content_type: 'application/json; charset=utf-8'

This service that is being exposed using the RESTful Command integration calls the Slack endpoint passing the required data.

Demo

If you have any questions, do not hesitate to contact me or leave a comment below.

Created by Jeroen Druwé