/
DrWinter
/
BoSinnBot
Обзор
Документация
Войти
/
DrWinter
/
BoSinnBot
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
main.py
135 строк
4 KB
DrWinter
Key
08 окт 2024, 23:59
08 окт 2024, 23:59
8d87a5c
Код
Авторство
О чём код?
#imports from aiogram import F import re from aiogram.filters import Command from aiogram.fsm.context import FSMContext from aiogram.fsm.state import StatesGroup, State from aiogram.types import Message, ReplyKeyboardRemove import asyncio import logging from aiogram import Bot, Dispatcher, types from aiogram.filters.command import Command from aiogram.utils.keyboard import InlineKeyboardBuilder #logging on logging.basicConfig(level=logging.INFO) #introduce class tasks(StatesGroup): Choosing_Task_Name = State() API_TOKEN = '' bot = Bot(token=API_TOKEN) dp = Dispatcher() #functions #read list from file def reader(filename="tasks.txt"): f = open(filename, 'r') tasks_list = [] for i in f: if i != "\n": if i not in tasks_list: tasks_list.append(i.replace('\n', '')) f.close() return tasks_list #write list to file def writer(filename="tasks.txt", tasks_list=[]): f = open(filename, 'w') for i in tasks_list: if (i != "\n"): f.write(i+ '\n') f.close() #delete emoji from user text def deEmojify(text): regrex_pattern = re.compile(pattern = "[" u"\U00000000-\U00000009" u"\U0000000B-\U0000001F" u"\U00000080-\U00000400" u"\U00000402-\U0000040F" u"\U00000450-\U00000450" u"\U00000452-\U0010FFFF" "]+", flags = re.UNICODE) return regrex_pattern.sub(r'',text) #Dispatcher #start command @dp.message(Command("start")) async def Welcome_Memessage(message:types.Message): #create buttons kb = [ [types.KeyboardButton(text="Add task")], [types.KeyboardButton(text="Show Tasks")], [types.KeyboardButton(text="Start Remind")], [types.KeyboardButton(text="Stop Remind")] ] input_field_placeholder = "What do you want to do?" keyboard = types.ReplyKeyboardMarkup( keyboard = kb, resize_keyboard=True ) await message.answer("Hello, I'm real Bo Sinn and i could remember and remind all tasks.", reply_markup=keyboard) #polling bot async def main(): await dp.start_polling(bot) #addtask button @dp.message(F.text.lower() == "add task") async def ad_tsk(message: Message, state: FSMContext): await message.answer(text="Write your task") await state.set_state(tasks.Choosing_Task_Name) #show task button @dp.message(F.text.lower() == "show tasks") async def shw_tsk(message: types.Message): tasks_list = reader("tasks.txt") if tasks_list !=[]: builder = InlineKeyboardBuilder() for i in tasks_list: builder.row(types.InlineKeyboardButton( text=i, callback_data=i, width = 1), ) await message.answer( "Click on task to remove them", reply_markup=builder.as_markup() ) else: await message.answer("You have no tasks today, congratulations.") #inline button remove @dp.callback_query(F.data) async def remover(callback: types.CallbackQuery): print(F.data) tasks_list = reader("tasks.txt") tasks_list.remove(F.data) print(F.data) print(tasks_list) writer("tasks.txt", tasks_list) #add task with FSM @dp.message(tasks.Choosing_Task_Name) async def task_added(message: Message, state: FSMContext): tasks_list = reader("tasks.txt") if deEmojify(message.text) not in tasks_list: if deEmojify(message.text) != '': tasks_list.append(deEmojify(message.text)) await message.answer(text="Done") else: await message.answer(text="Sorry i cant read this text") else: await message.answer(text="It's already in your ToDo list") writer("tasks.txt", tasks_list) print(tasks_list) tasks_list = [] await state.clear() #rerun main if __name__ == "__main__": asyncio.run(main())