-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
69 lines (51 loc) · 1.93 KB
/
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
import os
import pickle
import telebot as tb
from telebot.types import Message
import config
import db
import exceptions as ex
bot = tb.TeleBot(config.TOKEN)
@bot.message_handler(commands=['start', 'help'])
def start(message: Message):
pass
@bot.message_handler(commands=['new_task'])
def new_task(message: Message):
parsed = message.text.split(maxsplit=1)
if len(parsed) == 1:
bot.send_message(message.chat.id, 'Write a new task...')
bot.register_next_step_handler(message, _adding_task_next)
elif len(parsed) == 2:
db.insert_task(parsed[1])
bot.send_message(message.chat.id,
'Great, your task is written!')
else:
raise ex.ParseError()
def _adding_task_next(message: Message):
db.insert_task(message.text)
bot.send_message(message.chat.id,
'Great, your task is written!')
@bot.message_handler(commands=['list'])
def list_(message: Message):
if not db.TASK_LIST:
bot.send_message(message.chat.id, 'Your list is empty :(')
else:
msg = 'Your task list:'
for i, task in enumerate(db.TASK_LIST):
msg += f'\n{i+1}. {task}'
bot.send_message(message.chat.id, msg)
@bot.message_handler(content_types=['text'])
def handle_message(message: Message):
if 'привет' in (text := message.text):
bot.reply_to(message, text[::-1])
return
text = f"I've got message from user #{message.from_user.id}:\n{text}\nProcessing..."
bot.send_message(message.chat.id, text)
def _handle_project_name(message: Message):
projectname = message.text
bot.send_message(message.chat.id, f'New project "{projectname}" was added')
if __name__ == '__main__':
if 'list' in os.listdir(path='.'):
with open('list', 'rb') as tasklist:
db.TASK_LIST = pickle.load(tasklist)
bot.polling(timeout=40)