Commit 960be52eaf717900580a587deea4cef943208c06

Authored by Greg Sandstrom
1 parent 535edd78a8
Exists in main

tested slackbot, ready to deploy

Showing 2 changed files with 19 additions and 17 deletions Inline Diff

ssa/slackbot/slackbot.py View file @ 960be52
""" 1 1 """
slackbot.py 2 2 slackbot.py
3 3
Use slack app integration to post messages via the slack api 4 4 Use slack app integration to post messages via the slack api
""" 5 5 """
6 6
import configparser 7 7 import configparser
import influxdb 8 8 import influxdb
import time 9 9 import time
import datetime 10 10 import dateutil.parser
import requests 11 11 import requests
12 12
PUMP1_FLOW_QUERY = """ 13 13 PUMP1_FLOW_QUERY = """
SELECT last(value) as value From Pump1 14 14 SELECT last(value) as value From Pump1
""" 15 15 """
PUMP2_FLOW_QUERY = """ 16 16 PUMP2_FLOW_QUERY = """
SELECT last(value) as value From Pump2 17 17 SELECT last(value) as value From Pump2
""" 18 18 """
19 19
20 20
class SlackBot: 21 21 class SlackBot:
def __init__(self): 22 22 def __init__(self):
self.config = configparser.ConfigParser() 23 23 self.config = configparser.ConfigParser()
self.config.read("slackbot_config.ini") 24 24 self.config.read("slackbot_config.ini")
self.sleep_time_sec = int(self.config.get("update_period_minutes", "30")) * 60 25 25 self.sleep_time_sec = int(self.config["general"]["update_period_minutes"]) * 60
26 26
def run(self): 27 27 def run(self):
while True: 28 28 while True:
self.post_update() 29 29 self.post_update()
time.sleep(self.sleep_time_sec) 30 30 time.sleep(self.sleep_time_sec)
31 31
def post_update(self): 32 32 def post_update(self):
# poll latest data 33 33 # poll latest data
with influxdb.InfluxDBClient( 34 34 idb_client = influxdb.InfluxDBClient(
self.config["influxdb"].get("host", "localhost"), 35 35 self.config["influxdb"].get("host", "localhost"),
self.config["influxdb"].get("port", "8086"), 36 36 self.config["influxdb"].get("port", "8086"),
self.config["influxdb"].get("username", None), 37 37 self.config["influxdb"].get("username", None),
self.config["influxdb"].get("password", None), 38 38 self.config["influxdb"].get("password", None),
self.config["influxdb"].get("dbname", "paddle"), 39 39 self.config["influxdb"].get("dbname", "paddle"),
) as idb_client: 40 40 )
result1 = idb_client.query(PUMP1_FLOW_QUERY) 41 41 result1 = idb_client.query(PUMP1_FLOW_QUERY)
(pump1, time1) = self.parse_pump_query_result(result1, "Pump1") 42 42 (pump1, time1) = self.parse_pump_query_result(result1, "Pump1")
result2 = idb_client.query(PUMP2_FLOW_QUERY) 43 43 result2 = idb_client.query(PUMP2_FLOW_QUERY)
(pump2, time2) = self.parse_pump_query_result(result2, "Pump2") 44 44 (pump2, time2) = self.parse_pump_query_result(result2, "Pump2")
45 idb_client.close()
45 46
# post to slack channel 46 47 # post to slack channel
dtime1 = datetime.datetime.fromisoformat(time1) 47 48 # parse iso times and convert to local timezone
dtime2 = datetime.datetime.fromisoformat(time2) 48 49 dtime1 = dateutil.parser.isoparse(time1).astimezone()
message = f"Pump1: {pump1:4.1f} ({dtime1.strftime("%d %b %Y, %I:%M%p")})\nPump2: {pump2:4.1f} ({dtime2.strftime("%d %b %Y, %I:%M%p")})" 49 50 dtime2 = dateutil.parser.isoparse(time2).astimezone()
51 message = f"Pump1: {pump1:4.1f} ({dtime1.strftime('%d %b %Y, %I:%M%p')})\nPump2: {pump2:4.1f} ({dtime2.strftime('%d %b %Y, %I:%M%p')})"
50 52
json_message = { 51 53 json_message = {
"channel": self.config["slack"].get("channel_id"), 52 54 "channel": self.config["slack"]["channel_id"],
"text": message, 53 55 "text": message,
} 54 56 }
55 57
headers = { 56 58 headers = {
"Authorization": f"Bearer {self.config["slack"].get("token")}", 57 59 "Authorization": f"Bearer {self.config['slack']['token']}",
"Content-type": "application/json" 58 60 "Content-type": "application/json",
} 59 61 }
post_url = self.config["slack"].get("post_url") 60 62 post_url = self.config["slack"]["post_url"]
61 63
requests.post(url=post_url, headers=headers, json=json_message) 62 64 requests.post(url=post_url, headers=headers, json=json_message)
63
64 65
def parse_pump_query_result(self, result, name): 65 66 def parse_pump_query_result(self, result, name):
data = list(result.get_points(name))[0] 66 67 data = list(result.get_points(name))[0]
value = data.get("value") 67 68 value = data.get("value")
time = data.get("time") 68 69 time = data.get("time")
return (value, time) 69 70 return (value, time)
70 71
71 72
if __name__ == "__main__": 72 73 if __name__ == "__main__":
slackBot = SlackBot() 73 74 slackBot = SlackBot()
slackBot.run() 74 75 slackBot.run()
75 76
ssa/slackbot/slackbot_config.ini View file @ 960be52
1 [general]
update_period_minutes = 30 1 2 update_period_minutes = 30
2 3
[influxdb] 3 4 [influxdb]
host = localhost 4 5 host = localhost
port = 8086 5 6 port = 8086
dbname = change-me 6 7 dbname = change-me
7 8
[slack] 8 9 [slack]
post_url = https://slack.com/api/chat.postMessage 9 10 post_url = https://slack.com/api/chat.postMessage