-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_slack.py
48 lines (38 loc) · 1.19 KB
/
app_slack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from slackclient import SlackClient
from flask import Flask, request, Response, jsonify
import json
import requests
app = Flask(__name__)
SLACK_TOKEN = '---'
slack_client = SlackClient(SLACK_TOKEN)
slack_token = '---'
def send_message(channel_id, message,user):
slack_client.api_call(
"chat.postMessage",
channel=channel_id,
text=message,
username="Vitti",
icon_emoji=':sansa:'
)
@app.route('/slack',methods=['POST'])
def inbound():
data = request.json
if request.json.get('type') == 'url_verification':
return request.json.get('challenge'),200
if request.json.get('token') == slack_token:
messaging_events = data.get('event')
channel = messaging_events.get('channel')
user = messaging_events.get('user')
text = messaging_events.get('text')
bot = messaging_events.get('bot_id')
if 'subtype' not in data['event'].keys():
url = "http://localhost:12345/chatbot"
text = {"text" : text}
reply = requests.post(url,json=text).text
send_message(channel, reply, user)
return Response(),200
@app.route('/',methods=['GET','POST'])
def test():
return jsonify({'status':'ok'})
if __name__ == '__main__':
app.run(host='0.0.0.0',port=12345,debug=True)