-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarivanna.py
280 lines (238 loc) · 11.4 KB
/
marivanna.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
# Copyright Ivan Chernoknizhnikov t.me/treamz
# https://github.com/Treamz/
import telebot
from telebot import types
import pymysql
import time
from time import sleep
API_TOKEN = 'YOUR API KEY' # TG BOT KEY
daysweek = ['/monday - Понедельник','/tuesday - Вторник','/wednesday - Среда','/thursday - Четверг', '/friday - Пятница',]
del_daysweek = ['/del_monday - Понедельник','/del_tuesday - Вторник','/del_wednesday - Среда','/del_thursday - Четверг', '/del_friday - Пятница',]
# Handle 'start' and 'help'
pares_dict = {}
class Pares:
def __init__(self, day):
self.day = day
self.fpare = None
self.secpare = None
self.thirdpare = None
self.fourthpare = None
self.fifthpare = None
self.sixthpare = None
self.seventhpare = None
self.eighthpare = None
def nullcheack():
if pares.fpare == "0":
pares.fpare = None
return pares.fpare
# Handle delete sch
@bot.message_handler(commands=['delete'])
def delete(message):
msg = bot.send_message(message.chat.id, 'Какой день вы хотите удалить? \n' + ('\n'.join(del_daysweek)))
bot.register_next_step_handler(msg, process_first_step_delete)
def process_first_step_delete(message):
chat_id = message.chat.id
conn = pymysql.connect(host='localhost', port=3306, user='mysql',passwd='mysql', db='mysql', use_unicode=True, charset="utf8")
cur = conn.cursor(pymysql.cursors.DictCursor)
chatidd = str(message.chat.id)
if message.text =="/del_monday":
dday = "mysql.monday_info"
if message.text =="/del_tuesday":
dday = "mysql.tuesday_info"
if message.text =="/del_wednesday":
dday = "mysql.wednesday_info"
if message.text =="/del_thursday":
dday = "mysql.thursday_info"
if message.text =="/del_friday":
dday = "mysql.friday_info"
isql = "DELETE FROM {} WHERE chatidd={}".format(dday, chatidd)
cur.execute(isql)
conn.commit()
# Handle 'start' and 'help'
@bot.message_handler(commands=['start'])
def start(message):
bot.send_message(message.chat.id, 'Привет,надо расписание?')
print(str(message.chat.id))
@bot.message_handler(commands=['sch'])
def addsch(message):
info = 'Пожалуйста,выберите день:''\n' + ('\n'.join(daysweek))
print(info)
bot.send_message(message.chat.id, info)
# Handle add day
@bot.message_handler(commands=['addmonday', 'addtuesday', 'addwednesday', 'addthursday', 'addfriday'])
def add_tuesday(message):
chat_id = message.chat.id
if message.text == "/addmonday":
day = "mysql.monday_info"
if message.text == "/addtuesday":
day = "mysql.tuesday_info"
if message.text == "/addwednesday":
day = "mysql.wednesday_info"
if message.text == "/addthursday":
day = "mysql.thursday_info"
if message.text == "/addfriday":
day = "mysql.friday_info"
pares = Pares(day)
pares_dict[chat_id] = pares
msg = bot.reply_to(message, 'Какая у тебя первая пара? Если ее нет пиши 0')
bot.register_next_step_handler(msg, process_first_step)
def process_first_step(message):
try:
chat_id = message.chat.id
fpare = message.text
pares = pares_dict[chat_id]
pares.fpare = fpare
msg = bot.reply_to(message, 'А вторая? Если ее нет пиши 0')
bot.register_next_step_handler(msg, process_second_step)
except Exception as e:
bot.reply_to(message, 'oooops')
def process_second_step(message):
try:
chat_id = message.chat.id
secpare = message.text
pares = pares_dict[chat_id]
pares.secpare = secpare
msg = bot.reply_to(message, 'А Третья? Если ее нет пиши 0')
bot.register_next_step_handler(msg, process_third_step)
except Exception as e:
bot.reply_to(message, 'oooops')
def process_third_step(message):
try:
chat_id = message.chat.id
thirdpare = message.text
pares = pares_dict[chat_id]
pares.thirdpare = thirdpare
msg = bot.reply_to(message, 'А Четвертая? Если ее нет пиши 0')
bot.register_next_step_handler(msg, process_fourth_step)
except Exception as e:
bot.reply_to(message, 'oooops')
def process_fourth_step(message):
try:
chat_id = message.chat.id
fourthpare = message.text
pares = pares_dict[chat_id]
pares.fourthpare = fourthpare
msg = bot.reply_to(message, 'А Пятая? Если ее нет пиши 0')
bot.register_next_step_handler(msg, process_fifth_step)
except Exception as e:
bot.reply_to(message, 'oooops')
def process_fifth_step(message):
try:
chat_id = message.chat.id
fifthpare = message.text
pares = pares_dict[chat_id]
pares.fifthpare = fifthpare
msg = bot.reply_to(message, 'А Шестая? Если ее нет пиши 0')
bot.register_next_step_handler(msg, process_sixth_step)
except Exception as e:
bot.reply_to(message, 'oooops')
def process_sixth_step(message):
try:
chat_id = message.chat.id
sixthpare = message.text
pares = pares_dict[chat_id]
pares.sixthpare = sixthpare
msg = bot.reply_to(message, 'А седьмая? Если ее нет пиши 0')
bot.register_next_step_handler(msg, process_seventh_step)
except Exception as e:
bot.reply_to(message, 'oooops')
def process_seventh_step(message):
try:
chat_id = message.chat.id
seventhpare = message.text
pares = pares_dict[chat_id]
pares.seventhpare = seventhpare
msg = bot.reply_to(message, 'А Восьмая? Если ее нет пиши 0')
bot.register_next_step_handler(msg, process_eighth_step)
except Exception as e:
bot.reply_to(message, 'oooops')
def process_eighth_step(message):
try:
chat_id = message.chat.id
eighthpare = message.text
pares = pares_dict[chat_id]
pares.eighthpare = eighthpare
TUESDAY = '\n' + pares.fpare + '\n' + pares.secpare + '\n' + pares.thirdpare + '\n' + pares.fourthpare + '\n' + pares.fifthpare + '\n' + pares.sixthpare + '\n' + pares.seventhpare + '\n' + pares.eighthpare
info = 'Сохранить пары?'
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
markup.add('Да', 'Нет')
msg = bot.send_message(chat_id, TUESDAY + '\n' + info, reply_markup=markup)
bot.register_next_step_handler(msg, process_ninth_step)
except Exception as e:
bot.reply_to(message, 'oooops')
def process_ninth_step(message):
try:
chat_id = message.chat.id
pares = pares_dict[chat_id]
TUESDAY = '\n' + pares.fpare + '\n' + pares.secpare + '\n' + pares.thirdpare + '\n' + pares.fourthpare + '\n' + pares.fifthpare + '\n' + pares.sixthpare + '\n' + pares.seventhpare + '\n' + pares.eighthpare
if message.text == "Да":
conn = pymysql.connect(host='localhost', port=3306, user='mysql',passwd='mysql', db='mysql', use_unicode=True, charset="utf8")
cur = conn.cursor(pymysql.cursors.DictCursor)
chatidd = str(message.chat.id)
#isql = "INSERT INTO mysql.tuesday_info (first, second, third, fourth, fifth, sixth, seventh, eighth, chatidd) VALUES ('{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}');".format(pares.fpare, pares.secpare, pares.thirdpare, pares.fourthpare, pares.fifthpare, pares.sixthpare, pares.seventhpare, pares.eighthpare, chatidd)
isql = "INSERT INTO {} (first, second, third, fourth, fifth, sixth, seventh, eighth, chatidd) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s);".format(pares.day)
if pares.fpare == "0":
pares.fpare = "Пары нет"
if pares.secpare == "0":
pares.secpare = "Пары нет"
if pares.thirdpare == "0":
pares.thirdpare = "Пары нет"
if pares.fourthpare == "0":
pares.fourthpare = "Пары нет"
if pares.fifthpare == "0":
pares.fifthpare = "Пары нет"
if pares.sixthpare == "0":
pares.sixthpare = "Пары нет"
if pares.seventhpare == "0":
pares.seventhpare = "Пары нет"
if pares.eighthpare == "0":
pares.eighthpare = "Пары нет"
print(pares.fpare)
cur.execute(isql, (pares.fpare, pares.secpare, pares.thirdpare, pares.fourthpare, pares.fifthpare, pares.sixthpare, pares.seventhpare, pares.eighthpare, chatidd))
conn.commit()
bot.send_message(message.chat.id, 'Пары сохранены')
else:
bot.send_message(chat_id, 'Пары не сохранены')
except Exception as e:
bot.reply_to(message, 'oooop23s')
@bot.message_handler(commands=['monday', 'tuesday', 'wednesday', 'thursday', 'friday'])
def monday_info(message):
try:
conn = pymysql.connect(host='localhost', port=3306, user='mysql',passwd='mysql', db='mysql', use_unicode=True, charset="utf8")
cur = conn.cursor(pymysql.cursors.DictCursor)
chatidd = str(message.chat.id)
day = None
if message.text == "/monday":
day = "mysql.monday_info"
if message.text == "/tuesday":
day = "mysql.tuesday_info"
if message.text == "/wednesday":
day = "mysql.wednesday_info"
if message.text == "/thursday":
day = "mysql.thursday_info"
if message.text == "/friday":
day = "mysql.friday_info"
print(day)
ssql = "SELECT first, second, third, fourth, fifth, sixth, seventh, eighth, chatidd FROM {} WHERE chatidd='{}'".format(day, chatidd)
cur.execute(ssql)
for row in cur.fetchall():
try_sch_mon = ('<b>Первая:</b> ' + row['first'],'<b>Вторая:</b> ' + row['second'],'<b>Третья:</b> ' + row['third'],'<b>Четвертая:</b> ' + row['fourth'],'<b>Пятая:</b> ' + row['fifth'],'<b>Шестая:</b> ' + row['sixth'],'<b>Седьмая:</b> ' + row['seventh'],'<b>Восьмая:</b> ' + row['eighth'])
print('***********************************')
#print('\n Первая:' + row['first'],'\n Вторая: ' + row['second'],'\n Третья: ' + row['third'],'\n Четвертая: ' + row['fourth'],'\n Пятая: ' + row['fifth'],'\n Шестая: ' + row['sixth'],'\n Седьмая: ' + row['seventh'],'\n Восьмая: ' + row['eighth'])
print('***********************************')
for item in try_sch_mon:
if not item.find("Пары") != -1:
print(item)
bot.send_message(message.chat.id, item, parse_mode="HTML")
#try_sch_mon = ('\n'.join(try_sch_mon))
#bot.send_message(message.chat.id, try_sch_mon, parse_mode="HTML")
except:
bot.send_message(message.chat.id, 'Сорри,чет не получилось')
while True:
try:
bot.polling(none_stop=True)
except Exception as e:
print('***********************************')
print(e)
print('***********************************')
sleep(15)