diff --git a/ssa/slackbot/slackbot.py b/ssa/slackbot/slackbot.py index 3c9de72..045d9ee 100644 --- a/ssa/slackbot/slackbot.py +++ b/ssa/slackbot/slackbot.py @@ -7,7 +7,7 @@ Use slack app integration to post messages via the slack api import configparser import influxdb import time -import datetime +import dateutil.parser import requests PUMP1_FLOW_QUERY = """ @@ -22,7 +22,7 @@ class SlackBot: def __init__(self): self.config = configparser.ConfigParser() self.config.read("slackbot_config.ini") - self.sleep_time_sec = int(self.config.get("update_period_minutes", "30")) * 60 + self.sleep_time_sec = int(self.config["general"]["update_period_minutes"]) * 60 def run(self): while True: @@ -31,37 +31,38 @@ class SlackBot: def post_update(self): # poll latest data - with influxdb.InfluxDBClient( + idb_client = influxdb.InfluxDBClient( self.config["influxdb"].get("host", "localhost"), self.config["influxdb"].get("port", "8086"), self.config["influxdb"].get("username", None), self.config["influxdb"].get("password", None), self.config["influxdb"].get("dbname", "paddle"), - ) as idb_client: - result1 = idb_client.query(PUMP1_FLOW_QUERY) - (pump1, time1) = self.parse_pump_query_result(result1, "Pump1") - result2 = idb_client.query(PUMP2_FLOW_QUERY) - (pump2, time2) = self.parse_pump_query_result(result2, "Pump2") + ) + result1 = idb_client.query(PUMP1_FLOW_QUERY) + (pump1, time1) = self.parse_pump_query_result(result1, "Pump1") + result2 = idb_client.query(PUMP2_FLOW_QUERY) + (pump2, time2) = self.parse_pump_query_result(result2, "Pump2") + idb_client.close() # post to slack channel - dtime1 = datetime.datetime.fromisoformat(time1) - dtime2 = datetime.datetime.fromisoformat(time2) - 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")})" + # parse iso times and convert to local timezone + dtime1 = dateutil.parser.isoparse(time1).astimezone() + dtime2 = dateutil.parser.isoparse(time2).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')})" json_message = { - "channel": self.config["slack"].get("channel_id"), + "channel": self.config["slack"]["channel_id"], "text": message, } - + headers = { - "Authorization": f"Bearer {self.config["slack"].get("token")}", - "Content-type": "application/json" + "Authorization": f"Bearer {self.config['slack']['token']}", + "Content-type": "application/json", } - post_url = self.config["slack"].get("post_url") + post_url = self.config["slack"]["post_url"] requests.post(url=post_url, headers=headers, json=json_message) - def parse_pump_query_result(self, result, name): data = list(result.get_points(name))[0] value = data.get("value") diff --git a/ssa/slackbot/slackbot_config.ini b/ssa/slackbot/slackbot_config.ini index 19ad631..aa45ff2 100644 --- a/ssa/slackbot/slackbot_config.ini +++ b/ssa/slackbot/slackbot_config.ini @@ -1,3 +1,4 @@ +[general] update_period_minutes = 30 [influxdb]