1
0

3 Commits
v1.0 ... v1.1

2 changed files with 58 additions and 13 deletions

65
bot.py
View File

@@ -10,8 +10,12 @@ import json
import time
import telepot
from telepot.loop import MessageLoop
from fuzzywuzzy import process
from fuzzywuzzy import fuzz
from Task import Task
VERSION = "v1.1"
PROPERTY_LAST_COMMAND = "last_command"
PROPERTY_LAST_ARGUMENTS = "last_arguments"
@@ -82,6 +86,14 @@ def process_command(command, arguments, chat_id):
delete_all_tasks(chat_id)
elif command == '/priority':
priority(chat_id, arguments)
elif command == '/fdo':
fuzzy_action(chat_id, ' '.join(arguments), do_tasks)
elif command == '/fundo':
fuzzy_action(chat_id, ' '.join(arguments), undo_tasks)
elif command == '/frm':
fuzzy_action(chat_id, ' '.join(arguments), rm_tasks)
elif command == '/fpriority':
fuzzy_priority(chat_id, arguments)
else:
set_property(PROPERTY_LAST_COMMAND, '/add', chat_id)
set_property(PROPERTY_LAST_ARGUMENTS, arguments, chat_id)
@@ -103,17 +115,6 @@ def add_task(task, chat_id):
BOT.sendMessage(chat_id, "Added task: {0}".format(task))
def rm_task(task, chat_id):
"""
Deletes a task
:param task: A Task object
:param chat_id: A numerical telegram chat_id
"""
tasks = get_tasks(chat_id)
set_tasks([x for x in tasks if str(task) != str(x)], chat_id)
BOT.sendMessage(chat_id, "Removed task: {0}".format(task))
def rm_tasks(task_ids, chat_id):
"""
Delete multiple tasks
@@ -124,7 +125,8 @@ def rm_tasks(task_ids, chat_id):
for i in task_ids:
if not is_task_id_valid(chat_id, i):
continue
rm_task(tasks[int(i)], chat_id)
set_tasks([x for x in tasks if str(tasks[int(i)]) != str(x)], chat_id)
BOT.sendMessage(chat_id, "Removed task: {0}".format(tasks[int(i)]))
def get_property(property_name, chat_id):
@@ -316,6 +318,7 @@ def do_tasks(task_ids, chat_id):
for i in task_ids:
if not is_task_id_valid(chat_id, i):
continue
task = get_task(int(i), chat_id)
task.do()
set_task(int(i), task, chat_id)
@@ -383,7 +386,7 @@ def user_help_info(chat_id):
"""
with open('help.md') as help_file:
text = help_file.read()
text += "\ntodo.txt bot for Telegram version 1"
text += "\ntodo.txt bot for Telegram version {0}".format(VERSION)
text += "\n[View help on GitHub](alvierahman90.github.io/todo.txt_telegram/help.html)"
BOT.sendMessage(chat_id, text, parse_mode='Markdown')
@@ -468,6 +471,42 @@ def is_task_id_valid(chat_id, task_id):
return real_task_id
return fail()
def fuzzy_get_task_id_from_text(chat_id, text):
"""
Fuzzy searches for the closest match to the text string given
:param chat_id: Telegram chat_id
:param text: The string to fuzzy match
:return: task_id, matchness, task_text as a tuple
"""
tasks = [str(x) for x in get_tasks(chat_id)]
task_text, matchness = process.extractOne(text, tasks,
scorer=fuzz.token_sort_ratio)
task_id = tasks.index(task_text)
return task_id, matchness
def fuzzy_action(chat_id, text, function):
"""
Marks the task most similar to `text` as done
:param chat_id: Telegram chat_id
:param text: text to match to a task to perform function on it
:param function: the function with which to process the task_id
"""
task_id, matchness = fuzzy_get_task_id_from_text(chat_id, text)
return function([task_id], chat_id)
def fuzzy_priority(chat_id, arguments):
"""
Sets the priority of the closest matching task to text
:param chat_id: Telegram chat_id
:param text: text to match to a task to perform function on it
:param function: the function with which to process the task_id
"""
text = ' '.join(arguments[1:])
task_id, matchness = fuzzy_get_task_id_from_text(chat_id, text)
return priority(chat_id, [arguments[0], task_id])
if __name__ == "__main__":
MessageLoop(BOT, on_message).run_as_thread()

View File

@@ -9,6 +9,12 @@ Anything sent without a command is assumed to be a new task to be added
- `/rm <id> [id [id [id]...]]` - Remove task(s)
- `/undo <id> [id [id [id]...]]` - undo task(s)
### fuzzy actions
- `/fdo <text to match>`
- `/fpriority <text to match>`
- `/frm <text to match>`
- `/fundo <text to match>`
## general
- `/export` - Send all tasks as plaintext
- `/help` - Show help information