-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.py
More file actions
126 lines (101 loc) · 3.19 KB
/
Copy pathstart.py
File metadata and controls
126 lines (101 loc) · 3.19 KB
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
import os
import time
from datetime import datetime as dt
from datetime import timedelta
import telegram
from dotenv import load_dotenv
from fast_bitrix24 import Bitrix
from imap_tools import MailBox
import mail_parsing as parsing
import statistic
from models import Users
load_dotenv()
IMAP = os.getenv('IMAP')
FROM_ = os.getenv('FROM')
TELEGRAM_TOKEN = os.getenv('TELEGRAM_TOKEN')
secret_login = os.getenv('LOGIN')
secret_pass = os.getenv('PASSWORD')
RETRY_TIME = 30
BITRIX24_TOKEN = os.getenv('BITRIX24_TOKEN')
ROLE = {
'all': Users.select(),
'stuff': Users.select().where(Users.role == '1'),
}
def check_env():
"""Проверка заполнения переменных окружения."""
check_list = [
IMAP,
FROM_,
secret_login,
secret_pass,
TELEGRAM_TOKEN,
BITRIX24_TOKEN,
]
for check in check_list:
if not check:
return False
return True
def send_message(bot, message, group):
"""Отправка сообщения ботом."""
users = ROLE[group]
for user in users:
chat_id = user.id_gram
bot.send_message(
chat_id=chat_id,
text=message,
parse_mode='HTML',
)
time.sleep(1)
def get_mail(bot):
"""Проверка почты"""
mailbox = MailBox(IMAP).login(secret_login, secret_pass)
messages = parsing.get_mail(mailbox, FROM_)
mailbox.logout()
if messages:
for message in reversed(messages):
group = 'all'
send_message(bot, message, group)
def get_statistic(day_week, bitrix, bot):
"""Запрос статистики"""
if day_week != '6' and day_week != '0':
call_message = statistic.call(bitrix)
group = 'all'
send_message(bot, call_message, group)
time.sleep(5)
message = statistic.payer()
send_message(bot, message, group)
def main():
"""Основная логика работы бота."""
if not check_env():
error_env = (
'Не заполнены переменные окружения. '
'Работа программы остановлена'
)
raise ValueError(error_env)
bot = telegram.Bot(token=TELEGRAM_TOKEN)
bitrix = Bitrix(BITRIX24_TOKEN)
day_now = dt.now()
send_statistic_time = dt(
day_now.year, day_now.month, day_now.day, 12, 15, 0
)
if send_statistic_time < day_now:
send_statistic_time += timedelta(days=1)
cache_message = ''
while True:
try:
day_now = dt.now()
day_week = day_now.strftime('%w')
get_mail(bot)
if send_statistic_time < day_now:
send_statistic_time += timedelta(days=1)
get_statistic(day_week, bitrix, bot)
time.sleep(RETRY_TIME)
except Exception as error:
message = f'<b>Сбой в работе программы:</b> {error}'
if message != cache_message:
group = 'stuff'
send_message(bot, message, group)
cache_message = message
time.sleep(RETRY_TIME)
if __name__ == '__main__':
main()