How I use Slack as a logging tool

26 Apr 2024

Slack is a direct messaging platform with many IRC-style features. I use the messaging platform in my “company of one” to write messages to my self about interesting stuff. I mainly use it to notify my self on sales and errors, but you can do anything really. The advantage of Slack is that you can always mute channels or the entire application for a while or indefinitely if you want.

Setting up your own application

  1. Log into Slack.
  2. Go to https://api.slack.com/apps.
  3. Click “Create New App” and follow the wizard.
  4. You should retrieve an API key.
The API key will be used in the following to log into Slack. In my Laravel websites I save the API key in the .env-file and in Python I do something similar, however for testing purposes, you can simply write the API key directly in your code. Let's take a look at how you can write messages in Slack with your fresh API key.

Writing Slack messages

I utilize Slack in my Laravel websites and my Python scripts. Let's take a look at how I implement them.

Writing Slack messages in Laravel/PHP

In my Laravel applications I'll create a Slack-controller with a public static function that writes messages via cURL to the Slack API. It looks like this:
public static function sendMessage(message)
{
	curl_setopt(ch, CURLOPT_CUSTOMREQUEST, 'POST');
	curl_setopt(ch, CURLOPT_POSTFIELDS, data);
	curl_setopt(ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt(ch, CURLOPT_SSL_VERIFYPEER, false);
	result = curl_exec(ch);
	curl_close(ch);

	return result;
}
I can then call the function from other controllers like this:
SlackController::sendMessage('Hello.');

Writing Slack messages in Python

With the Python packages “dotenv” and “slackclient” installed and a .env-file with your Slack API key, you can run the following code:
import dotenv
import os
import slack

dotenv.load_dotenv('.env')
client = slack.WebClient(token = os.environ.get('SLACK_API_KEY')
client.chat_postMessage(channel = 'general', text = 'Hello.')

Wrapping up

That's how I use Slack in my one man army to keep me notified about important stuff in my applications. I'd love to hear, how you use or could imagine using Slack in your setup.

You might also enjoy

How to backup your data

How to backup your data

Published 2024-04-26

Datahoarding

A walkthrough of how to backup your data properly, so you don't end up having to cry, when your luck finally runs out. I share how I do it my self.

Read the post →
Simplifying Your Code with Advanced Operators in PHP

Simplifying Your Code with Advanced Operators in PHP

Published 2024-05-05

PHP

Web development

Ready to write PHP code like a pro? Learn how to use advanced operators to make your code more concise and readable, and take your skills to the next level.

Read the post →
Creating your own self-hosted YouTube

Creating your own self-hosted YouTube

Published 2024-04-26

Datahoarding

Python

Using Youtube API, yt-dlp and Laravel, you can create your own self-hosted Youtube. Learn to scrape data and download videos.

Read the post →