Commit d103a31d authored by Morgan McMillian's avatar Morgan McMillian

remove client lib from bot and keep to the api resolves #27

parent bdd39960
......@@ -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()
......
......@@ -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> " + 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()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment