{"id":801,"date":"2017-06-01T14:30:12","date_gmt":"2017-06-01T04:30:12","guid":{"rendered":"http:\/\/roger.venning.net\/wp\/?p=801"},"modified":"2017-06-01T14:30:52","modified_gmt":"2017-06-01T04:30:52","slug":"slackbot-on-raspberry-pi","status":"publish","type":"post","link":"http:\/\/roger.venning.net\/wp\/slackbot-on-raspberry-pi\/","title":{"rendered":"Slackbot on Raspberry Pi"},"content":{"rendered":"<p>As per <a href=\"http:\/\/blog.benjie.me\/building-a-slack-bot-to-talk-with-a-raspberry-pi\/\">Benjie.Me<\/a> and <a href=\"https:\/\/learn.adafruit.com\/neopixels-on-raspberry-pi?view=all\">Neopixels on Raspberry<\/a>\u00a0and also <a href=\"https:\/\/github.com\/jgarff\/rpi_ws281x.git\">JGarff<\/a><\/p>\n<pre>import re\r\nimport time\r\nimport json\r\nfrom slackclient import SlackClient\r\nimport time\r\nfrom neopixel import *\r\n\r\n\r\n# LED strip configuration:\r\nLED_COUNT = 8 # Number of LED pixels.\r\nLED_PIN = 18 # GPIO pin connected to the pixels (must support PWM!).\r\nLED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)\r\nLED_DMA = 5 # DMA channel to use for generating signal (try 5)\r\nLED_BRIGHTNESS = 20 # Set to 0 for darkest and 255 for brightest\r\nLED_INVERT = False # True to invert the signal (when using NPN transistor level shift)\r\n\r\n\r\ndef theaterChase(strip, color, wait_ms=50, iterations=2):\r\n \"\"\"Movie theater light style chaser animation.\"\"\"\r\n for j in range(iterations):\r\n for q in range(3):\r\n for i in range(0, strip.numPixels(), 3):\r\n strip.setPixelColor(i+q, color)\r\n strip.show()\r\n time.sleep(wait_ms\/1000.0)\r\n for i in range(0, strip.numPixels(), 3):\r\n strip.setPixelColor(i+q, 0)\r\n\r\n\r\n\r\n# Create NeoPixel object with appropriate configuration.\r\nstrip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)\r\n# Intialize the library (must be called once before other functions).\r\nstrip.begin()\r\n\r\n\r\n# Slack integration\r\nslack_client = SlackClient(YOUR_API_KEY_HERE)\r\n\r\n\r\n# Fetch your Bot's User ID\r\nuser_list = slack_client.api_call(\"users.list\")\r\nfor user in user_list.get('members'):\r\n if user.get('name') == \"labs-pi\":\r\n slack_user_id = user.get('id')\r\n break\r\n\r\n\r\n# Start connection\r\nif slack_client.rtm_connect():\r\n print \"Connected!\"\r\n\r\n while True:\r\n for message in slack_client.rtm_read():\r\n if 'text' in message and message['text'].startswith(\"&lt;@%s&gt;\" % slack_ user_id):\r\n\r\n print \"Message received: %s\" % json.dumps(message, indent=2)\r\n\r\n message_text = message['text'].\\\r\n split(\"&lt;@%s&gt;\" % slack_user_id)[1].\\\r\n strip()\r\n\r\n if re.match(r'.*light.*on.*', message_text, re.IGNORECASE):\r\n theaterChase(strip, Color(127,127,127))\r\n slack_client.api_call(\r\n \"chat.postMessage\",\r\n channel=message['channel'],\r\n text=\"Lights are now on\",\r\n as_user=True)\r\n\r\n if re.match(r'.*light.*off.*', message_text, re.IGNORECASE):\r\n theaterChase(strip, Color(0,0,0))\r\n slack_client.api_call(\r\n \"chat.postMessage\",\r\n channel=message['channel'],\r\n text=\"Lights are now off\",\r\n as_user=True)\r\n\r\n time.sleep(1)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>As per Benjie.Me and Neopixels on Raspberry\u00a0and 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 &hellip;<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[23],"tags":[],"_links":{"self":[{"href":"http:\/\/roger.venning.net\/wp\/wp-json\/wp\/v2\/posts\/801"}],"collection":[{"href":"http:\/\/roger.venning.net\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/roger.venning.net\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/roger.venning.net\/wp\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/roger.venning.net\/wp\/wp-json\/wp\/v2\/comments?post=801"}],"version-history":[{"count":2,"href":"http:\/\/roger.venning.net\/wp\/wp-json\/wp\/v2\/posts\/801\/revisions"}],"predecessor-version":[{"id":803,"href":"http:\/\/roger.venning.net\/wp\/wp-json\/wp\/v2\/posts\/801\/revisions\/803"}],"wp:attachment":[{"href":"http:\/\/roger.venning.net\/wp\/wp-json\/wp\/v2\/media?parent=801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roger.venning.net\/wp\/wp-json\/wp\/v2\/categories?post=801"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roger.venning.net\/wp\/wp-json\/wp\/v2\/tags?post=801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}