-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
115 lines (85 loc) · 3.53 KB
/
bot.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
# coding: utf-8
import logging
import os
from telegram.ext import CommandHandler, MessageHandler, Updater
import config
import database
import humino
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# create a file handler
handler = logging.FileHandler('humino_bot.log')
handler.setLevel(logging.INFO)
# create a logging format
log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
formatter = logging.Formatter(log_format)
handler.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(handler)
def start(bot, update):
logger.info("Linking to {}".format(update.message.chat_id))
update.message.reply_text("Hello.")
def error(bot, update, error):
logger.warn('Update "%s" caused error "%s"' % (update, error))
def measure(bot, update):
logger.info("Sending measurements")
raw = database.read_data()
data = humino.raw_to_hum(raw)
status = humino.status_message(data)
update.message.reply_text(status)
def graph(bot, update):
logger.info("Sending latest graph")
try:
with open(os.path.join(config.OUT_FOLDER, "plot.png"), "rb") as f:
bot.send_photo(chat_id=update.message.chat_id, photo=f)
except Exception as e:
logger.error(e)
update.message.reply_text("Error reading graph file: {}".format(e))
def enable_notifications(job_queue, chat_id):
job_queue.run_repeating(notify_about_dry_plants,
interval=config.STEP * 60, first=0, context=chat_id)
logging.info("Notifications enabled for chat {}".format(chat_id))
def toggle_notifications(bot, update, job_queue):
if (len(job_queue.jobs()) == 0):
enable_notifications(job_queue, update.message.chat_id)
bot.send_message(
chat_id=update.message.chat_id, text='Notifications enabled')
else:
jobs = job_queue.jobs()
jobs[0].schedule_removal()
logging.info("Notifications will be disabled after next job run")
bot.send_message(
chat_id=update.message.chat_id, text='Notifications disabled')
def notify_about_dry_plants(bot, job):
logging.info('Running notifications job')
raw = database.read_data(days=1)
data = humino.raw_to_hum(raw)
for plant_id in data.columns:
if len(data[plant_id]) < 2 or data[plant_id][-1] is None or data[plant_id][-2] is None:
logging.warning('Not enough or missing data for {}'.format(
config.PLANTS[plant_id][0]))
else:
dry_now = data[plant_id][-1] < config.PLANTS[plant_id][1]
dry_before = data[plant_id][-2] < config.PLANTS[plant_id][1]
if dry_now and not dry_before:
text = "💧 {} is thirsty now ({}%).".format(
config.PLANTS[plant_id][0], int(data[plant_id][-1]))
bot.send_message(chat_id=job.context, text=text)
logging.info("{} is dry".format(config.PLANTS[plant_id][0]))
def run():
logger.info("Starting bot")
updater = Updater(config.TELEGRAM_API_TOKEN)
dp = updater.dispatcher
dp.add_handler(CommandHandler('start', start))
dp.add_handler(CommandHandler('measure', measure))
dp.add_handler(CommandHandler('graph', graph))
dp.add_handler(CommandHandler('notify', toggle_notifications,
pass_job_queue=True))
if config.CHAT_ID is not None:
enable_notifications(updater.job_queue, config.CHAT_ID)
dp.add_error_handler(error)
updater.start_polling()
return updater
if __name__ == '__main__':
updater = run()
updater.idle()