def edit_note(message): """Edit command handler""" sqlighter = db_worker.SQLighter(os.getenv('DB_PATH')) chat_id = message.chat.id data = message.text.split('\n')[1:] if len(data) == 4: note = sqlighter.get('id', data[0]) if note: if utils.get_time_obj(data[3]): for i in range(len(utils.note_fields)): sqlighter.update(utils.note_fields[i], data[i + 1], 'id', data[0]) bot.send_message( chat_id, f'Your note with new header \"<strong>{data[1]}</strong>\" has been updated', parse_mode='HTML') else: bot.send_message( chat_id, '<strong>Error!</strong> Enter the <i>datetime</i> field correctly', parse_mode='HTML') else: bot.send_message( chat_id, f'Note with ID <i>{data[0]}</i> doesn\'t exists. Please check it', parse_mode='HTML') else: bot.send_message( chat_id, 'Please, write you command correctly. Type /templates for help') sqlighter.close()
def add_note(message): """Add command handler""" sqlighter = db_worker.SQLighter(os.getenv('DB_PATH')) chat_id = message.chat.id data = message.text.split('\n')[1:] timestamp = time.time() # get current time if len(data) == 3: note = sqlighter.get('id', timestamp) if not note: if utils.get_time_obj(data[2]): sqlighter.add( [chat_id, data[0], data[1], 0, data[2], timestamp]) bot.send_message( chat_id, f'Your note with the header \"{data[0]}\" has been added') else: bot.send_message( chat_id, '<strong>Error!</strong> Enter the <i>datetime</i> field correctly', parse_mode='HTML') else: bot.send_message(chat_id, f'Note with the header \"{data[0]}\" exists') else: bot.send_message( chat_id, 'Please, write you message correctly. Type /templates for help') sqlighter.close()
class Parent: """Parent class with need variables""" path = './database.db' token = os.getenv('TELEGRAM_TOKEN') url = f'https://api.telegram.org/bot{token}/' ok_status_code = 200 bot_name = 'Telegram Keep' chat_id = os.getenv('CHAT_ID') test_text = 'test' SQLighter = db_worker.SQLighter(path) timestamp = time.time()
def statistics_command(message): """Statistics command handler""" sqlighter = db_worker.SQLighter(os.getenv('DB_PATH')) chat_id = message.chat.id notes = sqlighter.get('chat_id', chat_id) ready_num = unready_num = 0 for note in notes: if note[3]: ready_num += 1 else: unready_num += 1 data = { 'all_num': len(notes), 'unready_num': unready_num, 'ready_num': ready_num } text = utils.statistics_template(data) bot.reply_to(message, 'Your statistics of all time:') bot.send_message(chat_id, text, parse_mode='HTML')
def get_notes(message): """Get command handler""" sqlighter = db_worker.SQLighter(os.getenv('DB_PATH')) bot.reply_to(message, 'Your notes:') chat_id = message.chat.id data = sqlighter.get('chat_id', chat_id) for note in data: msg = utils.note_template(note) # message template # Markup for note markup = types.InlineKeyboardMarkup(row_width=3) mark_item = types.InlineKeyboardButton( f'Mark as \"{utils.status_codes[note[3]].get("reverse_str")}\"', callback_data= f'mark{note[5]}{utils.status_codes[note[3]].get("int")}') edit_mark = types.InlineKeyboardButton('Edit', callback_data=f'edit{note[5]}') delete_mark = types.InlineKeyboardButton( 'Delete', callback_data=f'delete{note[5]}') markup.add(mark_item, edit_mark, delete_mark) bot.send_message(chat_id, msg, parse_mode='HTML', reply_markup=markup) sqlighter.close()
def test_conn(self): """Test connection to database""" self.assertIsNotNone(db_worker.SQLighter(self.path))
def callback_inline(call): """Callbacks handler""" sqlighter = db_worker.SQLighter(os.getenv('DB_PATH')) parameter = 'id' edit_str = 'edit' delete_str = 'delete' mark_str = 'mark' try: if call.message: # Edit button pressed if call.data.startswith(edit_str): bot.send_message(call.message.chat.id, 'Edit this note with ID and /edit command') bot.edit_message_text( chat_id=call.message.chat.id, message_id=call.message.message_id, text= f'Note <strong>ID</strong>: <i>{call.data[len(edit_str):]}</i>', reply_markup=None, parse_mode='HTML') # Delete button pressed elif call.data.startswith(delete_str): sqlighter.delete(parameter, call.data[len(delete_str):]) bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text='Deleted', reply_markup=None) # Mark button pressed elif call.data.startswith(mark_str): value_to_set = call.data[-1] # get value from call.data # get note id from call.data note_id = call.data[len(mark_str):len(call.data) - 1] sqlighter.update('status', value_to_set, 'id', note_id) note = sqlighter.get('id', note_id)[0] # get updated note if value_to_set == '1': # Mark as "ready" due_time = utils.get_time_obj(note[4]) now_timestamp = datetime.now().timestamp() due_time_timestamp = datetime.timestamp(due_time) if now_timestamp - due_time_timestamp > 0: # Show bad alert bot.answer_callback_query( callback_query_id=call.id, show_alert=True, text= 'You did not have time to complete the task. Try better next time\N{grinning face}' ) else: # Show good alert bot.answer_callback_query( callback_query_id=call.id, show_alert=True, text= 'Hooray! You managed to complete the task on time. Do not slow down\N{flexed biceps}' ) bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=utils.note_template(note), parse_mode='HTML') except Exception as error: print(f'Error: {error}') sqlighter.close()