complete user registration process, closes issue #5

This commit is contained in:
Morgan McMillian 2017-05-04 21:35:05 -07:00
parent b2a5eef43c
commit 69e0470437
2 changed files with 38 additions and 15 deletions

View file

@ -11,7 +11,7 @@ app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app) db.init_app(app)
cmdbot = MonkeyBot(app.config) cmdbot = MonkeyBot()
txId = 0 txId = 0

51
bot.py
View file

@ -7,16 +7,19 @@ import shlex
import json import json
import re import re
from pnutlib import Pnut
from matrix_client.client import MatrixClient from matrix_client.client import MatrixClient
from matrix_client.api import MatrixHttpApi from matrix_client.api import MatrixHttpApi
from matrix_client.api import MatrixError, MatrixRequestError from matrix_client.api import MatrixError, MatrixRequestError
from models import *
class MonkeyBot: class MonkeyBot:
txId = 0 txId = 0
def __init__(self, config): def __init__(self):
self.config = config with open("config.yaml", "rb") as config_file:
self.config = yaml.load(config_file)
self.client = MatrixClient(self.config['MATRIX_HOST'], self.client = MatrixClient(self.config['MATRIX_HOST'],
token=self.config['MATRIX_AS_TOKEN'], user_id=self.config['MATRIX_AS_ID']) token=self.config['MATRIX_AS_TOKEN'], user_id=self.config['MATRIX_AS_ID'])
@ -34,6 +37,7 @@ class MonkeyBot:
logging.debug(event) logging.debug(event)
rooms = self.client.get_rooms() rooms = self.client.get_rooms()
logging.info(rooms)
room = rooms[event['room_id']] room = rooms[event['room_id']]
if event['type'] == 'm.room.message': if event['type'] == 'm.room.message':
@ -51,20 +55,42 @@ class MonkeyBot:
logging.debug("<cmd> " + cmd) logging.debug("<cmd> " + cmd)
logging.debug(args) logging.debug(args)
if cmd.lower() == 'echo': if cmd.lower() == 'help':
room.send_text(' '.join(args))
elif cmd.lower() == 'help':
room.send_text(self._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: else:
reply = "I'm afraid I don't know this.\n" + " ".join(args)
room.send_text(self._help()) room.send_text(self._help())
def _help(self): def _help(self):
reply = "The following commands are available.\n\n" reply = "Visit the following URL to authorize pnut-matrix with your account on pnut.io.\n\n"
reply += "echo <text to be echoed>" 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 += " - Echo some text back.\n" reply += "The following commands are available.\n\n"
reply += "set_access_token <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 return reply
@ -72,9 +98,6 @@ if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
with open("config.yaml", "rb") as config_file: bot = MonkeyBot()
config = yaml.load(config_file)
bot = MonkeyBot(config)
bot.list_rooms() bot.list_rooms()