Commit 535edd78a8ab6236cf532cca0103ecadbf6d30f7
1 parent
35f86fafbf
Exists in
main
unify slackbot names, add service template
Showing 4 changed files with 102 additions and 75 deletions Inline Diff
ssa/slack-bot/slackbot.py
View file @
535edd7
""" | 1 | File was deleted | ||
slackbot.py | 2 | |||
3 | ||||
Use slack app integration to post messages via the slack api | 4 | |||
""" | 5 | |||
6 | ||||
import configparser | 7 | |||
import influxdb | 8 | |||
import time | 9 | |||
import datetime | 10 | |||
import requests | 11 | |||
12 | ||||
PUMP1_FLOW_QUERY = """ | 13 | |||
SELECT last(value) as value From Pump1 | 14 | |||
""" | 15 | |||
PUMP2_FLOW_QUERY = """ | 16 | |||
SELECT last(value) as value From Pump2 | 17 | |||
""" | 18 | |||
19 | ||||
20 | ||||
class SlackBot: | 21 | |||
def __init__(self): | 22 | |||
self.config = configparser.ConfigParser() | 23 | |||
self.config.read("slackbot_config.ini") | 24 | |||
self.last_dtime1 = 0 | 25 | |||
self.last_dtime2 = 0 | 26 | |||
27 | ||||
def run(self): | 28 | |||
while True: | 29 | |||
self.post_update() | 30 | |||
time.sleep(self.config.get("sleep_time_sec")) | 31 | |||
32 | ||||
def post_update(self): | 33 | |||
# poll latest data | 34 | |||
with influxdb.InfluxDBClient( | 35 | |||
self.config["influxdb"].get("host", "localhost"), | 36 | |||
self.config["influxdb"].get("port", "8086"), | 37 | |||
self.config["influxdb"].get("username", None), | 38 | |||
self.config["influxdb"].get("password", None), | 39 | |||
self.config["influxdb"].get("dbname", "paddle"), | 40 | |||
) as idb_client: | 41 | |||
result1 = idb_client.query(PUMP1_FLOW_QUERY) | 42 | |||
(pump1, time1) = self.parse_pump_query_result(result1, "Pump1") | 43 | |||
result2 = idb_client.query(PUMP2_FLOW_QUERY) | 44 | |||
(pump2, time2) = self.parse_pump_query_result(result2, "Pump2") | 45 | |||
46 | ||||
# post to slack channel | 47 | |||
dtime1 = datetime.datetime.fromisoformat(time1) | 48 | |||
dtime2 = datetime.datetime.fromisoformat(time2) | 49 | |||
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 | |||
51 | ||||
json_message = { | 52 | |||
"channel": self.config["slack"].get("channel_id"), | 53 | |||
"text": message, | 54 | |||
} | 55 | |||
56 | ||||
headers = { | 57 | |||
"Authorization": f"Bearer {self.config["slack"].get("token")}", | 58 | |||
"Content-type": "application/json" | 59 | |||
} | 60 |
ssa/slackbot/slackbot.py
View file @
535edd7
File was created | 1 | """ | ||
2 | slackbot.py | |||
3 | ||||
4 | Use slack app integration to post messages via the slack api | |||
5 | """ | |||
6 | ||||
7 | import configparser | |||
8 | import influxdb | |||
9 | import time | |||
10 | import datetime | |||
11 | import requests | |||
12 | ||||
13 | PUMP1_FLOW_QUERY = """ | |||
14 | SELECT last(value) as value From Pump1 | |||
15 | """ | |||
16 | PUMP2_FLOW_QUERY = """ | |||
17 | SELECT last(value) as value From Pump2 | |||
18 | """ | |||
19 | ||||
20 | ||||
21 | class SlackBot: | |||
22 | def __init__(self): | |||
23 | self.config = configparser.ConfigParser() | |||
24 | self.config.read("slackbot_config.ini") | |||
25 | self.sleep_time_sec = int(self.config.get("update_period_minutes", "30")) * 60 | |||
26 | ||||
27 | def run(self): | |||
28 | while True: | |||
29 | self.post_update() | |||
30 | time.sleep(self.sleep_time_sec) | |||
31 | ||||
32 | def post_update(self): | |||
33 | # poll latest data | |||
34 | with influxdb.InfluxDBClient( | |||
35 | self.config["influxdb"].get("host", "localhost"), | |||
36 | self.config["influxdb"].get("port", "8086"), | |||
37 | self.config["influxdb"].get("username", None), | |||
38 | self.config["influxdb"].get("password", None), | |||
39 | self.config["influxdb"].get("dbname", "paddle"), | |||
40 | ) as idb_client: | |||
41 | result1 = idb_client.query(PUMP1_FLOW_QUERY) | |||
42 | (pump1, time1) = self.parse_pump_query_result(result1, "Pump1") | |||
43 | result2 = idb_client.query(PUMP2_FLOW_QUERY) | |||
44 | (pump2, time2) = self.parse_pump_query_result(result2, "Pump2") | |||
45 | ||||
46 | # post to slack channel | |||
47 | dtime1 = datetime.datetime.fromisoformat(time1) | |||
48 | dtime2 = datetime.datetime.fromisoformat(time2) | |||
49 | 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 | ||||
51 | json_message = { | |||
52 | "channel": self.config["slack"].get("channel_id"), | |||
53 | "text": message, | |||
54 | } | |||
55 | ||||
56 | headers = { | |||
57 | "Authorization": f"Bearer {self.config["slack"].get("token")}", | |||
58 | "Content-type": "application/json" | |||
59 | } |
ssa/slackbot/slackbot.service
View file @
535edd7
File was created | 1 | [Unit] | ||
2 | Description=Slackbot service to post updates via SOARS-Bot app | |||
3 | Requires=time-sync.target | |||
4 | ||||
5 | [Service] | |||
6 | Type=simple | |||
7 | User=pi | |||
8 | PIDFile=/path/to/repo/slackbot.pid | |||
9 | ExecStart=python -m ssa.slackbot.slackbot | |||
10 | WorkingDirectory=/path/to/repo/ | |||
11 | Restart=always | |||
12 | RestartSec=30 | |||
13 | StandardOutput=journal | |||
14 | StandardError=journal | |||
15 |
ssa/slackbot/slackbot_config.ini
View file @
535edd7
File was created | 1 | update_period_minutes = 30 | ||
2 | ||||
3 | [influxdb] | |||
4 | host = localhost | |||
5 | port = 8086 | |||
6 | dbname = change-me | |||
7 | ||||
8 | [slack] | |||
9 | post_url = https://slack.com/api/chat.postMessage |