-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmattermost.py
More file actions
73 lines (62 loc) · 2.13 KB
/
mattermost.py
File metadata and controls
73 lines (62 loc) · 2.13 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/python
from flask import Flask, request, Response
import settings
import json
import bot
import responses
app = Flask(__name__)
@app.route('/', methods=['GET'])
def api_welcome():
return responses.API_WELCOME
@app.route('/', methods=['POST'])
def receive_mattermost():
try:
body = request.json
token = body['token']
fromChannel = body['channel_name']
user_name = body['user_name']
requestText = body['text']
except:
return send_message_back( get_error_payload( fromChannel, responses.WRONG_SETUP ) )
if settings.MATTERMOST_TOKEN:
if not token == settings.MATTERMOST_TOKEN:
return send_message_back( get_error_payload( fromChannel, responses.INVALID_TOKEN ) )
payload = get_response( fromChannel, user_name, requestText )
return send_message_back( payload )
def send_message_back( payload ):
resp = Response(
json.dumps( payload ),
status=200,
mimetype='application/json')
return resp
def get_error_payload( channel, errorMessage ):
'''
Return the payload of the return message in case of an error
'''
return { 'response_type': 'ephemeral', 'channel': channel, 'text': responses.GENERIC_ERROR, 'username': settings.BOT_NAME,
'attachments': [{
'fallback': responses.GENERIC_PROBLEM,
'color': '#FF141A',
'text': '',
'fields': [
{
'short': False,
'title': responses.REASON,
'value': errorMessage
}
]
}]
}
def get_response( fromChannel, user_name, requestText ):
response_text, attachment = bot.get_response_and_attachments(user_name, requestText)
if not response_text and not attachment:
return
return { 'response_type': 'in_channel',
'channel': fromChannel,
'text': response_text,
'username': settings.BOT_NAME,
'icon_url': settings.BOT_ICON,
'attachments': attachment
}
if __name__ == '__main__':
app.run(host='0.0.0.0', port=settings.WEBSERVER_PORT, debug=True)