Automating Slack Channel Topics

I set up a Slack channel for my friends over someones concerns with WhatsApp. We have a ‘World Cup Bants’ channel and I thought it would be neat to have the topic set to the current match. I’d already found a World Cup calendar and added that to my google calendar and integrated this with Slack to announce the events an hour before the match. I had already added my google calendars to HA, I won’t show this here as it’s trivial.

The first thing I wanted to do was to get the match details along with the flags. I’m sure there are better ways to do this but I wanted to use the template method to practice using them;

- platform: template
  sensors:
    next_wc_match:
      friendly_name: "Next World Cup 2018 Match"
      value_template: >
        {% set game = states.calendar.fifa_2018_world_cup_russia.attributes.message %}
        {% set game = game|replace("[A] ","")|replace("[B] ","")|replace("[C] ","")|replace("[D] ","")|replace("[E] ","")|replace("[F] ","")|replace("[G] ","")|replace("[H] ","") %}
        {% set teams = {
          "first": game.split(' - ')[0],
          "second": game.split(' - ')[1]
        } %}
        {% for country, flag in [('Argentina', ':flag-AR:'), ('Australia', ':flag-AU:'), ('Belgium', ':flag-BE:'), ('Brazil', ':flag-BR:'), ('Colombia', ':flag-CO:'), ('Costa Rica', ':flag-CR:'), ('Croatia', ':flag-HR:'), ('Denmark', ':flag-DK:'), ('Egypt', ':flag-EG:'), ('England', ':flag-EN:'), ('France', ':flag-FR:'), ('Germany', ':flag-DE:'), ('Iceland', ':flag-IS:'), ('Iran', ':flag-IR:'), ('Japan', ':flag-JP:'), ('Korea Republic', ':flag-KR:'), ('Mexico', ':flag-MX:'), ('Morocco', ':flag-MA:'), ('Nigeria', ':flag-NG:'), ('Panama', ':flag-PA:'), ('Peru', ':flag-PE:'), ('Poland', ':flag-PL:'), ('Portugal', ':flag-PT:'), ('Russia', ':flag-RU:'), ('Saudi Arabia', ':flag-SA:'), ('Senegal', ':flag-SN:'), ('Serbia', ':flag-RS:'), ('Spain', ':flag-ES:'), ('Sweden', ':flag-SE:'), ('Switzerland', ':flag-CH:'), ('Tunisia', ':flag-TN:'), ('Uruguay', ':flag-UY')]  %}{% if country == teams.first -%}{{flag}} {{ teams.first}} vs. {%- endif %}{% if country == teams.second -%}{{ teams.second }} {{flag}}{%- endif %}{% endfor %}

I have been trying to do various things with the restful commands components and had some limited success but again completely failed to get this working so I gave up with that method and used a shell script;

#!/bin/sh

TOKEN="$my_slack_token"
TOPIC="$1"

curl -X POST -H "Authorization: Bearer $TOKEN" \
-H 'Content-type: application/json' \
--data '{"channel":"$my_slack_channel_id","topic":"'"$TOPIC"'"}' \
https://slack.com/api/channels.setTopic

the shell command is very simple;

slack_status_wcb:
  /home/homeassistant/.homeassistant/slack_set_topic_wcb.sh "{{ states("sensor.next_wc_match") }}"

then just add the automation;

# Set the world cup bants channel topic per the next match
- id: 'Set_WCB_Channel_Topic'
  alias: 'Set World Cup Bants Channel Topic'
  trigger:
    platform: state
    entity_id: calendar.fifa_2018_world_cup_russia
    to: 'on'
  action:
  - service_template: shell_command.slack_status_wcb

Of course you have to add the relevant scope to Slack but again this is well documented.

screenshot-2018-06-22-1306

Update: I’ve just now noticed that if the order of the teams is not alphabetical this gets it the wrong way round!