From 69e0470437c1c8b220ddb18546ea8bee35eeef4a Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Thu, 4 May 2017 21:35:05 -0700 Subject: [PATCH] complete user registration process, closes issue #5 --- appservice.py | 2 +- bot.py | 51 +++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/appservice.py b/appservice.py index bee4edf..76b99ab 100644 --- a/appservice.py +++ b/appservice.py @@ -11,7 +11,7 @@ app = Flask(__name__) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db.init_app(app) -cmdbot = MonkeyBot(app.config) +cmdbot = MonkeyBot() txId = 0 diff --git a/bot.py b/bot.py index a20582c..c9c1d64 100644 --- a/bot.py +++ b/bot.py @@ -7,16 +7,19 @@ import shlex import json import re +from pnutlib import Pnut from matrix_client.client import MatrixClient from matrix_client.api import MatrixHttpApi from matrix_client.api import MatrixError, MatrixRequestError +from models import * class MonkeyBot: txId = 0 - def __init__(self, config): - self.config = config + def __init__(self): + with open("config.yaml", "rb") as config_file: + self.config = yaml.load(config_file) self.client = MatrixClient(self.config['MATRIX_HOST'], token=self.config['MATRIX_AS_TOKEN'], user_id=self.config['MATRIX_AS_ID']) @@ -34,6 +37,7 @@ class MonkeyBot: logging.debug(event) rooms = self.client.get_rooms() + logging.info(rooms) room = rooms[event['room_id']] if event['type'] == 'm.room.message': @@ -51,20 +55,42 @@ class MonkeyBot: logging.debug(" " + cmd) logging.debug(args) - if cmd.lower() == 'echo': - room.send_text(' '.join(args)) - - elif cmd.lower() == 'help': + if cmd.lower() == 'help': room.send_text(self._help()) + elif cmd.lower() == 'set_access_token': + token = args[0] + r = Pnut(token).get_user('me') + if r.status_code == 200: + rdata = json.loads(r.text) + pnutid = rdata["data"]["username"] + user = MatrixUser(matrix_id=event['user_id'], room_id=event['room_id'], + pnut_id=pnutid, pnut_token=token) + db.session.add(user) + db.session.commit() + reply = "Token verified, you are now linked as " + pnutid + else: + reply = "There was an error validating the account." + room.send_text(reply) + + elif cmd.lower() == 'drop_access_token': + user = MatrixUser.query.filter_by(matrix_id=event['user_id']).first() + db.session.delete(user) + db.session.commit() + reply = "Your token has been removed." + room.send_text(reply) + else: - reply = "I'm afraid I don't know this.\n" + " ".join(args) room.send_text(self._help()) def _help(self): - reply = "The following commands are available.\n\n" - reply += "echo " - reply += " - Echo some text back.\n" + reply = "Visit the following URL to authorize pnut-matrix with your account on pnut.io.\n\n" + reply += "https://pnut.io/oauth/authenticate?client_id=6SeCRCpCZkmZOKFLFGWbcdAeq2fX1M5t&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=stream,write_post,follow,update_profile,presence,messages&response_type=token\n\n" + reply += "The following commands are available.\n\n" + reply += "set_access_token \n" + reply += " - Set your access token for matrix -> pnut.io account puppeting\n\n" + reply += "drop_access_token\n" + reply += " - Drop your access token to remove puppeting\n\n" return reply @@ -72,9 +98,6 @@ if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG) - with open("config.yaml", "rb") as config_file: - config = yaml.load(config_file) - - bot = MonkeyBot(config) + bot = MonkeyBot() bot.list_rooms()