#!/usr/bin/python3.8 # # Simple script to send a notification to a Slack app's # "Incoming Message Webhook". The configuration of that webhook # determines where/how it will be distributed. This script # doesn't care, and just uses the hook's default. # import sys import configparser import json import argparse import requests CONFIG_FNAME = '/etc/homesvr/slack.conf' def notify(args): cfg = configparser.ConfigParser() cfg.read(args.conf) if args.secondary: payload = { 'text': f'{args.message} (more content)', 'blocks': [ { 'type': 'section', 'text': { 'type': 'mrkdwn', 'text': f'*{args.message}*', }, }, { 'type': 'section', 'text': { 'type': 'plain_text', 'text': args.secondary, }, }, ], } else: payload = { 'text': args.message, } r = requests.post(cfg['webhook']['url'], json=payload) if __name__ == '__main__': parser = argparse.ArgumentParser(description='Sent a notification to Slack.') parser.add_argument('--conf', default=CONFIG_FNAME, help='Configuration file for Slack credentials/info.') parser.add_argument('-m', '--message', required=True, help='Message to send to the Slack app/bot.') parser.add_argument('-s', '--secondary', help='Secondary/subtitle message to send.') args = parser.parse_args() notify(args)