forked from lab-skku/lab-github-pr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·89 lines (64 loc) · 2 KB
/
app.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os
import ngrok
import requests
from dotenv import load_dotenv
from flask import Flask
app = Flask(__name__)
load_dotenv()
slack_token = os.getenv('SLACK_TOKEN')
github_token = os.getenv('GITHUB_TOKEN')
ngrok_token = os.getenv('NGROK_TOKEN')
@app.route('/')
def index():
return {'message': 'Hello, folks!'}, 200
@app.route('/slack/events', methods=['POST'])
def slack_events():
data = request.json
event_type = data.get('type')
if event_type == 'url_verification':
challenge = data.get('challenge')
return jsonify({'challenge': challenge})
elif event_type == 'event_callback':
event = data.get('event')
if event['type'] == 'message':
response_text = "Handled message event"
else:
response_text = "Not supported event"
response = {
'text': response_text
}
return jsonify(response)
else:
return '', 200
@app.route('/slack/command', methods=['POST'])
def slack_command():
data = request.form.to_dict()
command = data.get('command')
if command == '/slack_command':
response_text = "The command of '/slack_command' executed"
else:
response_text = "Not supported command"
response = {
'response_type': 'in_channel',
'text': response_text
}
return jsonify(response)
@app.route('/github/<github_id>', methods=['GET'])
def github(github_id):
api_url = f'https://api.github.com/repos/lab-skku/lab-github-pr/collaborators/{github_id}'
data = {
'permission': 'admin'
}
headers = {
'Authorization': f'token {github_token}'
}
try:
response = requests.put(api_url, headers=headers, json=data)
if response.status_code == 204:
return {'message': 'added'}, 200
else:
return {'message': response.status_code}, 400
except requests.exceptions.RequestException as e:
return {'message': e}, 500
if __name__ == '__main__':
app.run(debug=True)