⬆️ ⬇️

Writing a bot for Slack in Python

A small tutorial on how to make a simple slak bot in Python, deploy it on Heroku, connect Travis CI in twenty minutes and start doing something useful.



So, we have a bot that punishes people who write to the “hi” chat, only to distract us from work. How to make yourself the same, but better?





Heroku + Python Web App



The installation of the hero is perfectly described here, we need only three files

')

 - requirements.txt
 - Procfile
 - runtime.txt


In the first one we list all the dependencies like this.



httplib2 slacker Flask==0.12 


you can not write them right away, smart PyCharm will tell you that we missed something.

Httplib2 we need to send requests for authorization, Slacker - to work comfortably with the Slack API, and Flask - to receive requests, this is the easiest and most painless web framework that requires zero configuration.



In runtime.txt we specify the version of Python that we like.

Procfile also has exactly one line.

 web: python app.py 


Now we are ready to write our first application, create app.py with such content.



 from flask import Flask from flask import request from flask import make_response app = Flask(__name__) #   @app.route('/webhook') def hello_slack(): #     request_json = request.get_json(silent=True, force=True) #           dict  ,    #   request_json -> response_body_json ... response_body = json.dumps(response_body_json) #      response = make_response(response_body) response.headers['Content-Type'] = 'application/json' #   return response if __name__ == '__main__': port = int(os.getenv('PORT', 5000)) app.run(debug=False, port=port, host='0.0.0.0') 


We push everything on the githab, go to Heroki and create a new application from the public repository. Heroku will download the codes himself, install dependencies from requirements.txt and launch the application according to Procfile .



Slack



Everything, from this moment we have a backend at



  https://YOUR_APP_NAME.herokuapp.com/webhook 
and you can create your own slak application that will * do-something-useful * . It remains to validate it for the events API (that is, the most useful API - API notifying us of any events). Slack itself is just a special challenge-request from our endpoint, from which you need to get the code and put it in response. Now in the settings of the slak application in the Event Subscriptions section we select the events we need and that's all. Events will fall to the same address. As soon as you do everything you want with the input data, you need to support the authorization of other users. To do this, you will need another endpoint, for example / auth , to which the slak will send a request as soon as the new user wants to install his application. After receiving such a request, you need to remove the code from it and send it to the POST on slack.com/api/oauth.access together with the application credits, and respond to the request itself with any successful response or redirect to the page you want to show to the user after logging in .



Travis ci



The bot is ready. It remains to make sure that it works. The application can always be run locally and send requests via curl, but there is little sense from this, so we immediately write unit tests and ask Travis to monitor the repository. A project in Travis is created almost as intuitively as in a hero, only the rep's address is needed, the rest will be done by yourself if you create another file in the root of the project .travis.yml



 language: python python: - "3.6" script: python3 -m unittest discover 
In the script section, we tell Trevis what to do after the latest version of the source code has been downloaded. This command (which you should run locally more often) will find the unit tests in our project and run them out.



From this point on, Travis and Heroku (if enabled in the settings) monitor the repository and download your application, deploy it and run the tests, send the results of their execution to the post. And you are almost ready for production, as soon as everything works, you only need to turn off the automatic installation of the sorts in Heroku, and to reconfigure Travis so that he himself can deploy it only when the tests have been successful. Profit!



The links to the sources are here , and the living Travis is here . The bot itself is available only to colleagues of the native company, but I do not exclude its release to public, if there is interest.



Good luck!

Source: https://habr.com/ru/post/334678/



All Articles