-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathslackbot_iot.py
123 lines (111 loc) · 4.2 KB
/
slackbot_iot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import re
import json
import psutil
import RPi.GPIO as GPIO
import time
from slackclient import SlackClient
#GPIO SETUP(you can change for whatever you want)
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
#gpio pins that i used, feel free to change
GPIO.setup(8,GPIO.OUT)
GPIO.setup(16,GPIO.OUT)
GPIO.setup(22,GPIO.OUT)
#PUT YOUR SLACK API
slack_client = SlackClient("your-slack-api-here")
# Fetch your Bot's User ID
#PUT YOUR BOT'S NAME
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
#functions to automate when message is called
#personalize
def lightOn():
GPIO.output(8, 1)
print ("light on!")
def lightOff():
GPIO.output(8, 0)
print ("light off!")
def fanOn():
GPIO.output(16, 1)
print ("fan on!")
def fanOff():
GPIO.output(16, 0)
print ("fan off!")
def alarmOn():
GPIO.output(22, 1)
print ("alarm on!")
def alarmOff():
GPIO.output(22, 0)
print ("alarm off!")
# Start connection
if slack_client.rtm_connect():
print ("Connected!")
#matching messages
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'.*(cpu).*', message_text, re.IGNORECASE):
cpu_pct = psutil.cpu_percent(interval=1, percpu=False)
slack_client.api_call(
"chat.postMessage",
channel=message['channel'],
text="My CPU is at %s%%." % cpu_pct,
as_user=True)
if re.match(r'.*(memory|ram).*', message_text, re.IGNORECASE):
mem = psutil.virtual_memory()
mem_pct = mem.percent
slack_client.api_call(
"chat.postMessage",
channel=message['channel'],
text="My RAM is at %s%%." % mem_pct,
as_user=True)
if re.match(r'.*(light on).*', message_text, re.IGNORECASE):
lightOn()
slack_client.api_call(
"chat.postMessage",
channel=message['channel'],
text="Light on!",
as_user=True)
if re.match(r'.*(light off).*', message_text, re.IGNORECASE):
lightOff()
slack_client.api_call(
"chat.postMessage",
channel=message['channel'],
text="Light off!",
as_user=True)
if re.match(r'.*(fan on).*', message_text, re.IGNORECASE):
fanOn()
slack_client.api_call(
"chat.postMessage",
channel=message['channel'],
text="Fan on!",
as_user=True)
if re.match(r'.*(fan off).*', message_text, re.IGNORECASE):
fanOff()
slack_client.api_call(
"chat.postMessage",
channel=message['channel'],
text="Fan off!",
as_user=True)
if re.match(r'.*(alarm on).*', message_text, re.IGNORECASE):
alarmOn()
slack_client.api_call(
"chat.postMessage",
channel=message['channel'],
text="Alarm on!",
as_user=True)
if re.match(r'.*(alarm off).*', message_text, re.IGNORECASE):
alarmOff()
slack_client.api_call(
"chat.postMessage",
channel=message['channel'],
text="Alarm off!",
as_user=True)
time.sleep(1)