Fiber optic strands
Image credit: Denny Müller

f451 Communications Library

TL;DR

This module provides a universal interface for various communications systems and services (e.g. email, Slack, SMS, etc.) and makes it possible to send the same message to several services with a single method call. The same call structure is used regardless of which services are enabled.

Current support

  • Email via Mailgun — plain text and HTML, with attachments and inline images
  • Slack — plain text and Slack blocks
  • SMS via Twilio — SMS with images
  • Twitter — status updates and DMs

Overview

This module was originally created to “scratch an itch.” I had several single-purpose applications running on different devices (e.g. Raspberry Pi) configured to support specific hardware configurations (i.e. sensors and displays, etc.), services, or functions. And all applications were designed to notify me via different channels that certain events had occurred and so on.

Using a standardized communications library , I’m able to call a simple send_message() method, which works the same way regardless of which services are enabled for a given device. And if I add a new communications channel, I can enable it quickly on my devices without updating the core applications. Simply adding the new channel to a configuration file is enough 😎

from configparser import ConfigParser, ExtendedInterpolation
from f451_comms.comms import Comms

secrets = ConfigParser(interpolation=ExtendedInterpolation())
secrets.read("_PATH_TO_YOUR_SECRETS_FILE_")

comms = Comms(secrets)
comms.send_message("Hello world!", "all")

The basic sequence is to first initialize the Comms object with the keys and secrets required to authenticate with the services that you want to use. After that you can send messages to one or more channels with a single method call to the Comms object.

The send_message() method also has a 3rd argument that allows you to include additional attributes using a dict structure. These attributes can contain a wide variety of items such as the HTML version of an email, or Slack blocks for more complex Slack messages. You can also include references to images or files to be attached to emails, and so on.