Slackbot on Raspberry Pi
As per Benjie.Me and Neopixels on Raspberry and also JGarff
import re
import time
import json
from slackclient import SlackClient
import time
from neopixel import *
# LED strip configuration:
LED_COUNT = 8 # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (must support PWM!).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 5 # DMA channel to use for generating signal (try 5)
LED_BRIGHTNESS = 20 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
def theaterChase(strip, color, wait_ms=50, iterations=2):
"""Movie theater light style chaser animation."""
for j in range(iterations):
for q in range(3):
for i in range(0, strip.numPixels(), 3):
strip.setPixelColor(i+q, color)
strip.show()
time.sleep(wait_ms/1000.0)
for i in range(0, strip.numPixels(), 3):
strip.setPixelColor(i+q, 0)
# Create NeoPixel object with appropriate configuration.
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
# Intialize the library (must be called once before other functions).
strip.begin()
# Slack integration
slack_client = SlackClient(YOUR_API_KEY_HERE)
# Fetch your Bot's User ID
user_list = slack_client.api_call("users.list")
for user in user_list.get('members'):
if user.get('name') == "labs-pi":
slack_user_id = user.get('id')
break
# Start connection
if slack_client.rtm_connect():
print "Connected!"
while True:
for message in slack_client.rtm_read():
if 'text' in message and message['text'].startswith("<@%s>" % slack_ user_id):
print "Message received: %s" % json.dumps(message, indent=2)
message_text = message['text'].\
split("<@%s>" % slack_user_id)[1].\
strip()
if re.match(r'.*light.*on.*', message_text, re.IGNORECASE):
theaterChase(strip, Color(127,127,127))
slack_client.api_call(
"chat.postMessage",
channel=message['channel'],
text="Lights are now on",
as_user=True)
if re.match(r'.*light.*off.*', message_text, re.IGNORECASE):
theaterChase(strip, Color(0,0,0))
slack_client.api_call(
"chat.postMessage",
channel=message['channel'],
text="Lights are now off",
as_user=True)
time.sleep(1)