Skip to content

Instantly share code, notes, and snippets.

@meub
Created March 6, 2020 19:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save meub/4574ffb127ac7ca92bebef5f9c8450e0 to your computer and use it in GitHub Desktop.
Save meub/4574ffb127ac7ca92bebef5f9c8450e0 to your computer and use it in GitHub Desktop.
Pi Servo Slack Script
#!/usr/bin/env python
# Raspberry PI Bell Ringer
import re
import time
import json
import os
import slack
import RPi.GPIO as GPIO # Importing the GPIO library
from time import sleep # Import sleep module from time library
slack_token = '<SLACK_TOKEN_HERE>'
slack_client = slack.WebClient(token=slack_token)
# Get Slack User ID
user_list = slack_client.api_call("users.list")
for user in user_list.get('members'):
if user.get('name') == "pibot":
slack_user_id = user.get('id')
break
# Set up GPIO
servo_pin = 21 # GPIO Pin where servo is connected
GPIO.setmode(GPIO.BCM)
# Define the Pin numbering type. Here we are using BCM type
# Define Servo Pin as output pin
# GPIO.setup(servo_pin, GPIO.OUT)
# gpio_object = GPIO.PWM(servo_pin, 50) # PWM channel at 50 Hz frequency
def hammer_time():
GPIO.setup(servo_pin, GPIO.OUT)
p = GPIO.PWM(servo_pin, 50)
p.start(0) # Zero duty cycle initially
sleeps = 0.4
sleep(sleeps)
p.ChangeDutyCycle(6)
sleep(sleeps)
p.ChangeDutyCycle(12)
sleep(0.25)
p.ChangeDutyCycle(6)
sleep(sleeps)
@slack.RTMClient.run_on(event='message')
def say_hello(**payload):
data = payload['data']
web_client = payload['web_client']
rtm_client = payload['rtm_client']
message_body = data.get('text', [])
if slack_user_id in message_body:
if "Bell" in message_body or "bell" in message_body:
hammer_time()
channel_id = data['channel']
thread_ts = data['ts']
user = data['user']
web_client.reactions_add(
channel=channel_id,
name="bell",
timestamp=thread_ts
)
rtm_client = slack.RTMClient(token=slack_token)
rtm_client.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment