From 9de4f392d31d586ce46953d9fe3d56750c55d829 Mon Sep 17 00:00:00 2001 From: DarshanPatel11 Date: Fri, 11 Oct 2019 11:59:15 +0530 Subject: [PATCH] Added Telegram To Do Bot Update README.md --- README.md | 1 + To Do Bot/Procfile | 1 + To Do Bot/README.md | 8 +++ To Do Bot/bot.py | 128 +++++++++++++++++++++++++++++++++++++ To Do Bot/dbhelper.py | 35 ++++++++++ To Do Bot/requirements.txt | 5 ++ To Do Bot/runtime.txt | 1 + To Do Bot/todo.sqlite | 0 8 files changed, 179 insertions(+) create mode 100755 To Do Bot/Procfile create mode 100755 To Do Bot/README.md create mode 100755 To Do Bot/bot.py create mode 100755 To Do Bot/dbhelper.py create mode 100755 To Do Bot/requirements.txt create mode 100755 To Do Bot/runtime.txt create mode 100644 To Do Bot/todo.sqlite diff --git a/README.md b/README.md index 96e4bb8..11342b6 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ So far, the following projects have been integrated to this repo: |[Squid installer for Ubuntu](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Squid-Proxy-Installer-for-Ubuntu16)|[Berkay Demir]()| |[Subtitle downloader](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Subtitle-downloader)|[Kaushlendra Pratap](https://github.com/kaushl1998)| |[Take Screenshot](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Take_screenshot)|[Moad Mohammed Elhebri](https://github.com/moadmmh)| +|[To Do Bot](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/To-Do-Bot) | [Darshan Patel](https://github.com/DarshanPatel11)| |[Vinegère Cipher](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/vigenere_cipher)|[victoni](https://github.com/victoni)| |[Web proxy](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Proxy-Request)|[Nikhil Kumar Singh](https://github.com/nikhilkumarsingh)| |[Website blocker](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Website-Blocker)|[Ayush Bhardwaj](https://github.com/hastagAB)| diff --git a/To Do Bot/Procfile b/To Do Bot/Procfile new file mode 100755 index 0000000..29cff6d --- /dev/null +++ b/To Do Bot/Procfile @@ -0,0 +1 @@ +worker: python bot.py diff --git a/To Do Bot/README.md b/To Do Bot/README.md new file mode 100755 index 0000000..8aececd --- /dev/null +++ b/To Do Bot/README.md @@ -0,0 +1,8 @@ +# ToDoBot +Telegram Bot for ToDo list to add, delete, see and check remaining date for particular task. + +## Usage +* create a bot on telegram app using bot-father. +* Add an unique TOKEN generated by bot-father to bot.py +* run the bot using `python bot.py` +* send /help to bot to know the detailed functionality. diff --git a/To Do Bot/bot.py b/To Do Bot/bot.py new file mode 100755 index 0000000..18417e5 --- /dev/null +++ b/To Do Bot/bot.py @@ -0,0 +1,128 @@ +import json +import requests +import time +import urllib +from dbhelper import DBHelper +import datetime +import os + +db = DBHelper() + +TOKEN = os.environ['TOKEN'] +URL = "https://api.telegram.org/bot{}/".format(TOKEN) + + +def get_url(url): + response = requests.get(url) + content = response.content.decode("utf8") + return content + + +def get_json_from_url(url): + content = get_url(url) + js = json.loads(content) + return js + + +def get_updates(offset=None): + url = URL + "getUpdates" + if offset: + url += "?offset={}".format(offset) + js = get_json_from_url(url) + return js + + +def get_last_update_id(updates): + update_ids = [] + for update in updates["result"]: + update_ids.append(int(update["update_id"])) + return max(update_ids) + + +def handle_updates(updates): + text = "" + for update in updates["result"]: + try: + text = update["message"]["text"] + except Exception as e: + pass + chat = update["message"]["chat"]["id"] + if text.startswith("/delete"): + t = text.split(" ") + if len(t) < 2: + send_message("Delete Item Properly.", chat) + else: + items = db.get_items(chat) + if items == []: + send_message("Nothing to Delete.", chat) + elif t[1] in items[0]: + db.delete_item(t[1], chat) + send_message("Item " + t[1] + " Deleted Successfully.", chat) + else: + send_message("Item Not Found.", chat) + elif text == "/start": + send_message("Send /help to show help.", chat) + elif text == "/help": + message = "Send /add item_name due_date(dd-mm-yyyy) to add an item.\nSend /show to display items in to-do list.\nSend /due to check remaining days of each item.\nSend /delele to delete item." + send_message(message, chat) + elif text == "/show": + items = db.get_items(chat) + if items == []: + send_message("Nothing to show.", chat) + for i in range(len(items)): + message = items[i][0] + " " + items[i][1] + send_message(message, chat) + elif text == "/due": + items = db.get_items(chat) + due = [0 for _ in range(0, len(items))] + if items == []: + send_message("Nothing to show.", chat) + today = datetime.datetime.today().strftime("%d-%m-%Y") + for i in range(0, len(items)): + due[i] = (datetime.datetime.strptime(items[i][1], "%d-%m-%Y") - + datetime.datetime.strptime(today, "%d-%m-%Y")).days + message = items[i][0] + " " + str(due[i]) + " days Remaining" + send_message(message, chat) + elif text.startswith("/add"): + t = text.split(" ") + if len(t) < 3: + send_message("Insert Item Properly.\nSend /add item_name due_date to add an item.", chat) + elif len(t[1]) < 1 or len(t[2]) < 1: + send_message("Insert Item Properly.\nSend /add item_name due_date to add an item.", chat) + else: + db.add_item(t[1], t[2], chat) + send_message("Item " + t[1] + " Inserted Successfully.", chat) + else: + send_message("Not a valid Input.", chat) + + +def get_last_chat_id_and_text(updates): + num_updates = len(updates["result"]) + last_update = num_updates - 1 + text = updates["result"][last_update]["message"]["text"] + chat_id = updates["result"][last_update]["message"]["chat"]["id"] + return text, chat_id + + +def send_message(text, chat_id, reply_markup=None): + text = urllib.parse.quote_plus(text) + url = URL + "sendMessage?text={}&chat_id={}&parse_mode=Markdown".format(text, chat_id) + if reply_markup: + url += "&reply_markup={}".format(reply_markup) + get_url(url) + + +def main(): + db.setup() + last_update_id = None + while True: + updates = get_updates(last_update_id) + if len(updates["result"]) > 0: + last_update_id = get_last_update_id(updates) + 1 + handle_updates(updates) + time.sleep(0.5) + + +if __name__ == '__main__': + main() + diff --git a/To Do Bot/dbhelper.py b/To Do Bot/dbhelper.py new file mode 100755 index 0000000..f2ce3b5 --- /dev/null +++ b/To Do Bot/dbhelper.py @@ -0,0 +1,35 @@ +import sqlite3 + + +class DBHelper: + + def __init__(self, dbname="todo.sqlite"): + self.dbname = dbname + self.conn = sqlite3.connect(dbname) + + def setup(self): + tblstmt = "CREATE TABLE IF NOT EXISTS items (description text, due_date date ,owner text)" + itemidx = "CREATE INDEX IF NOT EXISTS itemIndex ON items (description ASC)" + ownidx = "CREATE INDEX IF NOT EXISTS ownIndex ON items (owner ASC)" + self.conn.execute(tblstmt) + self.conn.execute(itemidx) + self.conn.execute(ownidx) + self.conn.commit() + + def add_item(self, item_text, due_date, owner): + stmt = "INSERT INTO items (description, due_date ,owner) VALUES (?, ? ,?)" + args = (item_text, due_date ,owner) + self.conn.execute(stmt, args) + self.conn.commit() + + def delete_item(self, item_text, owner): + stmt = "DELETE FROM items WHERE description = (?) AND owner = (?)" + args = (item_text, owner ) + self.conn.execute(stmt, args) + self.conn.commit() + + def get_items(self, owner): + stmt = "SELECT description,due_date FROM items WHERE owner = (?)" + args = (owner, ) + return [x for x in self.conn.execute(stmt, args)] + diff --git a/To Do Bot/requirements.txt b/To Do Bot/requirements.txt new file mode 100755 index 0000000..ac411cd --- /dev/null +++ b/To Do Bot/requirements.txt @@ -0,0 +1,5 @@ +python-dateutil==2.6.0 +requests +telepot==10.5 +urllib3 +virtualenv==15.1.0 diff --git a/To Do Bot/runtime.txt b/To Do Bot/runtime.txt new file mode 100755 index 0000000..5871d65 --- /dev/null +++ b/To Do Bot/runtime.txt @@ -0,0 +1 @@ +python-3.6.1 \ No newline at end of file diff --git a/To Do Bot/todo.sqlite b/To Do Bot/todo.sqlite new file mode 100644 index 0000000..e69de29