Skip to content

Instantly share code, notes, and snippets.

@chappy84
Last active February 20, 2020 09:19
Show Gist options
  • Save chappy84/fb8bba58c369675e6e949ad3cc5eb479 to your computer and use it in GitHub Desktop.
Save chappy84/fb8bba58c369675e6e949ad3cc5eb479 to your computer and use it in GitHub Desktop.
Velominati Slack Bot - Names the rule that someone is quoting in slack (Quickly hacked together script)
FROM python:2-alpine3.7
ARG slack_api_token
ENV SLACK_API_TOKEN $slack_api_token
WORKDIR /local/bot
COPY velominati-slack-bot.py ./
RUN apk update && \
apk add build-base libxml2 libxml2-dev libxslt libxslt-dev && \
pip install lxml slackclient && \
apk del build-base libxml2-dev libxslt-dev
CMD python velominati-slack-bot.py
from slackclient import SlackClient
import os
import re
import socket
import time
import websocket
rulesLink = "http://www.velominati.com/the-rules/"
ruleTitles = [
'Obey The Rules',
'Lead by example',
'Guide the uninitiated',
'Its all about the bike',
'Harden The Fuck Up',
'Free your mind and your legs will follow',
'Tan lines should be cultivated and kept razor sharp',
'Saddles, bars, and tires shall be carefully matched.',
'If you are out riding in bad weather, it means you are a badass. Period',
'It never gets easier, you just go faster',
'Family does not come first. The bike does',
'The correct number of bikes to own is n+1',
'If you draw race number 13, turn it upside down',
'Shorts should be black',
'Black shorts should also be worn with leaders jerseys',
'Respect the jersey',
'Team kit is for members of the team',
'Know what to wear. Dont suffer kit confusion',
'Introduce Yourself',
'There are only three remedies for pain.',
'Cold weather gear is for cold weather',
'Cycling caps are for cycling',
'Tuck only after reaching Escape Velocity',
'Speeds and distances shall be referred to and measured in kilometers',
'The bikes on top of your car should be worth more than the car',
'Make your bike photogenic',
'Shorts and socks should be like Goldilocks',
'Socks can be any damn colour you like',
'No European Posterior Man-Satchels',
'No frame-mounted pumps',
'Spare tubes, multi-tools and repair kits should be stored in jersey pockets',
'Humps are for camels: no hydration packs',
'Shave your guns',
'Mountain bike shoes and pedals have their place',
'No visors on the road',
'Eyewear shall be cycling specific',
'The arms of the eyewear shall always be placed over the helmet straps',
'Dont Play Leap Frog',
'Never ride without your eyewear',
'Tires are to be mounted with the label centered over the valve stem',
'Quick-release levers are to be carefully positioned',
'A bike race shall never be preceded with a swim and/or followed by a run',
'Dont be a jackass',
'Position matters',
'Slam your stem',
'Keep your bars level',
'Drink Tripels, dont ride triples',
'Saddles must be level and pushed back',
'Keep the rubber side down',
'Facial hair is to be carefully regulated',
'Livestrong wristbands are cockrings for your arms',
'Drink in Moderation',
'Keep your kit clean and new',
'No aerobars on road bikes',
'Earn your turns',
'Espresso or macchiato only',
'No stickers',
'Support your local bike shop',
'Hold your line',
'Ditch the washer-nut and valve-stem cap',
'Like your guns, saddles should be smooth and hard',
'You shall not ride with earphones',
'Point in the direction youre turning',
'Cornering confidence increases with time and experience',
'Maintain and respect your machine',
'No mirrors',
'Do your time in the wind',
'Rides are to be measured by quality, not quantity',
'Cycling shoes and bicycles are made for riding',
'The purpose of competing is to win',
'Train Properly',
'Legs speak louder than words',
'Gear and brake cables should be cut to optimum length',
'V Meters or small computers only',
'Race numbers are for races',
'Helmets are to be hung from your stem',
'Respect the earth; dont litter',
'Remove unnecessary gear',
'Fight for your town lines',
'Always be Casually Deliberate',
'Dont talk it up',
'Close the gap',
'Be self-sufficient',
'Follow the Code',
'Descend like a Pro',
'Dont half-wheel',
'The Ride Starts on Time. No exceptions',
'Dont surge',
'Pronounce it Correctly',
'Never Get Out of the Big Ring',
'No Food On Training Rides Under Four Hours',
'No Sprinting From the Hoods',
'Descents are not for recovery. Recovery Ales are for Recovery',
'Use the correct tool for the job, and use the tool correctly',
'Never lift your bike over your head'
]
slackToken = os.environ.get("SLACK_API_TOKEN")
sc = SlackClient(slackToken)
ruleMessageMatch = re.compile('[#]?Rule(?:\s*|\s+#)?([0-9]{1,2})', re.I)
if sc.rtm_connect():
while True:
try:
events = sc.rtm_read()
except (socket.error, websocket._exceptions.WebSocketConnectionClosedException) as error:
print('Connection closed, re-connecting...')
if not sc.rtm_connect():
print('Connection failed, something\'s amiss!')
break
events = sc.rtm_read()
for event in events:
if (
'channel' in event and
'text' in event and
event.get('type') == 'message'
):
message = event['text']
if ruleMessageMatch.search(message):
matches = ruleMessageMatch.findall(message)
for match in matches:
ruleNumber = match.lstrip('0')
if len(ruleNumber):
ruleNumber = int(ruleNumber)
if len(ruleTitles) >= ruleNumber:
sc.api_call(
'chat.postMessage',
channel=event['channel'],
text="<%s#%s|#%s>: %s" % (
rulesLink,
ruleNumber,
ruleNumber,
ruleTitles[ruleNumber - 1]
),
as_user='true:',
unfurl_links=False
)
time.sleep(1)
else:
print('Connection failed, invalid token?')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment