-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmybot.py
More file actions
159 lines (144 loc) · 5.57 KB
/
mybot.py
File metadata and controls
159 lines (144 loc) · 5.57 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
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
"""
bot for parsing tables
"""
import os
import telebot
from prettytable import PrettyTable, from_html_one
TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN")
BOT = telebot.TeleBot(TOKEN, parse_mode=None)
@BOT.message_handler(commands=['start', 'help'])
def send_welcome(message):
"""
info about commands
"""
BOT.reply_to(message,
'Мои команды: \n /table_text - выведет таблицу строкой по параметрам, '
'введенным через пробел: '
'количество столбцов, количество строк, названия столбцов, поэлементно строки '
'\n /table_html - принимает те же параметры, что первая, но выдает '
'html версию \n'
'/table_json - принимает те же параметры, что первая, но выдает '
'json версию \n'
' \n /to_text - переводит таблицу html, поданную текстом, '
'в обычный строчный вариант ')
@BOT.message_handler(commands=['table_text'], content_types=['text'])
def send_table(message):
"""
making table string
"""
table = PrettyTable()
list_words = message.text.split()
if len(list_words) < 3:
BOT.send_message(message.chat.id, 'Не надо так, неправильно введены параметры')
return
number_of_fields = int(list_words[1])
if number_of_fields == 0:
BOT.send_message(message.chat.id, 'Не надо так, неправильно введены параметры')
return
row = []
fields = []
for i in range(3, len(list_words)):
if i < 3 + number_of_fields:
fields.append(str(list_words[i]))
else:
if i == 3 + number_of_fields:
table.field_names = fields
if len(row) == number_of_fields:
table.add_row(row)
elem = str(list_words[i])
if list_words[i].isdigit():
elem = int(list_words[i])
row = [elem]
else:
elem = str(list_words[i])
if list_words[i].isdigit():
elem = int(list_words[i])
row.append(elem)
table.add_row(row)
string_s = table.get_string()
BOT.send_message(message.chat.id,
string_s)
@BOT.message_handler(commands=['table_html'], content_types=['text'])
def send_html_table(message):
"""
making html-table
"""
table = PrettyTable()
list_words = message.text.split()
if len(list_words) < 3:
BOT.send_message(message.chat.id, 'Не надо так, неправильно введены параметры')
return
number_of_fields = int(list_words[1])
if number_of_fields == 0:
BOT.send_message(message.chat.id, 'Не надо так, неправильно введены параметры')
return
row = []
fields = []
for i in range(3, len(list_words)):
if i < 3 + number_of_fields:
fields.append(str(list_words[i]))
else:
if i == 3 + number_of_fields:
table.field_names = fields
if len(row) == number_of_fields:
table.add_row(row)
elem = str(list_words[i])
if list_words[i].isdigit():
elem = int(list_words[i])
row = [elem]
else:
elem = str(list_words[i])
if list_words[i].isdigit():
elem = int(list_words[i])
row.append(elem)
table.add_row(row)
string_s = table.get_html_string()
BOT.send_message(message.chat.id,
string_s)
@BOT.message_handler(commands=['to_text'], content_types=['text'])
def send_table_from_html(message):
"""
making string-table from html
"""
list_words = message.text[8:]
string_s = from_html_one(list_words).get_string()
BOT.send_message(message.chat.id,
string_s)
@BOT.message_handler(commands=['table_json'], content_types=['text'])
def send_json_table(message):
"""
making json-table
"""
table = PrettyTable()
list_words = message.text.split()
if len(list_words) < 3:
BOT.send_message(message.chat.id, 'Не надо так, неправильно введены параметры')
return
number_of_fields = int(list_words[1])
if number_of_fields == 0:
BOT.send_message(message.chat.id, 'Не надо так, неправильно введены параметры')
return
row = []
fields = []
for i in range(3, len(list_words)):
if i < 3 + number_of_fields:
fields.append(str(list_words[i]))
else:
if i == 3 + number_of_fields:
table.field_names = fields
if len(row) == number_of_fields:
table.add_row(row)
elem = str(list_words[i])
if list_words[i].isdigit():
elem = int(list_words[i])
row = [elem]
else:
elem = str(list_words[i])
if list_words[i].isdigit():
elem = int(list_words[i])
row.append(elem)
table.add_row(row)
string_s = table.get_json_string()
BOT.send_message(message.chat.id,
string_s)
BOT.infinity_polling()