Send a message from Slack to a Python server

Overview

Create a slcak bot and send a message from slack to the server. Sending a message from the server to slack is-> "Send a message from python to Slack"

things to do

1 Prepare the server

Prepare a server to access from slack. It is necessary to meet the conditions by referring to url_verification event.

python code sample

import flask
from flask import request, Response
import os
import json

app = flask.Flask(__name__)


@app.route('/', methods=["POST"])
def index():
    data = request.data.decode('utf-8')
    data = json.loads(data)
    # for challenge of slack api
    if 'challenge' in data:
        token = str(data['challenge'])
        return Response(token, mimetype='text/plane')
    # for events which you added
    if 'event' in data:
        print("get event")
        event = data['event']
        if 'user' in event:
            print("user = ", event["user"])
        if "text" in event:
            print("text = ", event["text"])
    return Response("nothing", mimetype='text/plane')


port = os.getenv('VCAP_APP_PORT', '8000')


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=int(port), debug=True)

2 Creating a slack app

2.1 Go to slack api.

2.2 Creating an app

Click "Create New App". Create an app by selecting the app name and workspace.

2.3 Turn Enable Events ON

On the Event Subscriptions page, set Enable Events to "ON" スクリーンショット 2019-11-27 22.10.54.png

2.4 Specify Request URL

Specify the url of the server prepared in [1](# 1-Preparing the server).

2.5 workspace event settings

Under Subscribe to workspace events, select workspace event from "Add Workspace Event". This time, select message.channels to get the message of the channel. Other events can be found at API Event Types.

スクリーンショット 2019-11-28 0.47.53.png

3 Install slack app in workspace

3.1 Install in workspace

スクリーンショット 2019-11-28 0.54.19.png Install from "Install App to Workspace". # 3.2 Add apps to channels The slack channel you want to add the app to-> Settings-> Add the app

reference

  1. Types of Slack Bot and how to make it roughly
  2. Events API
  3. url_verification event
  4. API Event Types
  5. Send a message to Slack from python

At the end

I was able to receive messages from the slack channel on the server! I think I can do anything. Once you pass the challenge, you can stop the server afterwards and start it again.

Recommended Posts

Send a message from Slack to a Python server
Send a message from Python to Slack
I want to send a message from Python to LINE Bot
Create a setting in terraform to send a message from AWS Lambda Python3.8 to Slack
Post a message from IBM Cloud Functions to Slack in Python
Send a message to LINE with Python (LINE Notify)
[Python] Throw a message to the slack channel
Post from Python to Slack
How to install NPI + send a message to line with python
Push notification from Python server to Android
Send a message from the server to your Chrome extension using Google Cloud Messaging for Chrome
Edit Excel from Python to create a PivotTable
How to send a message to LINE with curl
How to open a web browser from python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Send mail with mailx to a dummy SMTP server set up with python.
How to generate a Python object from JSON
Sample to send slack notification with python lambda
[Python] Get one year's message history from Slack
[python] Send the image captured from the webcam to the server and save it
[LINE Messaging API] I want to send a message from the program to everyone's LINE
POST messages from python to Slack via incoming webhook
Send log data from the server to Splunk Cloud
Send data from Python to Processing via socket communication
Steps from installing Python 3 to creating a Django app
From buying a computer to running a program with python
Consider a conversion from a Python recursive function to a non-recursive function
Python script to create a JSON file from a CSV file
[Python] How to call a c function from python (ctypes)
How to create a kubernetes pod from python code
ODBC access to SQL Server from Linux with Python
Post to vim → Python → Slack
Cheating from PHP to Python
A road to intermediate Python
Post to slack with Python 3
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Send a signal to subprocess
Switch from python2.7 to python3.6 (centos7)
Connect to sqlite from python
Post to Slack in Python
How to slice a block multiple array from a multiple array in Python
How to run a Python program from within a shell script
Summary from building Python 3.4. * From source to building a scientific computing environment
How to launch AWS Batch from a python client app
I want to start a lot of processes from python
How to specify a public directory Python simple HTTP server
Send experiment results (text and images) to slack with Python
Build a Python environment and transfer data to the server
Post a message to Google Hangouts Chat with a thread (Python)
Create a message corresponding to localization with python translation string
Send a request from AWS Lambda to Amazon Elasticsearch Service
Extract the value closest to a value from a Python list element
A note that deployed a Python application from Circle CI to Elastic Beanstalk and notified Slack
I made a server with Python socket and ssl and tried to access it from a browser
Procedure from environment construction to operation test of testinfra, a server environment test tool made by Python
A Python script that allows you to check the status of the server from your browser
Call Matlab from Python to optimize
Call a Python function from p5.js.
How to write a Python class
Touch a Python object from Elixir