-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtelegram_bot.py
142 lines (110 loc) · 3.68 KB
/
telegram_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
import re
import logging
import configparser
import cv2
import numpy as np
import telegram
from telegram.ext import Updater
from telegram.ext import MessageHandler, Filters
from telegram.ext import CommandHandler
from emoji import emojize
def clean_message(s):
for i in ["-"]:
s = s.replace(i, f"\{i}")
return s
def get_image():
filename = "opencv_snap.png"
camera = cv2.VideoCapture(0)
return_value, image = camera.read()
cv2.imwrite(filename, image)
return filename
def get_video():
filename = "output.avi"
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(filename, fourcc, 20.0, (640,480))
i = 0
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
out.write(frame)
# 100 -> 5s
# 50 -> 2.5s
if i == 50:
break
else:
break
i += 1
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
return filename
# Main command functions
def test(update, context):
logging.info("test command")
context.bot.send_message(chat_id=update.effective_chat.id, text="It's working")
def data(update, context):
logging.info("data command")
line = None
with open("output.txt", "r") as f:
line = f.readlines()[-1]
s_date, _, s_relay, s_moisture, s_water_level = line.split(";")
msg = clean_message(f"{emojize(':herb:')} {s_date}\n\nRelay: {s_relay}\nMoisture: {s_moisture}\nWater level: {s_water_level}")
context.bot.send_message(
chat_id=update.effective_chat.id,
text=msg,
parse_mode=telegram.ParseMode.MARKDOWN_V2,
)
def smile(update, context):
logging.info("smile command")
filename = get_image()
context.bot.send_photo(
chat_id=update.effective_chat.id,
photo=open(filename, 'rb')
)
def dance(update, context):
logging.info("dance command")
filename = get_video()
context.bot.send_video(
chat_id=update.effective_chat.id,
video=open(filename, 'rb'),
supports_streaming=True
)
def callback_minute(context: telegram.ext.CallbackContext):
logging.info("Checking output")
line = None
with open("output.txt", "r") as f:
line = f.readlines()[-1]
s_date, _, _, s_moisture, s_water_level = line.split(";")
if int(s_moisture) < 415:
context.bot.send_message(chat_id=CHAT_ID,
text=f"Moisture level < 415")
if int(s_water_level) < 6:
context.bot.send_message(chat_id=CHAT_ID,
text=f"Water level < 6")
if __name__ == "__main__":
logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO)
config = configparser.ConfigParser()
config.read("config.ini")
TOKEN = config["DEFAULT"]["Token"]
CHAT_ID = config["DEFAULT"]["ChatID"]
updater = Updater(token=TOKEN, use_context=True)
dispatcher = updater.dispatcher
test_handler = CommandHandler("test", test)
dispatcher.add_handler(test_handler)
# Get last line from the data
data_handler = CommandHandler("data", data)
dispatcher.add_handler(data_handler)
# Get image
smile_handler = CommandHandler("smile", smile)
dispatcher.add_handler(smile_handler)
# Get short video
dance_handler = CommandHandler("dance", dance)
dispatcher.add_handler(dance_handler)
# Monitoring handler every 10 minutes
j = updater.job_queue
job_minute = j.run_repeating(callback_minute, interval=600, first=0)
updater.start_polling()