In the previous post, We have created a registration form and learned how to secure form with CSRF protection. Today we are going to integrate slack with our flask app. We are going to send a slack notification on user registration. Slack is a collaboration tool that helps your team to work together.
Table of content
Slack Setup and API Configuration
The first step is to create a slack app so we can get the API token. The slack API token will be used to send notification from our app to slack.
App Name is up to you like I have used the name Flask App for our WP Repair Gigs workspace. Make sure to select your Development team workspace.
The next step is to add a bot user under the Features section.
Click on the Add a Bot User button and fill the Display Name and the Default username and save the information.
The next step is to install our created app. The Install App is under the Settings section.
You will get the Slack App Authentication screen once you click on the Install App to Workspace button.
You will get the OAuth Access Token and Bot User OAuth Access Token after allowing the installation process.
Slack Notification with Flask
Now we have everything ready to send a notification to our slack workspace. We required to install slackclient library for our Flask app.
The next step is to create a class, we will use this class to send the notification with the help of slack client library.
import slack
class SlackHelper:
def __init__(self):
self.slackToken = 'Slack_Bot_User_OAuth_Token';
self.slackClient = slack.WebClient(token=self.slackToken);
def send_slack_message(self, channel, message):
blocks = [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": message
}
}
]
return self.slackClient.chat_postMessage(
channel=channel,
blocks=blocks
)
In this file, we required the Slack Bot User OAuth Token. We have created a method send_slack_message()
to send a simple message that required 2 parameters i.e. the channel name and the message that you are sending.
We are going to use this method for the registration process. So, we are going to modify it slightly so that we can get the registration field data.
def send_slack_message(self, channel, message, form=None):
blocks = [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": message
}
}
]
if form != None:
fields = [
{
"type": "mrkdwn",
"text": "*Full Name*"
},
{
"type": "mrkdwn",
"text": "*Email*"
},
{
"type": "mrkdwn",
"text": form.first_name.data + " " + form.last_name.data
},
{
"type": "mrkdwn",
"text": form.email.data
}
]
blocks[0]["fields"] = fields
return self.slackClient.chat_postMessage(
channel=channel,
blocks=blocks
)
Time to implement it to the form. Let’s open the registration process part.
from flask import render_template, redirect, url_for, jsonify
from .utils.slackhelper import SlackHelper
from . import app
from .forms import RegistrationForm
....
....
@app.route('/register', methods = ['POST'])
def register():
form = RegistrationForm()
if form.validate_on_submit():
slack = SlackHelper()
response = slack.send_slack_message('#general', 'New Registration', form)
if response['ok']:
return redirect(url_for('index'))
return render_template('auth/registration.html', form=form)
This is what we get after applying the above codes.