Created
November 2, 2019 00:14
-
-
Save yahelc/e1f6c4485b0872a927c67f2557863b8d to your computer and use it in GitHub Desktop.
A script that monitors changes and deletions to Slackbot messages using undocumented Slack APIs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import datetime | |
import time | |
SLACK_PERSONAL_TOKEN = '' | |
#only needed for deletion restoree | |
WEBSLACK_TOKEN = '' | |
def msg(m): | |
return requests.post("https://rands-leadership.slack.com/api/chat.postMessage", | |
data={ | |
'token': SLACK_PERSONAL_TOKEN, | |
'channel': 'zmeta-bot-response-changes', | |
'text': m, | |
'as_user': 'false', | |
'user_name': 'Slackbot Chhange Monitor' | |
}) | |
def pluralize(items): | |
return 's' if len(items)>1 else '' | |
def get_user_name(user_id): | |
return '<@' + user_id + '>' | |
def format_response(obj): | |
trigger_label = '- ' + str(len(obj['triggers'])) + ' Trigger' + pluralize(obj['triggers']) +': ' + ', '.join(obj['triggers']) | |
editor_label = "\n- Last Known Editor: " + get_user_name(obj.get('editor','')) | |
responses_responses = "\n\n- " + str(len(obj['responses'])) + " Response" + pluralize(obj['responses']) + ": \n" + '\n'.join(obj['responses']) | |
original_creator_label = "\n- Creator: " + get_user_name(obj['creator']) | |
edit_time_label = "\n- Last Known Edit Time (UTC): " + datetime.datetime.utcfromtimestamp(obj.get('edited',0)).strftime('%Y-%m-%dT%H:%M:%SZ') if obj.get('edited',0) > 0 else '' | |
return "%s%s%s%s%s" %( trigger_label, original_creator_label, editor_label, edit_time_label, responses_responses ) | |
def restore_slackbot(obj): | |
r = requests.post('https://rands-leadership.slack.com/api/slackbot.responses.add', | |
params={'_x_id':'38062df5-' + str(time.time()),'slack_route': 'T04T4TH8W', '_x_version_ts':time.time()}, | |
data={'token': WEBSLACK_TOKEN, | |
'triggers': ', '.join(obj['triggers']), | |
'responses': "\n".join(obj['responses']), | |
'set_active': True, | |
'_x_reason': 'legacy_no_reason_provided', | |
'_x_mode': 'online' | |
} | |
) | |
if r.json().get("ok") == True : | |
msg("Succeessfully restored deleted responder.") | |
else: | |
msg("Was unable to successfully resore the deleted responder") | |
#get list of responses | |
new_obj = requests.post("https://rands-leadership.slack.com/api/slackbot.responses.list", data={'token':SLACK_PERSONAL_TOKEN}).json() | |
#delete unnecessary top-level key | |
del new_obj["ok"] | |
live_ids = [] | |
live_id_map = {} | |
print (new_obj) | |
for key in new_obj: | |
live_ids.append(new_obj[key]["id"]) | |
live_id_map[new_obj[key]["id"]] = new_obj[key] | |
f = open("last_json.json", "r") | |
last_obj = json.loads(f.read()) | |
for key in last_obj: | |
old_id = last_obj[key]["id"] | |
if old_id not in live_ids: | |
msg(":siren: Detected a deleted Slackbot Response. Here is the metadata of that now deleted response: ```" + format_response(last_obj[key]) + "```") | |
print("Deleted slackbot response") | |
restore_slackbot(last_obj[key]) | |
elif live_id_map[old_id].get("edited",0) > last_obj[key].get("edited",0): | |
r = msg(":cone: Detected an edited Slackbot Response. Here is the old response: ```" + format_response(last_obj[key]) + "``` Here is the newly edited response: ```" + format_response(live_id_map[old_id] )+ '```' ) | |
f.close() | |
f = open("last_json.json", "w") | |
f.write(json.dumps(new_obj)) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment