• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
Maker Hacks

Maker Hacks

Ideas, news & tutorials for makers and hackers – Arduino/Raspberry Pi, 3D printing, robotics, laser cutting, and more

  • Home
  • About
  • 3D Printing
  • Laser Cutting
  • YouTube
  • Free Arduino Course
  • Recommendations
  • Contact

Linux Automation Tip: How to Send Messages from Python to Your Slack Channels

You are here: Home / Software / Linux Automation Tip: How to Send Messages from Python to Your Slack Channels
FacebookTweetPin
Author: Chris Garrett

Slack is a hugely popular live group chat system. While a lot of my notifications now go to Discord, as I wrote about here, Slack is still more popular with development teams, and it is what we use in my day job. Being able to notify your team when something happens programmatically can be very useful, and helps cut down on inbox clutter. Let’s see how you can easily do that in your own tools.

The first thing you will need to do is get an API key in /apps/manage/custom-integrations:

Next you need the Python module, Slack Client:

sudo python3 -m pip install slackclient

As our mission is to send messages to our Slack, we need to first discover the ID of the channel. We can simply list all the channels and their associated IDs with the following quick script:

import os
from slackclient import SlackClient
# get the api key from the environment variables
secret = os.environ["SECRET"]
# connect to the api and create client
sc = SlackClient(secret)
# set up the channel api call
# excluding archived channels
api_call = sc.api_call(
"channels.list",
exclude_archived=1
)
# get the list of channels
channels = api_call.get('channels')
# output the channels and their IDs
# formatted in nice columns for readability
print()
for channel in channels:
print("{} {}".format(channel.get('name').ljust(25), channel.get('id')))

Notice I am keeping my API key in an environment variable.

Another option would be to store it in a text file, and read it like so:

with open("/home/example/slack.api") as file:
secret = file.read()

Once you have the ID, copy and paste it into the following code to send a message:

from slackclient import SlackClient
# get the api key from the environment variables
secret = os.environ["SECRET"]
# connect to the api and create client
sc = SlackClient(secret)
# send a message to a specific channel
api_call = sc.api_call(
"chat.postMessage",
channel="YOUR CHANNEL ID HERE",
text="Hello World :wave:"
)

Related

Category: SoftwareTag: Hacks, linux, making, python, raspberry pi
FacebookTweetPin

About Chris Garrett

Marketing nerd by day, maker, retro gaming, tabletop war/roleplaying nerd by night. Co-author of the Problogger Book with Darren Rowse. Husband, Dad, 🇨🇦 Canadian.

Check out Retro Game Coders for retro gaming/computing.

☕️ Support Maker Hacks on Ko-Fi and get exclusive content and rewards!

Previous Post:EZ-Robot IoTiny
Next Post:Announce Your Entry with Raspberry Pi, Python and Bluetoothprogram bluetooth on raspberry pi with python

Sidebar

  • Facebook
  • Twitter
  • Instagram
  • YouTube

Recently Popular

  • Gweike Cloud Review
  • How to choose the right 3D printer for you
  • Glowforge Review – Glowforge Laser Engraver Impressions, Plus Glowforge Versus Leading Laser Cutters
  • Prusa i3 Mk3S Review
  • Best 3D Printing Facebook Groups
  • xTool D1 Pro Laser Cutter Review
  • Elegoo Mars Review – Review of the Elegoo Mars MSLA Resin 3D Printer
  • Glowforge ‘Pass-Through’ Hack: Tricking the Front Flap of the Glowforge with Magnets to Increase Capacity
  • How to Make a DIY “Internet of Things” Thermometer with ESP8266/Arduino
  • Wanhao Duplicator i3 Review
  • IKEA 3D Printer Enclosure Hack for Wanhao Di3
  • Creality CR-10 3d printer review – Large format, quality output, at a low price!
  • 3D Printed Tardis with Arduino Lights and Sounds
  • Anet A8 Review – Budget ($200 or less!) 3D Printer Kit Review
  • Make your own PEI 3D printer bed and get every print to stick!
  • Upgrading the Wanhao Di3 from Good to Amazing
  • How to Install and Set Up Octopi / Octoprint
  • Creality CR-10 S5 Review
  • Glowforge Air Filter Review
  • Thunder Laser Review
  • 3D Printing
  • CNC
  • Hardware
  • Laser Cutting
  • News
  • Retro Computing
  • Software

amiga amos arduino basic programming budget buying cad cnc commodore corexy c programming creality crypto doctor who emulation esp32 esp8266 filament fpga glowforge Hacks Ideas kids lighting linux Makes making Monoprice Parts pc props prusa python raspberry pi ratrig resin Reviews robots software sounds technology tips upgrades wanhao xtool

.

Maker Hacks Blog Copyright © 2023 · All Rights Reserved · Powered by Mai Theme