aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorromangraef <roman.graef@gmail.com>2018-03-17 22:11:47 +0100
committerromangraef <roman.graef@gmail.com>2018-03-17 22:11:47 +0100
commit50afa23b9dc26f914522a55fe5cbb893d34ad27c (patch)
tree9711c076e4af283523f51fbd72ceb67bde4f5366 /modules
parent473d84ec2c82932fa1c09e4b96d6e8198bbb71df (diff)
downloadtelegramuserbot-50afa23b9dc26f914522a55fe5cbb893d34ad27c.tar.gz
telegramuserbot-50afa23b9dc26f914522a55fe5cbb893d34ad27c.tar.bz2
telegramuserbot-50afa23b9dc26f914522a55fe5cbb893d34ad27c.zip
added todos as well as regex based command triggers.HEADmaster
Diffstat (limited to 'modules')
-rw-r--r--modules/todo/todo.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/modules/todo/todo.py b/modules/todo/todo.py
new file mode 100644
index 0000000..89a2b78
--- /dev/null
+++ b/modules/todo/todo.py
@@ -0,0 +1,52 @@
+import sqlite3
+
+import lib
+
+
+def init_todo_db(conn: sqlite3.Connection):
+ c = conn.cursor()
+ c.execute(
+ """
+ CREATE TABLE IF NOT EXISTS todos (
+ id INTEGER PRIMARY KEY,
+ text VARCHAR(1024)
+ );
+ """)
+ conn.commit()
+
+
+def add_todo(todo):
+ conn = sqlite3.connect('todos.db')
+ init_todo_db(conn)
+ c = conn.cursor()
+ c.execute(
+ """
+ INSERT INTO todos (text) VALUES (?);
+ """, [todo])
+ conn.commit()
+
+
+def read_todos() -> sqlite3.Cursor:
+ conn = sqlite3.connect('todos.db')
+ init_todo_db(conn)
+ c = conn.cursor()
+ return c.execute(
+ """
+ SELECT id, text FROM todos LIMIT 50;
+ """)
+
+
+@lib.match_text('(?i)^TODO: (?P<todos>.*)$')
+def handle_todo(ctx: lib.MatchContext):
+ for todo in ctx.named_groups['todos'].split(','):
+ todo = todo.strip()
+ add_todo(todo)
+ ctx.respond(todo + ' added')
+
+
+@lib.name('todos')
+def list_todos(ctx: lib.CommandContext):
+ mes = 'Todos: \n'
+ for row in read_todos():
+ mes += '%d - %s\n' % row
+ ctx.respond(mes)