Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences…

Follow publication

Slack Automated Messaging with Webhooks using Python

Hello Every one I hope all are fine and good, today we are going to see about automated messaging in Slack application using web hooks with python programming language. Let's dive !

Slack Webhooks

You might have seen a webhook choice in your slack settings and puzzled if this can be one thing you ought to use. Victimisation webhooks one, will send machine-controlled messages (JSON payload) from one app to different. This could be triggered in response to specific events. Making an Incoming Webhook provides you a novel universal resource locator to that you send a JSON payload with the message text and a few choices.

Getting started with webhooks

Step 1: Create a Slack workspace by clicking here

Step 2: Go to Browse Slack and click Apps + icon and search for incoming webhook, click on Add

incoming webhook

Step 3: From the App directory page, add incoming webhooks to slack

Step 4: After adding webhooks it will redirect to channel configuration page at that select your channel then click add channel.

Step 5: Don't forgot to copy the webhook URL

Step 6: After that, our Slack channel look like this,

Step 7: Then create a new python file called app.py, you can name it with your favourable file name with .py extension

Step 8:

Prerequisites: Install request module

Pip install request

import jsonimport sysimport randomimport requestsif __name__ == ‘__main__’:     url = “<Webhook URL>”
message = (“A Sample message by bot”)
title = (f”New Incoming Message :zap:”)
slack_data = {
“username”: “AshBot”,
“icon_emoji”: “:satellite:”,
“channel” : “general”,
“attachments”: [
{
“color”: “#9737EE”,
“fields”: [
{
“title”: title,
“value”: message,
“short”: “false”,
}
]}
]}
byte_length = str(sys.getsizeof(slack_data))
headers = {‘Content-Type’: “application/json”, ‘Content-Length’: byte_length}
response = requests.post(url, data=json.dumps(slack_data), headers=headers)
if response.status_code != 200:
raise Exception(response.status_code, response.text)

Step 9: Finally, our channel will get a message from our source file

Thank You !

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Nerd For Tech
Nerd For Tech

Published in Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Ashwin Kumar Ramasamy
Ashwin Kumar Ramasamy

Written by Ashwin Kumar Ramasamy

Passionate in software development, network security and technical training.

No responses yet

Write a response