-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
110 lines (86 loc) · 3.11 KB
/
main.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
import logging
import time
from telegram import (
Bot, Update
)
from telegram.ext import (
Updater, CommandHandler, MessageHandler,
Filters
)
from bot.commands import *
from bot.functions.common import (
ping, user_panel, error
)
from bot.functions.statistics import data, temp_statistic, hum_statistic, co2_statistic, pressure_statistic
from config import TOKEN, IP, PORT
logging.basicConfig(
level=logging.WARNING,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
def manage_all(bot: Bot, update: Update, chat_data: dict):
if update.message.chat.type == 'private':
text = update.message.text
if not text:
return
# Avoid flood
now = time.time()
if ((now - chat_data.get('last_time', 0)) < 1.1) and (chat_data.get('last_text', None) == text) or \
((now - chat_data.get('last_time', 0)) < 0.2):
chat_data['last_text'] = text
chat_data['last_time'] = now
return
chat_data['last_time'] = now
chat_data['last_text'] = text
if text == USER_COMMAND_DATA:
data(bot, update)
elif text == USER_COMMAND_CO2:
co2_statistic(bot, update, hour=1)
elif text == USER_COMMAND_TEMPERATURE:
temp_statistic(bot, update, hour=1)
elif text == USER_COMMAND_HUMIDITY:
hum_statistic(bot, update, hour=1)
elif text == USER_COMMAND_PRESSURE:
pressure_statistic(bot, update, hour=3)
elif text.startswith('/co2'):
arg = int(text.split('_')[1])
if arg:
co2_statistic(bot, update, hour=arg)
elif text.startswith('/temp'):
arg = int(text.split('_')[1])
if arg:
temp_statistic(bot, update, hour=arg)
elif text.startswith('/hum'):
arg = int(text.split('_')[1])
if arg:
hum_statistic(bot, update, hour=arg)
elif text.startswith('/p'):
arg = int(text.split('_')[1])
if arg:
pressure_statistic(bot, update, hour=arg)
else:
user_panel(bot, update)
def main():
# Create the EventHandler and pass it your bot's token.
updater = Updater(TOKEN)
# Get the dispatcher to register handlers
disp = updater.dispatcher
# on different commands - answer in Telegram
disp.add_handler(CommandHandler('start', user_panel))
disp.add_handler(CommandHandler('help', user_panel))
disp.add_handler(CommandHandler('ping', ping))
disp.add_handler(MessageHandler(
Filters.all, manage_all, pass_chat_data=True))
# log all errors
disp.add_error_handler(error)
# Start the Bot
print('Start bot')
updater.start_polling(poll_interval=1)
# updater.start_webhook(listen='0.0.0.0',
# port=PORT,
# url_path=TOKEN,
# key='private.key',
# cert='cert.pem',
# webhook_url='https://%s:%s/%s' % (IP, PORT, TOKEN))
updater.idle()
if __name__ == '__main__':
main()