Skip to content

Instantly share code, notes, and snippets.

@libfun
Created July 20, 2017 20:31
Show Gist options
  • Save libfun/55bfb5a7795ac15aa89c136cded1ad82 to your computer and use it in GitHub Desktop.
Save libfun/55bfb5a7795ac15aa89c136cded1ad82 to your computer and use it in GitHub Desktop.
Slack bot that converts pdf links to abs and posts it in thread.
import os
import time
from slackclient import SlackClient
import re
slack_client = SlackClient(apikey)
def proc_out(msg):
try:
if msg['type'] == 'message':
text = msg['text'].lower()
insts = [m.start() for m in re.finditer('arxiv.org/pdf/', text)]
chan = msg['channel']
if 'thread_ts' in msg:
ts = msg['thread_ts']
else:
ts = msg['ts']
links = []
for inst in insts:
lk = text[inst + 14:inst + 24]
if text.find('arxiv.org/abs/' + str(lk)) == -1:
links.append('https://arxiv.org/abs/' + str(lk))
links = list(set(links))
if links != []:
response = 'Please, stop using links to pdfs.\n'
response += 'Here are your links to abstracts:\n'
for link in links[:-1]:
response += link + '\n'
response += links[-1]
slack_client.api_call("chat.postMessage", channel=chan,
text=response, as_user=True, thread_ts=ts)
except Exception as e:
print(e)
if __name__ == "__main__":
READ_WEBSOCKET_DELAY = 1 # 1 second delay between reading from firehose
if slack_client.rtm_connect():
print("StarterBot connected and running!")
while True:
res = slack_client.rtm_read()
if res != []:
for r in res:
proc_out(r)
time.sleep(READ_WEBSOCKET_DELAY)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment