From 6e55bae42ca9f3564cf6798785322392c92cd0af Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Mon, 11 Mar 2019 17:26:25 +0530 Subject: [PATCH 01/23] Move buttons to button_groups.py --- bot.py | 51 ++++++++++++------------------------------------ button_groups.py | 17 ++++++++++++++++ 2 files changed, 30 insertions(+), 38 deletions(-) create mode 100644 button_groups.py diff --git a/bot.py b/bot.py index 5c2f734..2a0db09 100644 --- a/bot.py +++ b/bot.py @@ -10,6 +10,7 @@ from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, \ import signal import sys import re +import button_groups try: import config @@ -33,11 +34,7 @@ def start(bot, update): name += " " + str(update.message.from_user.last_name) text = f"Hello {name}!\n" + \ "Welcome to Questable. To get started, check /help." - custom_keyboard = [ - ['โ‡๏ธ Add Quest', '๐Ÿ“ฏ Add Side Quest'], - ['๐Ÿ“œ List Quests', '๐Ÿ“ƒ List Side Quests'], - ['๐Ÿ… Player Status'] - ] + custom_keyboard = button_groups.main reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) bot.send_message(chat_id=chat_id, text=text, reply_markup=reply_markup) @@ -87,7 +84,7 @@ def add_name(bot, update, player, type, qid): chat_id = update.message.chat_id text = "How difficult is it?" - custom_keyboard = [["๐Ÿ“™ Low", "๐Ÿ“˜ Medium", "๐Ÿ“— High"]] + custom_keyboard = button_groups.difficulty reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) bot.send_message(chat_id=chat_id, text=text, reply_markup=reply_markup) @@ -118,7 +115,7 @@ def add_diff(bot, update, player, type, qid): x.update_db() text = "How important is it?" - custom_keyboard = [["๐Ÿ”น Low", "๐Ÿ”ธ Medium", "๐Ÿ”บ High"]] + custom_keyboard = button_groups.importance reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) bot.send_message(chat_id=chat_id, text=text, reply_markup=reply_markup) @@ -170,11 +167,7 @@ def send_status(bot, update, player, prefix=""): f'๐Ÿ’  Side Quests: {completed_side_quests}/' f'{total_side_quests}\n') chat_id = update.message.chat_id - custom_keyboard = [ - ['โ‡๏ธ Add Quest', '๐Ÿ“ฏ Add Side Quest'], - ['๐Ÿ“œ List Quests', '๐Ÿ“ƒ List Side Quests'], - ['๐Ÿ… Player Status'] - ] + custom_keyboard = button_groups.main reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) bot.send_message(chat_id=chat_id, text=text, reply_markup=reply_markup, parse_mode="HTML") @@ -246,13 +239,7 @@ def quest(bot, update, player, qid, type): elif x.state == 0: state = {"quest": "eq", "side_quest": "esq"}[type] player.set_state(state, qid) - custom_keyboard = [ - ["โœ… Mark as done"], - ["๐Ÿ“ Edit Name", "โš ๏ธ Change Priority"], - ["๐Ÿ“š Change Difficulty", "๐Ÿ—‘ Delete " + - {"quest": "Quest", "side_quest": "Side Quest"}[type]], - ["โฌ…๏ธ Back"]] - + custom_keyboard = button_groups.quests(type) reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) chat_id = update.message.chat_id bot.send_message(chat_id=chat_id, text=text, parse_mode="HTML", @@ -287,11 +274,7 @@ def mark_as_done(bot, update, player, qid, type): player.set_state('none', 0) send_status(bot, update, player, f"๐ŸŒŸ Earned {points} XP\n\n") chat_id = update.message.chat_id - custom_keyboard = [ - ['โ‡๏ธ Add Quest', '๐Ÿ“ฏ Add Side Quest'], - ['๐Ÿ“œ List Quests', '๐Ÿ“ƒ List Side Quests'], - ['๐Ÿ… Player Status'] - ] + custom_keyboard = button_groups.main reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) bot.send_animation(chat_id=chat_id, animation=random.choice(config.gifs), reply_markup=reply_markup) @@ -371,11 +354,7 @@ def help_command(bot, update, db): player = questable.player(db, update.message.chat_id) drop_state(bot, update, player) chat_id = update.message.chat_id - custom_keyboard = [ - ['โ‡๏ธ Add Quest', '๐Ÿ“ฏ Add Side Quest'], - ['๐Ÿ“œ List Quests', '๐Ÿ“ƒ List Side Quests'], - ['๐Ÿ… Player Status'] - ] + custom_keyboard = button_groups.main text = ("*Questable Bot*\n\nQuestable is an RPG-like bot for maintaining " "events in real life. _Main Tasks_ are _Quests_ while _other " "tasks_ are _Side Quests._ You can use the bot to maintain a " @@ -396,11 +375,7 @@ def rate_command(bot, update, db): player = questable.player(db, update.message.chat_id) drop_state(bot, update, player) chat_id = update.message.chat_id - custom_keyboard = [ - ['โ‡๏ธ Add Quest', '๐Ÿ“ฏ Add Side Quest'], - ['๐Ÿ“œ List Quests', '๐Ÿ“ƒ List Side Quests'], - ['๐Ÿ… Player Status'] - ] + custom_keyboard = button_groups.main text = ("[Vote for us on Telegram Directory!](https://t.me/tgdrbot?" "start=questable_bot)\n\n" "Telegram Directory is a website that helps you discover top " @@ -474,14 +449,14 @@ def message_handling(bot, update, db): elif text == "change priority" or text == "โš ๏ธ change priority": player.set_state('eqi', state["extra"]) text = "How important is it?" - custom_keyboard = [["๐Ÿ”น Low", "๐Ÿ”ธ Medium", "๐Ÿ”บ High"]] + custom_keyboard = button_groups.importance reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) bot.send_message(chat_id=player.CHAT_ID, text=text, reply_markup=reply_markup) elif text == "change difficulty" or text == "๐Ÿ“š change difficulty": player.set_state('eqd', state["extra"]) text = "How difficult is it?" - custom_keyboard = [["๐Ÿ“™ Low", "๐Ÿ“˜ Medium", "๐Ÿ“— High"]] + custom_keyboard = button_groups.difficulty reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) bot.send_message(chat_id=player.CHAT_ID, text=text, reply_markup=reply_markup) @@ -510,14 +485,14 @@ def message_handling(bot, update, db): elif text == "change priority" or text == "โš ๏ธ change priority": player.set_state('esqi', state["extra"]) text = "How important is it?" - custom_keyboard = [["๐Ÿ”น Low", "๐Ÿ”ธ Medium", "๐Ÿ”บ High"]] + custom_keyboard = button_groups.importance reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) bot.send_message(chat_id=player.CHAT_ID, text=text, reply_markup=reply_markup) elif text == "change difficulty" or text == "๐Ÿ“š change difficulty": player.set_state('esqd', state["extra"]) text = "How difficult is it?" - custom_keyboard = [["๐Ÿ“™ Low", "๐Ÿ“˜ Medium", "๐Ÿ“— High"]] + custom_keyboard = button_groups.difficulty reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) bot.send_message(chat_id=player.CHAT_ID, text=text, reply_markup=reply_markup) diff --git a/button_groups.py b/button_groups.py new file mode 100644 index 0000000..d66618f --- /dev/null +++ b/button_groups.py @@ -0,0 +1,17 @@ +main = [ + ['โ‡๏ธ Add Quest', '๐Ÿ“ฏ Add Side Quest'], + ['๐Ÿ“œ List Quests', '๐Ÿ“ƒ List Side Quests'], + ['๐Ÿ… Player Status'] + ] + +importance = [["๐Ÿ”น Low", "๐Ÿ”ธ Medium", "๐Ÿ”บ High"]] +difficulty = [["๐Ÿ“™ Low", "๐Ÿ“˜ Medium", "๐Ÿ“— High"]] + + +def quests(cat): + return [ + ["โœ… Mark as done"], + ["๐Ÿ“ Edit Name", "โš ๏ธ Change Priority"], + ["๐Ÿ“š Change Difficulty", "๐Ÿ—‘ Delete " + + {"quest": "Quest", "side_quest": "Side Quest"}[cat]], + ["โฌ…๏ธ Back"]] From 92612471150566e6d1dc3cbe654d13a179a5bce0 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Mon, 11 Mar 2019 18:09:48 +0530 Subject: [PATCH 02/23] Add token table, get_tokens(), add_token() --- bot.py | 3 +++ questable.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/bot.py b/bot.py index 2a0db09..ee3ebd0 100644 --- a/bot.py +++ b/bot.py @@ -557,6 +557,9 @@ queries = [ ("CREATE TABLE IF NOT EXISTS state(chat_id int PRIMARY KEY, state " "varchar(10), extra varchar(10));"), + + ("CREATE TABLE IF NOT EXISTS tokens(chat_id int, token varchar(36)," + "UNIQUE(chat_id, token));"), ] for query in queries: cursor.execute(query) diff --git a/questable.py b/questable.py index da5eb7a..40791b8 100644 --- a/questable.py +++ b/questable.py @@ -1,3 +1,6 @@ +import uuid + + class base_quest(): TABLE = None @@ -153,3 +156,17 @@ class player(): q = side_quest(self.DB, *row) quests.append(q) return quests + + def get_tokens(self): + cursor = self.DB.cursor() + query = ('SELECT token FROM tokens WHERE chat_id=?') + cursor.execute(query, (self.CHAT_ID,)) + tokens = list(map(lambda x: x[0], cursor)) + return tokens + + def add_token(self): + cursor = self.DB.cursor() + token = str(uuid.uuid4()) + query = ('INSERT INTO tokens(chat_id, token) values(?, ?)') + cursor.execute(query, (self.CHAT_ID, token)) + return token From f5f81f5a93acddbc2362c228c0ebc75cbe0e2010 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Mon, 11 Mar 2019 18:17:01 +0530 Subject: [PATCH 03/23] Add delete_token() --- questable.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/questable.py b/questable.py index 40791b8..c470948 100644 --- a/questable.py +++ b/questable.py @@ -169,4 +169,11 @@ class player(): token = str(uuid.uuid4()) query = ('INSERT INTO tokens(chat_id, token) values(?, ?)') cursor.execute(query, (self.CHAT_ID, token)) + self.DB.commit() return token + + def delete_token(self, token): + cursor = self.DB.cursor() + query = ('DELETE FROM tokens WHERE chat_id = ? AND token = ?') + cursor.execute(query, (self.CHAT_ID, token)) + self.DB.commit() From 4737f023e9c722af463c1f13897c4bfc0ec5fc85 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Mon, 11 Mar 2019 21:11:09 +0530 Subject: [PATCH 04/23] Add tokens UI --- bot.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ button_groups.py | 8 +++++- 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/bot.py b/bot.py index ee3ebd0..5891b3c 100644 --- a/bot.py +++ b/bot.py @@ -348,6 +348,8 @@ def drop_state(bot, update, player): x = questable.get_side_quest(player.DB, player.CHAT_ID, state["extra"]) x.delete_from_db() player.set_state('none', 0) + else: + player.set_state('none', 0) def help_command(bot, update, db): @@ -385,6 +387,56 @@ def rate_command(bot, update, db): reply_markup=reply_markup) +def tokens(bot, update): + custom_keyboard = button_groups.tokens + reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) + reply_text = ("Tokens are used to authenticate external " + "applications. This only provides access to " + "Questable data.") + bot.send_message(chat_id=update.message.chat_id, text=reply_text, + reply_markup=reply_markup) + + +def add_token(bot, update, player): + custom_keyboard = button_groups.main + reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) + if len(player.get_tokens()) < 3: + reply_text = player.add_token() + else: + reply_text = "Maximum number of tokens reached." + bot.send_message(chat_id=player.CHAT_ID, text=reply_text, + reply_markup=reply_markup) + + +def delete_token(bot, update, player): + tokens = player.get_tokens() + custom_keyboard = list(map(lambda x: [x], tokens)) + reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) + reply_text = "Choose token to remove." + player.set_state("rt") + bot.send_message(chat_id=update.message.chat_id, text=reply_text, + reply_markup=reply_markup) + + +def delete_token_rt(bot, update, player): + player.delete_token(update.message.text.lower()) + custom_keyboard = button_groups.main + reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) + reply_text = "Token has been removed." + player.set_state("none") + bot.send_message(chat_id=player.CHAT_ID, text=reply_text, + reply_markup=reply_markup) + + +def list_tokens(bot, update, player): + custom_keyboard = button_groups.main + reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) + reply_text = "๐Ÿ“‹ List of tokens\n\n" + reply_text += "\n".join(player.get_tokens()) + bot.send_message(chat_id=update.message.chat_id, text=reply_text, + reply_markup=reply_markup) + + def message_handling(bot, update, db): text = update.message.text.lower() player = questable.player(db, update.message.chat_id) @@ -402,6 +454,7 @@ def message_handling(bot, update, db): # eqn / esqn: Edit Quest / Side Quest Name # eqi / esqi: Edit Quest / Side Quest Importance # eqd / esqd: Edit Quest / Side Quest Difficulty + # rt: Remove token if state["state"] == "none": if text == "add quest" or text == "โ‡๏ธ add quest": @@ -412,6 +465,17 @@ def message_handling(bot, update, db): list_quests(bot, update, player, "quest") elif text == "list side quests" or text == "๐Ÿ“ƒ list side quests": list_quests(bot, update, player, "side_quest") + elif text == "tokens" or text == "๐Ÿ”‘ tokens": + tokens(bot, update) + elif text == "list tokens" or text == "๐Ÿ“‹ list tokens": + list_tokens(bot, update, player) + + elif text == "generate token" or text == "๐Ÿ”‘ generate token": + add_token(bot, update, player) + + elif text == "delete token" or text == "๐Ÿงน delete token": + delete_token(bot, update, player) + else: drop_state(bot, update, player) send_status(bot, update, player) @@ -528,6 +592,8 @@ def message_handling(bot, update, db): elif state["state"] == "esqd": edit_quest(bot, update, player, state["extra"], "diff", "side_quest") + elif state["state"] == "rt": + delete_token_rt(bot, update, player) else: drop_state(bot, update, player) send_status(bot, update, player) diff --git a/button_groups.py b/button_groups.py index d66618f..b5c5174 100644 --- a/button_groups.py +++ b/button_groups.py @@ -1,7 +1,7 @@ main = [ ['โ‡๏ธ Add Quest', '๐Ÿ“ฏ Add Side Quest'], ['๐Ÿ“œ List Quests', '๐Ÿ“ƒ List Side Quests'], - ['๐Ÿ… Player Status'] + ['๐Ÿ… Player Status', '๐Ÿ”‘ Tokens'] ] importance = [["๐Ÿ”น Low", "๐Ÿ”ธ Medium", "๐Ÿ”บ High"]] @@ -15,3 +15,9 @@ def quests(cat): ["๐Ÿ“š Change Difficulty", "๐Ÿ—‘ Delete " + {"quest": "Quest", "side_quest": "Side Quest"}[cat]], ["โฌ…๏ธ Back"]] + + +tokens = [ + ["๐Ÿ“‹ List tokens"], + ["๐Ÿ”‘ Generate token", "๐Ÿงน Delete token"] + ] From 6d67f050939d23e1bfbcb99606fbab34986e8905 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Wed, 13 Mar 2019 17:50:53 +0530 Subject: [PATCH 05/23] Add get_player_from_token --- questable.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/questable.py b/questable.py index c470948..28e3565 100644 --- a/questable.py +++ b/questable.py @@ -177,3 +177,14 @@ class player(): query = ('DELETE FROM tokens WHERE chat_id = ? AND token = ?') cursor.execute(query, (self.CHAT_ID, token)) self.DB.commit() + + +def get_player_from_token(db, token): + cursor = db.cursor() + query = "SELECT chat_id FROM tokens WHERE token=?" + cursor.execute(query, (token,)) + chat_id = cursor.fetchone() + if chat_id is None: + return False + else: + return player(db, chat_id[0]) From 1239cd08cc2c59f751ebcc165e7df910920e24ea Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Wed, 13 Mar 2019 18:30:35 +0530 Subject: [PATCH 06/23] Add /auth --- server.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 server.py diff --git a/server.py b/server.py new file mode 100644 index 0000000..b45b23c --- /dev/null +++ b/server.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +import questable +import sqlite3 +from flask import Flask, jsonify, request + +app = Flask(__name__) +db = sqlite3.connect("questable.db", check_same_thread=False) + + +# /auth. +def auth(db): + success = jsonify({"success": True}) + failure = jsonify({"success": False}) + try: + token = request.args['token'] + except (AttributeError): + return failure + except (KeyError): + return failure + if questable.get_player_from_token(db, token) is False: + return failure + else: + return success + + +app.add_url_rule('/auth', '/auth', lambda: auth(db), methods=['GET']) From 2315b05f2c79763594a470caabb9f1bde96df9db Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Wed, 13 Mar 2019 18:52:44 +0530 Subject: [PATCH 07/23] Add errors.py, get_player, /player. --- errors.py | 3 +++ server.py | 39 ++++++++++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 errors.py diff --git a/errors.py b/errors.py new file mode 100644 index 0000000..0826a60 --- /dev/null +++ b/errors.py @@ -0,0 +1,3 @@ +_400 = {"error": "insufficient parameters"} +_401 = {"error": "invalid token"} +_404 = {"error": "The quest/side quest doesn't exist"} diff --git a/server.py b/server.py index b45b23c..6b9cd66 100644 --- a/server.py +++ b/server.py @@ -2,26 +2,47 @@ import questable import sqlite3 +import errors from flask import Flask, jsonify, request app = Flask(__name__) db = sqlite3.connect("questable.db", check_same_thread=False) -# /auth. -def auth(db): - success = jsonify({"success": True}) - failure = jsonify({"success": False}) +# Returns the player object if valid token +def get_player(db): try: token = request.args['token'] except (AttributeError): - return failure + return False except (KeyError): - return failure - if questable.get_player_from_token(db, token) is False: - return failure + return False + return questable.get_player_from_token(db, token) + + +# /auth. +def auth(db): + if get_player(db) is False: + return jsonify({"success": False}) else: - return success + return jsonify({"success": True}) app.add_url_rule('/auth', '/auth', lambda: auth(db), methods=['GET']) + + +# /player +def player(db): + player = get_player(db) + if player is False: + return jsonify(errors._401), 401 + return jsonify({ + "xp": player.get_points(), + "quests_completed": len(player.get_quests()), + "total_quests": len(player.get_quests(None)), + "side_quests_completed": len(player.get_side_quests()), + "total_side_quests": len(player.get_side_quests(None)), + }) + + +app.add_url_rule('/player', '/player', lambda: player(db), methods=['GET']) From b3093fa1a47cc3cb3f72f3bac80fd1eb6db06e75 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Wed, 13 Mar 2019 19:12:45 +0530 Subject: [PATCH 08/23] Add /get_quests, /get_side_quests --- server.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/server.py b/server.py index 6b9cd66..7eca409 100644 --- a/server.py +++ b/server.py @@ -46,3 +46,39 @@ def player(db): app.add_url_rule('/player', '/player', lambda: player(db), methods=['GET']) + + +def objectify(quest): + return { + "id": quest.QID, + "name": quest.name, + "difficulty": quest.diff, + "priority": quest.imp, + "state": [False, True][quest.state] + } + + +# /get_quests +def get_quests(db): + player = get_player(db) + if player is False: + return jsonify(errors._401), 401 + quests = list(map(objectify, player.get_quests())) + return jsonify(quests) + + +app.add_url_rule('/get_quests', '/get_quests', lambda: get_quests(db), + methods=['GET']) + + +# /get_side_quests +def get_side_quests(db): + player = get_player(db) + if player is False: + return jsonify(errors._401), 401 + side_quests = list(map(objectify, player.get_side_quests())) + return jsonify(side_quests) + + +app.add_url_rule('/get_side_quests', '/get_side_quests', + lambda: get_side_quests(db), methods=['GET']) From 2a5ae51d71bd65c4eb165d9e80881094b6c84ef4 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Wed, 13 Mar 2019 19:49:15 +0530 Subject: [PATCH 09/23] Add get_quest, get_side_quest to player. --- questable.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/questable.py b/questable.py index 28e3565..d75c2ef 100644 --- a/questable.py +++ b/questable.py @@ -157,6 +157,28 @@ class player(): quests.append(q) return quests + def get_quest(self, qid): + cursor = self.DB.cursor() + query = ('SELECT chat_id, qid, name, importance, difficulty, ' + 'state FROM quests WHERE chat_id = ? AND qid = ?') + cursor.execute(query, (self.CHAT_ID, qid)) + row = cursor.fetchone() + if row is None: + return False + else: + return quest(self.DB, *row) + + def get_side_quest(self, qid): + cursor = self.DB.cursor() + query = ('SELECT chat_id, qid, name, importance, difficulty, ' + 'state FROM quests WHERE chat_id = ? AND qid = ?') + cursor.execute(query, (self.CHAT_ID, qid)) + row = cursor.fetchone() + if row is None: + return False + else: + return side_quest(self.DB, *row) + def get_tokens(self): cursor = self.DB.cursor() query = ('SELECT token FROM tokens WHERE chat_id=?') From aa0b3881263ef117140949eae5ff462cde5147d0 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Wed, 13 Mar 2019 19:49:55 +0530 Subject: [PATCH 10/23] Add /get_quest, /get_side_quest --- server.py | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/server.py b/server.py index 7eca409..a45315b 100644 --- a/server.py +++ b/server.py @@ -48,7 +48,7 @@ def player(db): app.add_url_rule('/player', '/player', lambda: player(db), methods=['GET']) -def objectify(quest): +def dictify_quest(quest): return { "id": quest.QID, "name": quest.name, @@ -63,7 +63,7 @@ def get_quests(db): player = get_player(db) if player is False: return jsonify(errors._401), 401 - quests = list(map(objectify, player.get_quests())) + quests = list(map(dictify_quest, player.get_quests())) return jsonify(quests) @@ -76,9 +76,49 @@ def get_side_quests(db): player = get_player(db) if player is False: return jsonify(errors._401), 401 - side_quests = list(map(objectify, player.get_side_quests())) + side_quests = list(map(dictify_quest, player.get_side_quests())) return jsonify(side_quests) app.add_url_rule('/get_side_quests', '/get_side_quests', lambda: get_side_quests(db), methods=['GET']) + + +# /get_quest +def get_quest(db): + player = get_player(db) + if player is False: + return jsonify(errors._401), 401 + try: + qid = request.args['qid'] + except(KeyError): + return jsonify(errors._400), 400 + + quest = player.get_quest(qid) + if quest is False: + return jsonify(errors._404), 404 + return jsonify(dictify_quest(quest)) + + +app.add_url_rule('/get_quest', '/get_quest', lambda: get_quest(db), + methods=['GET']) + + +# /get_side_quest +def get_side_quest(db): + player = get_player(db) + if player is False: + return jsonify(errors._401), 401 + try: + qid = request.args['id'] + except(KeyError): + return jsonify(errors._400), 400 + + side_quest = player.get_side_quest(qid) + if side_quest is False: + return jsonify(errors._404), 404 + return jsonify(dictify_quest(side_quest)) + + +app.add_url_rule('/get_side_quest', '/get_side_quest', + lambda: get_side_quest(db), methods=['GET']) From 7c99523cf180b3fd82020ed986ec901e550a6c08 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Thu, 14 Mar 2019 00:03:49 +0530 Subject: [PATCH 11/23] Add /add_quest, /add_side_quest --- server.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/server.py b/server.py index a45315b..6812dbd 100644 --- a/server.py +++ b/server.py @@ -12,7 +12,7 @@ db = sqlite3.connect("questable.db", check_same_thread=False) # Returns the player object if valid token def get_player(db): try: - token = request.args['token'] + token = request.values['token'] except (AttributeError): return False except (KeyError): @@ -90,7 +90,7 @@ def get_quest(db): if player is False: return jsonify(errors._401), 401 try: - qid = request.args['qid'] + qid = request.values['qid'] except(KeyError): return jsonify(errors._400), 400 @@ -110,7 +110,7 @@ def get_side_quest(db): if player is False: return jsonify(errors._401), 401 try: - qid = request.args['id'] + qid = request.values['id'] except(KeyError): return jsonify(errors._400), 400 @@ -122,3 +122,60 @@ def get_side_quest(db): app.add_url_rule('/get_side_quest', '/get_side_quest', lambda: get_side_quest(db), methods=['GET']) + + +# /add_quest +def add_quest(db): + player = get_player(db) + if player is False: + return jsonify(errors._401), 401 + + x = player.get_quests(None) + if len(x) == 0: + qid = 1 + else: + x.sort(key=lambda i: i.QID, reverse=True) + qid = x[0].QID + 1 + + try: + name = request.values['name'] + imp = request.values['priority'] + diff = request.values['difficulty'] + except (KeyError): + return jsonify(errors._400), 400 + + quest = questable.add_quest(db, player.CHAT_ID, qid, name, imp, diff, 0) + return jsonify(dictify_quest(quest)) + + +app.add_url_rule('/add_quest', '/add_quest', lambda: add_quest(db), + methods=['POST']) + + +# /add_side_quest +def add_side_quest(db): + player = get_player(db) + if player is False: + return jsonify(errors._401), 401 + + x = player.get_side_quests(None) + if len(x) == 0: + qid = 1 + else: + x.sort(key=lambda i: i.QID, reverse=True) + qid = x[0].QID + 1 + + try: + name = request.values['name'] + imp = request.values['priority'] + diff = request.values['difficulty'] + except (KeyError): + return jsonify(errors._400), 400 + + quest = questable.add_side_quest(db, player.CHAT_ID, qid, name, imp, + diff, 0) + return jsonify(dictify_quest(quest)) + + +app.add_url_rule('/add_side_quest', '/add_side_quest', + lambda: add_side_quest(db), methods=['POST']) From 28c33ee97629eb046cd653774b08f57ea2b6158b Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Thu, 14 Mar 2019 00:49:42 +0530 Subject: [PATCH 12/23] Add /update_*. Add checks. --- server.py | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 123 insertions(+), 5 deletions(-) diff --git a/server.py b/server.py index 6812dbd..9719ba1 100644 --- a/server.py +++ b/server.py @@ -90,7 +90,7 @@ def get_quest(db): if player is False: return jsonify(errors._401), 401 try: - qid = request.values['qid'] + qid = request.values['id'] except(KeyError): return jsonify(errors._400), 400 @@ -139,10 +139,15 @@ def add_quest(db): try: name = request.values['name'] - imp = request.values['priority'] - diff = request.values['difficulty'] + imp = int(request.values['priority']) + diff = int(request.values['difficulty']) except (KeyError): return jsonify(errors._400), 400 + except (ValueError): + return jsonify(errors._400), 400 + + if imp not in [1, 2, 3] or diff not in [1, 2, 3]: + return jsonify(errors._400), 400 quest = questable.add_quest(db, player.CHAT_ID, qid, name, imp, diff, 0) return jsonify(dictify_quest(quest)) @@ -167,10 +172,15 @@ def add_side_quest(db): try: name = request.values['name'] - imp = request.values['priority'] - diff = request.values['difficulty'] + imp = int(request.values['priority']) + diff = int(request.values['difficulty']) except (KeyError): return jsonify(errors._400), 400 + except (ValueError): + return jsonify(errors._400), 400 + + if imp not in [1, 2, 3] or diff not in [1, 2, 3]: + return jsonify(errors._400), 400 quest = questable.add_side_quest(db, player.CHAT_ID, qid, name, imp, diff, 0) @@ -179,3 +189,111 @@ def add_side_quest(db): app.add_url_rule('/add_side_quest', '/add_side_quest', lambda: add_side_quest(db), methods=['POST']) + + +# /update_quest +def update_quest(db): + player = get_player(db) + if player is False: + return jsonify(errors._401), 401 + try: + qid = request.values['id'] + except(KeyError): + return jsonify(errors._400), 400 + + available_keys = [i for i in ['name', 'difficulty', 'priority', 'state'] + if i in request.values.keys()] + if len(available_keys) == 0: + return jsonify(errors._400), 400 + quest = questable.get_quest(db, player.CHAT_ID, qid) + + if quest.state == 1: + return jsonify(dictify_quest(quest)) + + for i in available_keys: + try: + if i == "name": + quest.name = request.values["name"] + elif i == "difficulty": + diff = int(request.values["difficulty"]) + if diff in [1, 2, 3]: + quest.diff = diff + else: + return jsonify(errors._400), 400 + elif i == "priority": + imp = int(request.values["priority"]) + if imp in [1, 2, 3]: + quest.imp = imp + else: + return jsonify(errors._400), 400 + elif i == "state": + state = bool(request.values["state"]) + if state is True: + quest.state = 1 + points = 55 + 10*quest.imp + 15*quest.diff + player.add_points(points) + else: + return jsonify(errors._400), 400 + except (ValueError): + return jsonify(errors._400), 400 + + quest.update_db() + return jsonify(dictify_quest(quest)) + + +app.add_url_rule('/update_quest', '/update_quest', lambda: update_quest(db), + methods=['POST']) + + +# /update_side_quest +def update_side_quest(db): + player = get_player(db) + if player is False: + return jsonify(errors._401), 401 + try: + qid = request.values['id'] + except(KeyError): + return jsonify(errors._400), 400 + + available_keys = [i for i in ['name', 'difficulty', 'priority', 'state'] + if i in request.values.keys()] + if len(available_keys) == 0: + return jsonify(errors._400), 400 + quest = questable.get_side_quest(db, player.CHAT_ID, qid) + + if quest.state == 1: + return jsonify(dictify_quest(quest)) + + for i in available_keys: + try: + if i == "name": + quest.name = request.values["name"] + elif i == "difficulty": + diff = int(request.values["difficulty"]) + if diff in [1, 2, 3]: + quest.diff = diff + else: + return jsonify(errors._400), 400 + elif i == "priority": + imp = int(request.values["priority"]) + if imp in [1, 2, 3]: + quest.imp = imp + else: + return jsonify(errors._400), 400 + elif i == "state": + state = bool(request.values["state"]) + if state is True: + quest.state = 1 + points = 10*quest.imp + 15*quest.diff + player.add_points(points) + else: + return jsonify(errors._400), 400 + except (ValueError): + return jsonify(errors._400), 400 + + quest.update_db() + return jsonify(dictify_quest(quest)) + + +app.add_url_rule('/update_side_quest', '/update_side_quest', + lambda: update_side_quest(db), methods=['POST']) From 46097116240c0d335a353ac14cad47105714ece6 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Thu, 14 Mar 2019 20:12:24 +0530 Subject: [PATCH 13/23] Send 404 if quest/side_quest not found --- server.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/server.py b/server.py index 9719ba1..3069914 100644 --- a/server.py +++ b/server.py @@ -207,6 +207,9 @@ def update_quest(db): return jsonify(errors._400), 400 quest = questable.get_quest(db, player.CHAT_ID, qid) + if quest is False: + return jsonify(errors._404), 404 + if quest.state == 1: return jsonify(dictify_quest(quest)) @@ -261,6 +264,9 @@ def update_side_quest(db): return jsonify(errors._400), 400 quest = questable.get_side_quest(db, player.CHAT_ID, qid) + if quest is False: + return jsonify(errors._404), 404 + if quest.state == 1: return jsonify(dictify_quest(quest)) From 5b19524876f36679f57e9f5e92f6c802563be109 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Fri, 15 Mar 2019 11:16:24 +0530 Subject: [PATCH 14/23] Add /delete_* --- server.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/server.py b/server.py index 3069914..b36a46f 100644 --- a/server.py +++ b/server.py @@ -94,9 +94,11 @@ def get_quest(db): except(KeyError): return jsonify(errors._400), 400 - quest = player.get_quest(qid) - if quest is False: + try: + quest = player.get_quest(qid) + except Exception: return jsonify(errors._404), 404 + return jsonify(dictify_quest(quest)) @@ -115,8 +117,12 @@ def get_side_quest(db): return jsonify(errors._400), 400 side_quest = player.get_side_quest(qid) - if side_quest is False: + + try: + side_quest = player.get_quest(qid) + except Exception: return jsonify(errors._404), 404 + return jsonify(dictify_quest(side_quest)) @@ -303,3 +309,55 @@ def update_side_quest(db): app.add_url_rule('/update_side_quest', '/update_side_quest', lambda: update_side_quest(db), methods=['POST']) + + +# /delete_quest +def delete_quest(db): + player = get_player(db) + if player is False: + return jsonify(errors._401), 401 + try: + qid = request.values['id'] + except(KeyError): + return jsonify(errors._400), 400 + + try: + quest = questable.get_quest(db, player.CHAT_ID, qid) + except Exception: + return jsonify(errors._404), 404 + + if quest.state == 1: + return jsonify({"success": False}) + + quest.delete_from_db() + return jsonify({"success": True}) + + +app.add_url_rule('/delete_quest', '/delete_quest', + lambda: delete_quest(db), methods=['DELETE']) + + +# /delete_side_quest +def delete_side_quest(db): + player = get_player(db) + if player is False: + return jsonify(errors._401), 401 + try: + qid = request.values['id'] + except(KeyError): + return jsonify(errors._400), 400 + + try: + side_quest = questable.get_side_quest(db, player.CHAT_ID, qid) + except Exception: + return jsonify(errors._404), 404 + + if side_quest.state == 1: + return jsonify({"success": False}) + + side_quest.delete_from_db() + return jsonify({"success": True}) + + +app.add_url_rule('/delete_side_quest', '/delete_side_quest', + lambda: delete_side_quest(db), methods=['DELETE']) From 333b772d41ed8402f0c3c3812faac80dec733511 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Fri, 15 Mar 2019 12:57:07 +0530 Subject: [PATCH 15/23] Move schema to schema.sql --- bot.py | 28 +++++----------------------- schema.sql | 5 +++++ 2 files changed, 10 insertions(+), 23 deletions(-) mode change 100644 => 100755 bot.py create mode 100644 schema.sql diff --git a/bot.py b/bot.py old mode 100644 new mode 100755 index 5891b3c..614245f --- a/bot.py +++ b/bot.py @@ -604,31 +604,13 @@ def sigterm_handler(signal, frame, db): sys.exit(0) -db = sqlite3.connect("questable.db", check_same_thread=False) -cursor = db.cursor() signal.signal(signal.SIGTERM, lambda x, y: sigterm_handler(x, y, db)) -# Set up tables -queries = [ - ("CREATE TABLE IF NOT EXISTS quests(chat_id int NOT NULL, qid int NOT" - " NULL, name varchar(255), difficulty int, importance int, " - "state int DEFAULT 0, UNIQUE(chat_id, qid));"), - - ("CREATE TABLE IF NOT EXISTS side_quests(chat_id int NOT NULL, qid int " - "NOT NULL, name varchar(255), difficulty int, importance int, " - "state int DEFAULT 0, UNIQUE(chat_id, qid));"), - - ("CREATE TABLE IF NOT EXISTS points(chat_id int PRIMARY KEY, points " - "int);"), - - ("CREATE TABLE IF NOT EXISTS state(chat_id int PRIMARY KEY, state " - "varchar(10), extra varchar(10));"), - - ("CREATE TABLE IF NOT EXISTS tokens(chat_id int, token varchar(36)," - "UNIQUE(chat_id, token));"), - ] -for query in queries: - cursor.execute(query) +# Set up database and tables +db = sqlite3.connect("questable.db", check_same_thread=False) +cursor = db.cursor() +with open('schema.sql') as f: + cursor.executescript(f.read()) db.commit() updater = Updater(token=config.api_key) diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..1c659de --- /dev/null +++ b/schema.sql @@ -0,0 +1,5 @@ +CREATE TABLE IF NOT EXISTS quests(chat_id int NOT NULL, qid int NOT NULL, name varchar(255), difficulty int, importance int, state int DEFAULT 0, UNIQUE(chat_id, qid)); +CREATE TABLE IF NOT EXISTS side_quests(chat_id int NOT NULL, qid int NOT NULL, name varchar(255), difficulty int, importance int, state int DEFAULT 0, UNIQUE(chat_id, qid)); +CREATE TABLE IF NOT EXISTS points(chat_id int PRIMARY KEY, points int); +CREATE TABLE IF NOT EXISTS state(chat_id int PRIMARY KEY, state varchar(10), extra varchar(10)); +CREATE TABLE IF NOT EXISTS tokens(chat_id int, token varchar(36) PRIMARY KEY); From 85c7add252f82056adda6f9b0d9cc939b784d4f0 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Sat, 16 Mar 2019 10:18:09 +0530 Subject: [PATCH 16/23] Redirect / to docs --- server.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/server.py b/server.py index b36a46f..975f06f 100644 --- a/server.py +++ b/server.py @@ -3,7 +3,7 @@ import questable import sqlite3 import errors -from flask import Flask, jsonify, request +from flask import Flask, jsonify, request, redirect app = Flask(__name__) db = sqlite3.connect("questable.db", check_same_thread=False) @@ -361,3 +361,8 @@ def delete_side_quest(db): app.add_url_rule('/delete_side_quest', '/delete_side_quest', lambda: delete_side_quest(db), methods=['DELETE']) + + +@app.route('/') +def redirect_to_docs(): + return redirect("https://questable.webionite.com/api", code=301) From cf05499e439eab4461a2a311ca62ec9c97eacb20 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Sat, 16 Mar 2019 14:23:51 +0530 Subject: [PATCH 17/23] Fix /player. --- server.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/server.py b/server.py index 975f06f..d918cb0 100644 --- a/server.py +++ b/server.py @@ -36,12 +36,14 @@ def player(db): player = get_player(db) if player is False: return jsonify(errors._401), 401 + noq = len(player.get_quests(None)) + nosq = len(player.get_side_quests(None)) return jsonify({ "xp": player.get_points(), - "quests_completed": len(player.get_quests()), - "total_quests": len(player.get_quests(None)), - "side_quests_completed": len(player.get_side_quests()), - "total_side_quests": len(player.get_side_quests(None)), + "quests_completed": noq - len(player.get_quests()), + "total_quests": noq, + "side_quests_completed": nosq - len(player.get_side_quests()), + "total_side_quests": nosq, }) From 4d610c5bff9267d2f12d76b6408cc0aa53824027 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Sat, 16 Mar 2019 14:39:44 +0530 Subject: [PATCH 18/23] Fix /get_quest, /get_side_quest --- server.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/server.py b/server.py index d918cb0..1a630fd 100644 --- a/server.py +++ b/server.py @@ -96,9 +96,8 @@ def get_quest(db): except(KeyError): return jsonify(errors._400), 400 - try: - quest = player.get_quest(qid) - except Exception: + quest = player.get_quest(qid) + if quest is False: return jsonify(errors._404), 404 return jsonify(dictify_quest(quest)) @@ -120,9 +119,7 @@ def get_side_quest(db): side_quest = player.get_side_quest(qid) - try: - side_quest = player.get_quest(qid) - except Exception: + if side_quest is False: return jsonify(errors._404), 404 return jsonify(dictify_quest(side_quest)) From 05d22a0ce8eea2b5f85aa3ec89561aa81e4cf2c6 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Sat, 16 Mar 2019 14:46:47 +0530 Subject: [PATCH 19/23] Add "bad values" error --- errors.py | 1 + server.py | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/errors.py b/errors.py index 0826a60..a0ae6d8 100644 --- a/errors.py +++ b/errors.py @@ -1,3 +1,4 @@ _400 = {"error": "insufficient parameters"} +_400_bv = {"error": "bad values"} _401 = {"error": "invalid token"} _404 = {"error": "The quest/side quest doesn't exist"} diff --git a/server.py b/server.py index 1a630fd..e310954 100644 --- a/server.py +++ b/server.py @@ -149,10 +149,10 @@ def add_quest(db): except (KeyError): return jsonify(errors._400), 400 except (ValueError): - return jsonify(errors._400), 400 + return jsonify(errors._400_bv), 400 if imp not in [1, 2, 3] or diff not in [1, 2, 3]: - return jsonify(errors._400), 400 + return jsonify(errors._400_bv), 400 quest = questable.add_quest(db, player.CHAT_ID, qid, name, imp, diff, 0) return jsonify(dictify_quest(quest)) @@ -182,10 +182,10 @@ def add_side_quest(db): except (KeyError): return jsonify(errors._400), 400 except (ValueError): - return jsonify(errors._400), 400 + return jsonify(errors._400_bv), 400 if imp not in [1, 2, 3] or diff not in [1, 2, 3]: - return jsonify(errors._400), 400 + return jsonify(errors._400_bv), 400 quest = questable.add_side_quest(db, player.CHAT_ID, qid, name, imp, diff, 0) @@ -227,13 +227,13 @@ def update_quest(db): if diff in [1, 2, 3]: quest.diff = diff else: - return jsonify(errors._400), 400 + return jsonify(errors._400_bv), 400 elif i == "priority": imp = int(request.values["priority"]) if imp in [1, 2, 3]: quest.imp = imp else: - return jsonify(errors._400), 400 + return jsonify(errors._400_bv), 400 elif i == "state": state = bool(request.values["state"]) if state is True: @@ -241,9 +241,9 @@ def update_quest(db): points = 55 + 10*quest.imp + 15*quest.diff player.add_points(points) else: - return jsonify(errors._400), 400 + return jsonify(errors._400_bv), 400 except (ValueError): - return jsonify(errors._400), 400 + return jsonify(errors._400_bv), 400 quest.update_db() return jsonify(dictify_quest(quest)) @@ -284,13 +284,13 @@ def update_side_quest(db): if diff in [1, 2, 3]: quest.diff = diff else: - return jsonify(errors._400), 400 + return jsonify(errors._400_bv), 400 elif i == "priority": imp = int(request.values["priority"]) if imp in [1, 2, 3]: quest.imp = imp else: - return jsonify(errors._400), 400 + return jsonify(errors._400_bv), 400 elif i == "state": state = bool(request.values["state"]) if state is True: @@ -298,9 +298,9 @@ def update_side_quest(db): points = 10*quest.imp + 15*quest.diff player.add_points(points) else: - return jsonify(errors._400), 400 + return jsonify(errors._400_bv), 400 except (ValueError): - return jsonify(errors._400), 400 + return jsonify(errors._400_bv), 400 quest.update_db() return jsonify(dictify_quest(quest)) From 99c1795b93beb25217891a1fb86785c6f6952d77 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Sat, 16 Mar 2019 15:15:01 +0530 Subject: [PATCH 20/23] Fix player.get_side_quest --- questable.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/questable.py b/questable.py index d75c2ef..895773a 100644 --- a/questable.py +++ b/questable.py @@ -171,7 +171,7 @@ class player(): def get_side_quest(self, qid): cursor = self.DB.cursor() query = ('SELECT chat_id, qid, name, importance, difficulty, ' - 'state FROM quests WHERE chat_id = ? AND qid = ?') + 'state FROM side_quests WHERE chat_id = ? AND qid = ?') cursor.execute(query, (self.CHAT_ID, qid)) row = cursor.fetchone() if row is None: From 021467817be52d69c0341f59ddcd2b63bcfe842e Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Tue, 19 Mar 2019 16:53:04 +0530 Subject: [PATCH 21/23] Move points calculation outside loop. --- server.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/server.py b/server.py index e310954..f09c88b 100644 --- a/server.py +++ b/server.py @@ -238,13 +238,15 @@ def update_quest(db): state = bool(request.values["state"]) if state is True: quest.state = 1 - points = 55 + 10*quest.imp + 15*quest.diff - player.add_points(points) else: return jsonify(errors._400_bv), 400 except (ValueError): return jsonify(errors._400_bv), 400 + if quest.state == 1: + points = 55 + 10*quest.imp + 15*quest.diff + player.add_points(points) + quest.update_db() return jsonify(dictify_quest(quest)) @@ -295,13 +297,15 @@ def update_side_quest(db): state = bool(request.values["state"]) if state is True: quest.state = 1 - points = 10*quest.imp + 15*quest.diff - player.add_points(points) else: return jsonify(errors._400_bv), 400 except (ValueError): return jsonify(errors._400_bv), 400 + if quest.state == 1: + points = 10*quest.imp + 15*quest.diff + player.add_points(points) + quest.update_db() return jsonify(dictify_quest(quest)) From 697738945fc9385bf8cd8c05066c5b26eb680670 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Tue, 19 Mar 2019 17:47:36 +0530 Subject: [PATCH 22/23] Add group support - names, /Q_, /SQ_ --- bot.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/bot.py b/bot.py index 614245f..d75945b 100755 --- a/bot.py +++ b/bot.py @@ -29,9 +29,12 @@ def escape_html(message): def start(bot, update): chat_id = update.message.chat_id - name = str(update.message.from_user.first_name) - if update.message.from_user.last_name: - name += " " + str(update.message.from_user.last_name) + if update.message.chat.type == "private": + name = str(update.message.from_user.first_name) + if update.message.from_user.last_name: + name += " " + str(update.message.from_user.last_name) + else: + name = update.message.chat.title text = f"Hello {name}!\n" + \ "Welcome to Questable. To get started, check /help." custom_keyboard = button_groups.main @@ -65,7 +68,10 @@ def add_quest(bot, update, player, type="quest"): chat_id = update.message.chat_id text = ("What shall the name of " + {"quest": "Quest", "side_quest": "Side Quest"}[type] + " be?") - reply_markup = telegram.ReplyKeyboardRemove() + if update.message.chat.type == "private": + reply_markup = telegram.ReplyKeyboardRemove() + else: + reply_markup = telegram.ForceReply() bot.send_message(chat_id=chat_id, text=text, reply_markup=reply_markup) @@ -151,9 +157,12 @@ def add_imp(bot, update, player, type, qid): def send_status(bot, update, player, prefix=""): - name = str(update.message.from_user.first_name) - if update.message.from_user.last_name: - name += " " + str(update.message.from_user.last_name) + if update.message.chat.type == "private": + name = str(update.message.from_user.first_name) + if update.message.from_user.last_name: + name += " " + str(update.message.from_user.last_name) + else: + name = update.message.chat.title name = escape_html(name) points = player.get_points() total_quests = len(player.get_quests(None)) @@ -247,7 +256,7 @@ def quest(bot, update, player, qid, type): def quest_handling(bot, update, db): - text = update.message.text.lower().split("_") + text = update.message.text.lower().split("@")[0].split("_") chat_id = update.message.chat_id player = questable.player(db, chat_id) drop_state(bot, update, player) From 3b134ae5f29e9442681876950927caacedd339c8 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Tue, 19 Mar 2019 18:02:05 +0530 Subject: [PATCH 23/23] Avoid sending excess messages and unnecessary state drops --- bot.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/bot.py b/bot.py index d75945b..ea24074 100755 --- a/bot.py +++ b/bot.py @@ -486,8 +486,8 @@ def message_handling(bot, update, db): delete_token(bot, update, player) else: - drop_state(bot, update, player) - send_status(bot, update, player) + if update.message.chat.type == "private": + send_status(bot, update, player) elif state["state"] == "aq": add_name(bot, update, player, "quest", state["extra"]) @@ -540,8 +540,9 @@ def message_handling(bot, update, db): prefix = f"Quest {quest.name} has been deleted\n\n" send_status(bot, update, player, prefix=prefix) else: - drop_state(bot, update, player) - send_status(bot, update, player) + if update.message.chat.type == "private": + drop_state(bot, update, player) + send_status(bot, update, player) elif state["state"] == "esq": if text == "back" or text == "โฌ…๏ธ back": @@ -576,12 +577,14 @@ def message_handling(bot, update, db): prefix = f"Side Quest {sq.name} has been deleted\n\n" send_status(bot, update, player, prefix=prefix) else: - drop_state(bot, update, player) - send_status(bot, update, player) + if update.message.chat.type == "private": + drop_state(bot, update, player) + send_status(bot, update, player) elif state["state"] == "bo": - player.set_state('none', 0) - send_status(bot, update, player) + if text == "back" or update.message.chat.type == "private": + player.set_state('none', 0) + send_status(bot, update, player) elif state["state"] == "eqn": edit_quest(bot, update, player, state["extra"], "name", "quest") @@ -604,8 +607,9 @@ def message_handling(bot, update, db): elif state["state"] == "rt": delete_token_rt(bot, update, player) else: - drop_state(bot, update, player) - send_status(bot, update, player) + if update.message.chat.type == "private": + drop_state(bot, update, player) + send_status(bot, update, player) def sigterm_handler(signal, frame, db):