-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
99 lines (79 loc) · 3.49 KB
/
main.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
90
91
92
93
94
95
96
97
98
99
from flask import Flask, request, jsonify
import requests
app = Flask(__name__)
@app.route('/deactivate', methods=['POST'])
def deactivate():
token = request.form.get('token')
user_id = request.form.get('user_id')
text = request.form.get('text')
if not text:
return jsonify({"response_type": "ephemeral", "text": "Please specify the user(s) to deactivate."})
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {token}"
}
users = text.split()
response_message = f"User <@{user_id}> is deactivating users: {', '.join(users)}"
# Print the text to the console
print(f"Deactivate command triggered by user <@{user_id}> with users: {text}")
for user in users:
# Extract the user ID from the escaped user format <@U1234|user>
user_id_to_deactivate = user.split('|')[0][2:]
deactivate_payload = {
"user_id": user_id_to_deactivate
}
# Uncomment this section to actually send the request to Slack API
# response = requests.post('https://slack.com/api/admin.users.remove', headers=headers, json=deactivate_payload)
# if not response.ok:
# return jsonify({"error": response.text})
# Return a public message
return jsonify({
"response_type": "in_channel",
"text": response_message,
"delete_original": True
})
@app.route('/deactivate_all', methods=['POST'])
def deactivate_all():
token = request.form.get('token')
channel_id = request.form.get('channel_id')
user_id = request.form.get('user_id')
text = request.form.get('text')
exclude_channels = []
if text and text.startswith('exclude='):
exclude_channels = text[len('exclude='):].split(',')
if channel_id in exclude_channels:
return jsonify({"response_type": "ephemeral", "text": "This channel is excluded from deactivation."})
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {token}"
}
# Uncomment this section to actually send the request to Slack API
# response = requests.get('https://slack.com/api/conversations.members', headers=headers, params={"channel": channel_id})
# if not response.ok:
# return jsonify({"error": response.text})
# members = response.json().get('members', [])
# Simulated members for testing
members = ['U12345678', 'U87654321'] # Remove this line and uncomment above section to fetch actual members
if exclude_channels:
response_message = f"User <@{user_id}> is deactivating all users in the channel except in the excluded channels: {', '.join(exclude_channels)}."
else:
response_message = f"User <@{user_id}> is deactivating all users in the channel."
# Print the text to the console
print(f"Deactivate all command triggered by user <@{user_id}> with exclusions: {text}")
for member in members:
deactivate_payload = {
"user_id": member,
"channel_id": channel_id
}
# Uncomment this section to actually send the request to Slack API
# response = requests.post('https://slack.com/api/admin.users.remove', headers=headers, json=deactivate_payload)
# if not response.ok:
# return jsonify({"error": response.text})
# Return a public message
return jsonify({
"response_type": "in_channel",
"text": response_message,
"delete_original": True
})
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')