From d103a31df3e4385088d90e8f2429393a9a084acb Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Sat, 24 Mar 2018 18:49:16 -0700 Subject: [PATCH] remove client lib from bot and keep to the api resolves #27 --- appservice.py | 8 +++--- bot.py | 75 ++++++++++++++++++++------------------------------- 2 files changed, 33 insertions(+), 50 deletions(-) diff --git a/appservice.py b/appservice.py index 55beee6..ec92bbe 100644 --- a/appservice.py +++ b/appservice.py @@ -13,7 +13,7 @@ app = Flask(__name__) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db.init_app(app) -#cmdbot = MonkeyBot() +cmdbot = MonkeyBot() txId = 0 @@ -37,8 +37,8 @@ def on_receive_events(transaction): chan_id = chan.pnut_chan else: adminrm = MatrixAdminRooms.query.filter_by(room_id=event['room_id']).first() - #if adminrm: - # cmdbot.on_message(event) + if adminrm: + cmdbot.on_message(event) return jsonify({}) if (event['content']['msgtype'] == 'm.text' @@ -161,7 +161,7 @@ def on_receive_events(transaction): if event['content']['membership'] == 'invite' and 'is_direct' in event['content'] and event['content']['is_direct'] == True: logging.info('>> GOT PRIVATE INVITE') - #cmdbot.on_invite(event) + cmdbot.on_invite(event) addadminrm = MatrixAdminRooms(matrix_id=event['sender'], room_id=event['room_id']) db.session.add(addadminrm) db.session.commit() diff --git a/bot.py b/bot.py index 65c0eed..a252938 100644 --- a/bot.py +++ b/bot.py @@ -6,8 +6,8 @@ import time import shlex import json import re +import pnutpy -from pnutlib import Pnut from matrix_client.client import MatrixClient from matrix_client.api import MatrixHttpApi from matrix_client.api import MatrixError, MatrixRequestError @@ -20,75 +20,66 @@ class MonkeyBot: 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']) self.api = MatrixHttpApi(self.config['MATRIX_HOST'], self.config['MATRIX_AS_TOKEN']) - - def list_rooms(self): - rooms = self.client.get_rooms() - for rm in rooms: - logging.debug(rm) - logging.debug(len(rooms)) + self.pnut_token = self.config['MATRIX_PNUT_TOKEN'] + pnutpy.api.add_authorization_token(self.pnut_token) def on_invite(self, event): - - logging.info("<__on_invite__>") + logging.debug("<__on_invite__>") logging.debug(event) - - room = self.client.join_room(event['room_id']) + room = self.api.join_room(event['room_id']) def on_message(self, event): - - logging.info("<__on_message__>") + logging.debug("<__on_message__>") logging.debug(event) - rooms = self.client.get_rooms() - logging.info(rooms) - room = rooms[event['room_id']] - if event['type'] == 'm.room.message': if event['content']['msgtype'] == 'm.text': argv = shlex.split(event['content']['body']) cmd = argv[0] args = argv[1:] - self._parse_cmd(room, event, cmd, args) + self._parse_cmd(event, cmd, args) - - def _parse_cmd(self, room, event, cmd, args): - - logging.debug("<__parse_cmd>") - logging.debug(event) + def _parse_cmd(self, event, cmd, args): + logging.debug("<__parse_cmd__>") logging.debug(" " + cmd) logging.debug(args) if cmd.lower() == 'help': - room.send_text(self._help()) + self.api.send_notice(event['room_id'], 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"] + pnutpy.api.add_authorization_token(token) + try: + response, meta = pnutpy.api.get_user('me') + user = MatrixUser(matrix_id=event['user_id'], room_id=event['room_id'], - pnut_id=pnutid, pnut_token=token) + pnut_id=response['username'], 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) + reply = "Token verified, you are now linked as " + response['username'] + + except pnut.api.PnutAuthAPIException as e: + reply = "Your account is not authorized." + + except Exception as e: + reply = "Something went wrong...\n" + reply += str(e) + logging.exception('::set_access_token::') + + self.api.send_notice(event['room_id'], reply) + pnutpy.api.add_authorization_token(self.pnut_token) 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) + self.api.send_notice(event['room_id'], reply) else: - room.send_text(self._help()) + self.api.send_notice(event['room_id'], self._help()) def _help(self): reply = "Visit the following URL to authorize pnut-matrix with your account on pnut.io.\n\n" @@ -100,11 +91,3 @@ class MonkeyBot: reply += " - Drop your access token to remove puppeting\n\n" return reply - -if __name__ == '__main__': - - logging.basicConfig(level=logging.DEBUG) - - bot = MonkeyBot() - - bot.list_rooms()