From 8cd8445a2a4c417aba5cae1c57d8648ea96240a7 Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Tue, 8 May 2018 21:52:53 -0700 Subject: [PATCH] new bot to handle user registrations --- pnut-matrix-bot.py | 124 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 pnut-matrix-bot.py diff --git a/pnut-matrix-bot.py b/pnut-matrix-bot.py new file mode 100644 index 0000000..a05655d --- /dev/null +++ b/pnut-matrix-bot.py @@ -0,0 +1,124 @@ +import logging +import yaml +import time +import pnutpy + +from matrix_bot_api.matrix_bot_api import MatrixBotAPI +from matrix_bot_api.mregex_handler import MRegexHandler +from matrix_bot_api.mcommand_handler import MCommandHandler + +from appservice import app +from models import * + + +def help_cb(room, event): + reply = "This is an admin room for controlling your connection to pnut.io\n" + reply += "The following commands are available.\n\n" + reply += "!auth - Authorize your account on pnut.io\n" + reply += "!save - Save your pnut.io auth token\n" + reply += "!drop - Drop your pnut.io auth token\n" + reply += "!status - Status of your pnut.io auth token\n" + room.send_notice(reply) + +def auth_cb(room, event): + reply = "Visit the following URL to authorize your account on pnut.io.\n\n" + reply += "https://pnut.io/oauth/authenticate" + reply += "?client_id=6SeCRCpCZkmZOKFLFGWbcdAeq2fX1M5t" + reply += "&redirect_uri=urn:ietf:wg:oauth:2.0:oob" + reply += "&scope=write_post,presence,messages&response_type=token\n\n" + reply += "You will be provided with a token that you store with the !save command.\n" + room.send_notice(reply) + +def save_cb(room, event): + args = event['content']['body'].split(' ', maxsplit=1) + + if len(args) < 2: + reply = "You must provide a token with this command.\n" + reply += "!save " + room.send_notice(reply) + return + + pnutpy.api.add_authorization_token(args[1]) + try: + response, meta = pnutpy.api.get_user('me') + + with app.app_context(): + user = MatrixUser( + matrix_id=event['sender'], + room_id='', + pnut_id=response['username'], + pnut_token=args[1]) + db.session.add(user) + db.session.commit() + + reply = "Success! You are now authorized as " + response['username'] + + except pnutpy.errors.PnutAuthAPIException as e: + reply = "Error! Unable to authorize your account." + + except Exception as e: + logging.exception('!save') + reply = "Error! Problem saving your token." + + room.send_notice(reply) + +def drop_cb(room, event): + try: + with app.app_context(): + user = MatrixUser.query.filter_by(matrix_id=event['sender']).first() + db.session.delete(user) + db.session.commit() + + reply = "Success! Your auth token has been removed." + + except Exception as e: + logging.exception('!drop') + reply = "Error! Problem removing your token." + + room.send_notice(reply) + +def status_cb(room, event): + try: + with app.app_context(): + user = MatrixUser.query.filter_by(matrix_id=event['sender']).first() + logging.debug('-- got something --') + logging.debug(user) + + if user is None: + reply = "You are currently not authorized on pnut.io" + else: + pnutpy.api.add_authorization_token(user.pnut_token) + response, meta = pnutpy.api.get_user('me') + reply = "You are currently authorized as " + response['username'] + + except pnutpy.errors.PnutAuthAPIException as e: + reply = "You are currently not authorized on pnut.io" + + except Exception as e: + logging.exception('!status') + reply = "Error! There was a problem checking your account." + + room.send_notice(reply) + + +if __name__ == "__main__": + + logging.basicConfig(level=logging.DEBUG) + + with open("config.yaml", "rb") as config_file: + config = yaml.load(config_file) + + app.config.update(config) + + bot = MatrixBotAPI(config['TBOT_USER'], config['TBOT_PASS'], config['MATRIX_HOST']) + + bot.add_handler(MCommandHandler("help", help_cb)) + bot.add_handler(MCommandHandler("auth", auth_cb)) + bot.add_handler(MCommandHandler("save", save_cb)) + bot.add_handler(MCommandHandler("drop", drop_cb)) + bot.add_handler(MCommandHandler("status", status_cb)) + + bot.start_polling() + + while True: + time.sleep(1)