Skip to content

Instantly share code, notes, and snippets.

@jquast
Created August 6, 2021 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jquast/ed02eef837c1182612d95b0b5927531d to your computer and use it in GitHub Desktop.
Save jquast/ed02eef837c1182612d95b0b5927531d to your computer and use it in GitHub Desktop.
import os
import time
import slack
import random
client = slack.WebClient(token=os.environ['SLACK_BOT_TOKEN'])
#response = client.chat_postMessage(
# channel=' #adam-test-channel',
# text="Hello world!")
#assert response["ok"]
#assert response["message"]["text"] == "Hello world!"
response = client.users_list()
assert response["ok"]
dpath = os.path.expanduser('~/.ripbot.data.next')
deleted_announced = []
new_announced = []
if os.path.exists(dpath):
deleted_announced = list(
line.split()[1]
for line in open(dpath, 'r').readlines()
if line.split()[0] == 'd'
)
new_announced = list(
line.split()[1]
for line in open(dpath, 'r').readlines()
if line.split()[0] == 'n'
)
dry_run = False
for expirey, deleted, uid, real_name, in sorted([
(_user['updated'], _user['deleted'], _user['id'], _user['profile']['real_name'])
for _user in response['members']
]):
if deleted:
# goodbyte
if uid not in deleted_announced:
txt_expirey = time.ctime(expirey)
text = f'{real_name} dearly departed us on {txt_expirey}.'
print(text)
if not dry_run:
response = client.chat_postMessage(channel='#in-loving-memory', text=text)
assert response["ok"]
deleted_announced.append(uid + ' ' + real_name)
time.sleep(1)
else:
# freshen entry
deleted_announced.remove(uid)
deleted_announced.append(uid + ' ' + real_name)
else:
# new user?
if uid not in new_announced:
txt_hello = time.ctime(expirey)
join_message = random.choice(
['joined the slacktavist party',
'first praised bob on',
'joined the church of the sub-genius at',
'joined us in slackemony',
'joined us on this year of our slack',
])
text = f'{real_name} {join_message} {txt_hello}.'
print(text)
if not dry_run:
response = client.chat_postMessage(channel='#it-change_notice', text=text)
assert response["ok"]
new_announced.append(uid + ' ' + real_name)
time.sleep(1)
else:
# freshen entry
new_announced.remove(uid)
new_announced.append(uid + ' ' + real_name)
if not dry_run:
with open(dpath, 'w') as fout:
fout.write('\n'.join(['d ' + announced for announced in deleted_announced]))
fout.write('\n')
fout.write('\n'.join(['n ' + announced for announced in new_announced]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment