How to Use Vue.js and Axios to Display Data from an API
0

Introduction

Slack is an interaction platform for groups. Slack has add-ons that are numerous let teams extend Slack, and integrate it with other programs. Slack slash commands are a quick and way that is easy perform actions inside message input field. All users in the current channel for example, typing /who lists. You can find a list that is complete of slash commands at https://get.slack.help/hc/en-us/articles/201259356-Slash-commands.

You can cause your commands that are slash members of your Slack workspace find useful. You can direct Slack to make a request to a program you’ve written when you install the command to your workspace and invoke the command. This system gets the given information from Slack and comes back a reply, that is shown in Slack. You are able to find out about Slack slash commands by reading the API paperwork.

In this guide, you are going to produce a Slack slash demand called /slash, running on a Flask application operating on an Ubuntu 16.04 host and install this demand towards Slack workplace. As soon as you’re done, typing /slash inside message input field will be sending information towards Flask application that will process the demand and get back a message that is short Slack helping you discover it worked.

You’ll provide this Flask application making use of a application that is uWSGI and make use of Nginx as a reverse proxy.

Prerequisites

To complete this guide, you’ll need:

Step 1 — making and setting up the Slack App

(in a development Slack workspace*******)Before we write our code, we will first create a Slack app, which provides additional functionality to Slack, and install it. Then we are going to determine the demand and specify the URL your demand should request whenever we invoke the demand.

To create a Slack application, see slack that is https://api and click on the green Create New App button. In the form that appears, enter the details that are following**********)

  • For App Name, enter DigitalOcean Slack slash demand.
  • For Development Slack Workspace, pick your development workplace.

Then go through the green Create App key.

Once the application is established, click Slash commands and click the ( then****************)Create New Command button. You’ll see the page that is following**********)

Page for creating new command.

For this guide, you are going to produce a demand called /slash, that will deliver information via HTTP POST to a demand URL which http://server_domain_or_IP/slash. Therefor, complete the information:( that is following**********)

  • Fill inside Command industry with /slash.
  • For Request URL, enter http://your_server_ip_or_domain/slash.
  • For Short Description, enter DigitalOcean Slack slash demand.

Then regarding the green Save key to complete producing the slash demand.

Now install the application towards workplace by simply clicking the Install App website link. Press the green Install App to Workspace key. Then click the green Authorize key.

We have finally produced and set up a Slack application inside development Slack workplace., nevertheless the demand will not work until we create an internet application that processes the slash demand. We need to configure our Python environment.( before we can build the app,**********)

Step 2 — Configuring the Python Environment

After finishing the How To Serve Flask Applications with uWSGI and Nginx on Ubuntu 16.04 tutorial, you have a app that is flask in ~/myproject/. This directory contains the files that are following directory:

  • myproject.ini
  • myproject.py
  • wsgi.py
  • myprojectenv/

We’ll change the Flask application in myproject.py to behave regarding the information delivered by the Slack slash demand and get back a response that is JSON Slack.

The API paperwork for commands states we should validate the slash demand utilising the verification token linked to the commnd, supplied by Slack.

This verification token is held key, so we are going to save your self it in a file that is new .env file which is not held under variation control. We will make use of the python-dotenv package to export the key-value pairs in .env as environment factors, and now we’ll access those environment variables in myproject.py.

First, stimulate the Python environment that is virtual operating:

  • source myprojectenv/bin/activate

To concur that the virtualenv is triggered, you ought to see (myprojectenv) regarding the side that is left-hand of Bash prompt. Secrets such as the verification token ought not to be saved under variation control. To do this, we make use of the python-dotenv package that exports the secrets as environment factors. Utilizing pip, we install the python-dotenv package:

  • pip install python-dotenv

Using nano or your text that is favorite editor create the .env file:

Get the verification token at https://api.slack.com/apps. Click the DigitalOcean Slack slash demand application then click Basic Information. Then find Verification Token.

The verification token

Copy the worthiness the token and put it inside .env file, assigning the worthiness to a breeding ground adjustable called VERIFICATION_TOKEN:

~/myproject/.env

VERIFICATION_TOKEN=your_verification_token

Save the file and leave the editor.

when you are developing a Flask application, you will want the server that is uWSGI reload immediately whenever you make modifications towards application. To work on this, first available myproject.ini inside editor:

Add this line towards end for the file to automatically ensure that uWSGI reloads whenever you touch or change the Flask application in myproject.py

~/myproject/myproject.ini

...

touch-reload = myproject.py

Save the file and leave the editor.

Now we are going to produce the Flask application that gets and operations the info delivered by the slash demand and comes back an response that is appropriate Slack.

Step 3 – Creating the Flask App

As soon as we invoke the /slash demand in Slack, Slack could make a demand to the host. We configured the demand hitting the url /slash, therefore we are going to affect the Flask application we intended to react to that endpoint.

Open myproject.py inside editor:

Remove the articles for the file. We will produce a app that is new scratch.

Add this rule which imports Flask and lots extra modules for managing JSON information and web that is making:

~/myproject/myproject.py

#!/usr/bin/env python

from flask import Flask, jsonify, demand

Then include this rule which loads the dotenv module. checks out the articles for the .env file you created. lots its articles into environment factors, after which fetches the verification token from environment, saving it inside adjustable verification_token:

~/myproject/myproject.py

...
import os
import dotenv

dotenv_path = os.path.join(os.path.dirname(__file__), '.env')
dotenv.load_dotenv(dotenv_path)
verification_token =.environ that is os*)

Now include this rule to configure the Flask application to react to the Slack slash demand by giving a text reaction that states "DigitalOcean Slack slash demand works!":

~/myproject/myproject.py

...

application =__name__ that is flask(

@app.route('/slash', techniques=['POST'])
def slash():
    if demand.form['token'] == verification_token:
        payload = {'text': 'DigitalOcean Slack slash command is successful!'}
        Return payload that is jsonify(


if __name__ == '__main__':
    app.run()

Save the file and leave the editor.

Restart the myproject systemd solution to guarantee the latest form of your rule is operating:

  • sudo systemctl restart myproject

Now let us change the Nginx setup to aid our slash demand.

Step 4 – Configuring Nginx to provide the Command

Because our demand URL is http://server_domain_or_IP/slash, we must replace the location value within our Nginx host block from / to /slash.

Open the file /etc/nginx/sites-available/myproject inside editor:

  • sudo nano***********************)myproject( that is/etc/nginx/sites-available/(************************)

Change the worthiness for location from / to /slash :

/etc/nginx/sites-available/myproject

...

    location /slash {
        consist of uwsgi_params;
        uwsgi_pass unix:/home/sammy/myproject/myproject.sock;
    }
}

Save the file and leave the editor.

Then check out the Nginx setup apply for syntax mistakes:

If there are not any errors that are syntax the Nginx setup file, restart the Nginx solution:

  • sudo systemctl restart nginx

Visit your development Slack workplace and kind /slash in almost any channel. You need to understand response that is following**********)

Slack slash command is successful!

If you will get a note stating your demand failed, double-check the rule in myproject.py for just about any errors that are syntax dilemmas. Then decide to try once more.

Finally, when you're done developing your demand, deactivate the Python environment that is virtual that future Python commands make use of the system Python interpreter:

You have finally effectively produced a Flask application that gets information from /slash slash demand and comes back a reply to Slack.

To ensure the interaction between host and Slack is protected, encrypt the bond the slash demand HTTPS that is using for request URL. You can do so by installing a SSL that is free certificate by let us Encrypt regarding the Nginx host. If you are done, make sure you change the Address for the Slack change and app(********)http:// to https://.

Conclusion

In this guide, you applied a Slack slash demand by creating a Flask application which offered by a application that is uWSGI and a Nginx reverse proxy ip server.

Now you can implement any slash command that your team needs that you know the basics of creating slash commands. You could create commands that retrieve data from databases, interact with other APIs , or commands that are even create deploying rule.

What’s a Core i3, Core i5, or Core i7 as quickly as possible

Previous article

Shadows in website design – 1stWebDesigner

Next article

You may also like

Comments

Leave a reply

Your email address will not be published. Required fields are marked *