Slack Bot with internet of things
This is a project of a slack bot, to automate your office/room/lab using some Raspberry Pi GPIO pins.
Set up your slack workspace
Go to https://slack.com/ . First you'll need to create a new slack workspace to use in this project(or just get the API token from a existing bot in the workspace). Then, go to https://my.slack.com/services/new/bot and choose a username for your bot.
After submit, we now have an API key. Copy this somewhere we'll need later.
Now, on your Pi you'll need to install some dependencies:
sudo apt-get install python3 python3-pip python3-dev python3-rpi.gpio -y
And then some packages:
pip3 install slackclient
pip3 install psutil
And download the repository:
git clone https://github.com/vitor-veras/slackbot_iot.git
Configuring the python file
After downloading slackbot_iot.py file there's some things you'll need to adapt to your bot, like your API token that we generated earlier.
slack_client = SlackClient("your-api-token-here")
In this next step, we fetch our bot User ID. We will use this later to know when somebody is talking to our bot. We keep it in
user_list = slack_client.api_call("users.list") for user in user_list.get('members'): if user.get('name') == "your-bot-name": slack_user_id = user.get('id') break
Now you can personalize your bot with some GPIO pins(any doubts about GPIO pins go to RPI.GPIO Documentation):
from slackclient import SlackClient #GPIO SETUP GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) GPIO.setup(8,GPIO.OUT)
And some methods:
def lightOn(): GPIO.output(8, 1) print ("light on!") def lightOff(): GPIO.output(8, 0) print ("light off!")
And then link the methods you've created to the bot's matching text part.
if re.match(r'.*(light off).*', message_text, re.IGNORECASE): //words you want your bot to recognize lightOff() //the gpio method to execute slack_client.api_call( "chat.postMessage", channel=message['channel'], text="Light off!", //bot response as_user=True)
Now, run it!
Physical gpio setup example
This is an example configuration, of how you can use the GPIO pins to turn on a Led.
Now a picture of the physical setup:
Raspberry Pi + relay board with a Led
Some useful links :)