-
Notifications
You must be signed in to change notification settings - Fork 3
/
telegram_houseplant_bot.py
467 lines (383 loc) · 16.5 KB
/
telegram_houseplant_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import pprint
from enum import IntEnum
from telegram import ForceReply, Update, ReplyKeyboardMarkup, ReplyKeyboardRemove
from telegram.ext import (
Application,
CommandHandler,
ContextTypes,
ConversationHandler,
MessageHandler,
filters
)
from helpers import clients,logging
from classes.mapping import Mapping
from classes.houseplant import Houseplant
logger = logging.set_logging('telegram_houseplant_bot')
config = clients.config()
PLANT_STATE = IntEnum('PlantState', [
'ID',
'GIVEN',
'SCIENTIFIC',
'COMMON',
'TEMP_LOW',
'TEMP_HIGH',
'MOISTURE_LOW',
'MOISTURE_HIGH',
'COMMIT_PLANT'
])
MAPPING_STATE = IntEnum('MappingState', [
'SENSOR_ID',
'PLANT_ID',
'COMMIT_MAPPING'
])
####################################################################################
# #
# UPDATE PLANT HANDLERS #
# #
####################################################################################
async def update_plant_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if config['telegram']['chat-id'] == update.message.chat_id:
context.user_data['plant'] = {}
await update.message.reply_text(
"Enter the plant id of the plant you'd like to update."
)
return PLANT_STATE.ID
else:
await update.message.reply_text(
"You are not authorized to use this application."
)
return ConversationHandler.END
async def id_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if config['telegram']['chat-id'] == update.message.chat_id:
context.user_data['plant']['plant_id'] = int(update.message.text)
await update.message.reply_text(
"Please enter plant's given name."
)
return PLANT_STATE.GIVEN
else:
await update.message.reply_text(
"You are not authorized to use this application."
)
return ConversationHandler.END
async def given_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if config['telegram']['chat-id'] == update.message.chat_id:
context.user_data['plant']['given_name'] = update.message.text
await update.message.reply_text(
"Please enter plant's scientific name."
)
return PLANT_STATE.SCIENTIFIC
else:
await update.message.reply_text(
"You are not authorized to use this application."
)
return ConversationHandler.END
async def scientific_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if config['telegram']['chat-id'] == update.message.chat_id:
context.user_data['plant']['scientific_name'] = update.message.text
await update.message.reply_text(
"Please enter plant's common name."
)
return PLANT_STATE.COMMON
else:
await update.message.reply_text(
"You are not authorized to use this application."
)
return ConversationHandler.END
async def common_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if config['telegram']['chat-id'] == update.message.chat_id:
context.user_data['plant']['common_name'] = update.message.text
await update.message.reply_text(
"Please enter plant's low temperature threshold in C or /skip to use the default."
)
return PLANT_STATE.TEMP_LOW
else:
await update.message.reply_text(
"You are not authorized to use this application."
)
return ConversationHandler.END
async def temp_low_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if config['telegram']['chat-id'] == update.message.chat_id:
# capture and store low temp data
temp_low = update.message.text
if temp_low != '/skip':
# update state
context.user_data['plant']['temperature_low'] = int(temp_low)
else:
context.user_data['plant']['temperature_low'] = 40
await update.message.reply_text(
"Please enter plant's high temperature threshold in C or /skip to use the default."
)
return PLANT_STATE.TEMP_HIGH
else:
await update.message.reply_text(
"You are not authorized to use this application."
)
return ConversationHandler.END
async def temp_high_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if config['telegram']['chat-id'] == update.message.chat_id:
# capture and store high temp data
temp_high = update.message.text
if temp_high != '/skip':
# update state
context.user_data['plant']['temperature_high'] = int(temp_high)
else:
context.user_data['plant']['temperature_high'] = 100
await update.message.reply_text(
"Please enter plant's low moisture threshold or /skip to use the default."
)
return PLANT_STATE.MOISTURE_LOW
else:
await update.message.reply_text(
"You are not authorized to use this application."
)
return ConversationHandler.END
async def moisture_low_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if config['telegram']['chat-id'] == update.message.chat_id:
# capture and store high temp data
moisture_low = update.message.text
if moisture_low != '/skip':
# update state
context.user_data['plant']['moisture_low'] = int(moisture_low)
else:
context.user_data['plant']['moisture_low'] = 25
await update.message.reply_text(
"Please enter plant's high moisture threshold or /skip to use the default."
)
return PLANT_STATE.MOISTURE_HIGH
else:
await update.message.reply_text(
"You are not authorized to use this application."
)
return ConversationHandler.END
async def moisture_high_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if config['telegram']['chat-id'] == update.message.chat_id:
# capture and store high temp data
moisture_high = update.message.text
if moisture_high != '/skip':
# update state
context.user_data['plant']['moisture_high'] = int(moisture_high)
else:
context.user_data['plant']['moisture_high'] = 65
# build up summary for confirmation
summary = pprint.pformat(context.user_data['plant'])
await update.message.reply_text(
"Please confirm your metadata entry.\n\n"
f"{summary}"
"\nIs this correct? Reply /y to commmit the metadata entry or use /cancel to start over.",
)
return PLANT_STATE.COMMIT_PLANT
else:
await update.message.reply_text(
"You are not authorized to use this application."
)
return ConversationHandler.END
async def commit_plant_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if config['telegram']['chat-id'] == update.message.chat_id:
try:
send_metadata(context.user_data['plant'])
context.user_data['plant'].clear()
await update.message.reply_text(
"You've confirmed your metadata entry, and it has been updated. "
)
except Exception as e:
await update.message.reply_text(
"Your metadata was not sent; please use /update_plant to re-try."
)
return ConversationHandler.END
else:
await update.message.reply_text(
"You are not authorized to use this application."
)
return ConversationHandler.END
def send_metadata(metadata):
# send metadata message
try:
# set up Kafka producer for houseplant metadata
producer = clients.producer(clients.houseplant_serializer())
value = Houseplant.dict_to_houseplant(metadata)
k = str(metadata.get('plant_id'))
logger.info("Publishing metadata message for key %s", k)
producer.produce(config['topics']['houseplants'], key=k, value=value)
except Exception as e:
logger.error("Got exception %s", e)
raise e
finally:
producer.poll()
producer.flush()
####################################################################################
# #
# UPDATE MAPPING HANDLERS #
# #
####################################################################################
async def update_mapping_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if config['telegram']['chat-id'] == update.message.chat_id:
context.user_data['mapping'] = {}
reply_keyboard = [
["0x36"],
["0x37"],
["0x38"],
["0x39"]
]
await update.message.reply_text(
"Select the sensor ID for the mapping you'd like to update.",
reply_markup=ReplyKeyboardMarkup(
reply_keyboard, one_time_keyboard=True, input_field_placeholder="Sensor ID"
)
)
return MAPPING_STATE.SENSOR_ID
else:
await update.message.reply_text(
"You are not authorized to use this application."
)
return ConversationHandler.END
async def sensor_id_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if config['telegram']['chat-id'] == update.message.chat_id:
context.user_data['mapping']['sensor_id'] = update.message.text
await update.message.reply_text(
f"Please enter the plant id you'd like to map to {update.message.text}.",
reply_markup=ReplyKeyboardRemove()
)
return MAPPING_STATE.PLANT_ID
else:
await update.message.reply_text(
"You are not authorized to use this application."
)
return ConversationHandler.END
async def plant_id_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if config['telegram']['chat-id'] == update.message.chat_id:
context.user_data['mapping']['plant_id'] = int(update.message.text)
# build up summary for confirmation
summary = pprint.pformat(context.user_data['mapping'])
await update.message.reply_text(
"Please confirm your mapping entry.\n\n"
f"{summary}"
"\nIs this correct? Reply /y to commmit the mapping entry or use /cancel to start over.",
)
return MAPPING_STATE.COMMIT_MAPPING
else:
await update.message.reply_text(
"You are not authorized to use this application."
)
return ConversationHandler.END
async def commit_mapping_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if config['telegram']['chat-id'] == update.message.chat_id:
try:
send_mapping(context.user_data['mapping'])
context.user_data['mapping'].clear()
await update.message.reply_text(
"You've confirmed your mapping entry, and it has been updated. "
)
except Exception as e:
await update.message.reply_text(
"Your mapping entry was not sent; please use /update_mapping to re-try."
)
return ConversationHandler.END
else:
await update.message.reply_text(
"You are not authorized to use this application."
)
return ConversationHandler.END
def send_mapping(mapping):
# send mapping message
try:
# set up Kafka producer for mappings
producer = clients.producer(clients.mapping_serializer())
value = Mapping.dict_to_mapping(mapping)
k = str(mapping.get('sensor_id'))
logger.info("Publishing mapping message for key %s", k)
producer.produce(config['topics']['mappings'], key=k, value=value)
except Exception as e:
logger.error("Got exception %s", e)
raise e
finally:
producer.poll()
producer.flush()
####################################################################################
# #
# OTHER HANDLERS #
# #
####################################################################################
async def cancel_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Cancels and ends the conversation."""
context.user_data.clear()
await update.message.reply_text(
"Cancelled metadata update."
)
return ConversationHandler.END
async def post_init(application: Application) -> None:
await application.bot.set_my_commands([
('update_plant', "Update plant metadata"),
('update_mapping', "Update sensor-plant mapping")
])
####################################################################################
def main() -> None:
# create the application and pass in bot token
application = Application.builder().token(config['telegram']['api-token']).post_init(post_init).build()
# define conversation handlers
update_plant_handler = ConversationHandler(
entry_points=[CommandHandler('update_plant', update_plant_command)],
states={
PLANT_STATE.ID: [
MessageHandler(filters.TEXT & ~filters.COMMAND, id_command),
CommandHandler('cancel', cancel_command)
],
PLANT_STATE.GIVEN: [
MessageHandler(filters.TEXT & ~filters.COMMAND, given_command),
CommandHandler('cancel', cancel_command)
],
PLANT_STATE.SCIENTIFIC: [
MessageHandler(filters.TEXT & ~filters.COMMAND, scientific_command),
CommandHandler('cancel', cancel_command)
],
PLANT_STATE.COMMON: [
MessageHandler(filters.TEXT & ~filters.COMMAND, common_command),
CommandHandler('cancel', cancel_command)
],
PLANT_STATE.TEMP_LOW: [
MessageHandler(filters.TEXT & (~filters.COMMAND | filters.Regex("^\/skip$")), temp_low_command),
CommandHandler('cancel', cancel_command)
],
PLANT_STATE.TEMP_HIGH: [
MessageHandler(filters.TEXT & (~filters.COMMAND | filters.Regex("^\/skip$")), temp_high_command),
CommandHandler('cancel', cancel_command)
],
PLANT_STATE.MOISTURE_LOW: [
MessageHandler(filters.TEXT & (~filters.COMMAND | filters.Regex("^\/skip$")), moisture_low_command),
CommandHandler('cancel', cancel_command)
],
PLANT_STATE.MOISTURE_HIGH: [
MessageHandler(filters.TEXT & (~filters.COMMAND | filters.Regex("^\/skip$")), moisture_high_command),
CommandHandler('cancel', cancel_command)
],
PLANT_STATE.COMMIT_PLANT: [
CommandHandler('y', commit_plant_command),
CommandHandler('n', cancel_command)
]
},
fallbacks=[CommandHandler('cancel', cancel_command)]
)
update_mapping_handler = ConversationHandler(
entry_points=[CommandHandler('update_mapping', update_mapping_command)],
states={
MAPPING_STATE.SENSOR_ID: [
MessageHandler(filters.TEXT & ~filters.COMMAND, sensor_id_command),
CommandHandler('cancel', cancel_command)
],
MAPPING_STATE.PLANT_ID: [
MessageHandler(filters.TEXT & ~filters.COMMAND, plant_id_command),
CommandHandler('cancel', cancel_command)
],
MAPPING_STATE.COMMIT_MAPPING: [
CommandHandler('y', commit_mapping_command),
CommandHandler('n', cancel_command)
]
},
fallbacks=[CommandHandler("cancel", cancel_command)]
)
# add handlers
application.add_handler(update_plant_handler)
application.add_handler(update_mapping_handler)
# run the bot application
application.run_polling()
if __name__ == '__main__':
main()