remove client lib from bot and keep to the api resolves #27
This commit is contained in:
parent
bdd39960d2
commit
d103a31df3
2 changed files with 33 additions and 50 deletions
|
@ -13,7 +13,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()
|
cmdbot = MonkeyBot()
|
||||||
|
|
||||||
txId = 0
|
txId = 0
|
||||||
|
|
||||||
|
@ -37,8 +37,8 @@ def on_receive_events(transaction):
|
||||||
chan_id = chan.pnut_chan
|
chan_id = chan.pnut_chan
|
||||||
else:
|
else:
|
||||||
adminrm = MatrixAdminRooms.query.filter_by(room_id=event['room_id']).first()
|
adminrm = MatrixAdminRooms.query.filter_by(room_id=event['room_id']).first()
|
||||||
#if adminrm:
|
if adminrm:
|
||||||
# cmdbot.on_message(event)
|
cmdbot.on_message(event)
|
||||||
return jsonify({})
|
return jsonify({})
|
||||||
|
|
||||||
if (event['content']['msgtype'] == 'm.text'
|
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:
|
if event['content']['membership'] == 'invite' and 'is_direct' in event['content'] and event['content']['is_direct'] == True:
|
||||||
|
|
||||||
logging.info('>> GOT PRIVATE INVITE')
|
logging.info('>> GOT PRIVATE INVITE')
|
||||||
#cmdbot.on_invite(event)
|
cmdbot.on_invite(event)
|
||||||
addadminrm = MatrixAdminRooms(matrix_id=event['sender'], room_id=event['room_id'])
|
addadminrm = MatrixAdminRooms(matrix_id=event['sender'], room_id=event['room_id'])
|
||||||
db.session.add(addadminrm)
|
db.session.add(addadminrm)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
75
bot.py
75
bot.py
|
@ -6,8 +6,8 @@ import time
|
||||||
import shlex
|
import shlex
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
|
import pnutpy
|
||||||
|
|
||||||
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
|
||||||
|
@ -20,75 +20,66 @@ class MonkeyBot:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
with open("config.yaml", "rb") as config_file:
|
with open("config.yaml", "rb") as config_file:
|
||||||
self.config = yaml.load(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'])
|
self.api = MatrixHttpApi(self.config['MATRIX_HOST'], self.config['MATRIX_AS_TOKEN'])
|
||||||
|
self.pnut_token = self.config['MATRIX_PNUT_TOKEN']
|
||||||
def list_rooms(self):
|
pnutpy.api.add_authorization_token(self.pnut_token)
|
||||||
rooms = self.client.get_rooms()
|
|
||||||
for rm in rooms:
|
|
||||||
logging.debug(rm)
|
|
||||||
logging.debug(len(rooms))
|
|
||||||
|
|
||||||
def on_invite(self, event):
|
def on_invite(self, event):
|
||||||
|
logging.debug("<__on_invite__>")
|
||||||
logging.info("<__on_invite__>")
|
|
||||||
logging.debug(event)
|
logging.debug(event)
|
||||||
|
room = self.api.join_room(event['room_id'])
|
||||||
room = self.client.join_room(event['room_id'])
|
|
||||||
|
|
||||||
def on_message(self, event):
|
def on_message(self, event):
|
||||||
|
logging.debug("<__on_message__>")
|
||||||
logging.info("<__on_message__>")
|
|
||||||
logging.debug(event)
|
logging.debug(event)
|
||||||
|
|
||||||
rooms = self.client.get_rooms()
|
|
||||||
logging.info(rooms)
|
|
||||||
room = rooms[event['room_id']]
|
|
||||||
|
|
||||||
if event['type'] == 'm.room.message':
|
if event['type'] == 'm.room.message':
|
||||||
if event['content']['msgtype'] == 'm.text':
|
if event['content']['msgtype'] == 'm.text':
|
||||||
argv = shlex.split(event['content']['body'])
|
argv = shlex.split(event['content']['body'])
|
||||||
cmd = argv[0]
|
cmd = argv[0]
|
||||||
args = argv[1:]
|
args = argv[1:]
|
||||||
self._parse_cmd(room, event, cmd, args)
|
self._parse_cmd(event, cmd, args)
|
||||||
|
|
||||||
|
def _parse_cmd(self, event, cmd, args):
|
||||||
def _parse_cmd(self, room, event, cmd, args):
|
logging.debug("<__parse_cmd__>")
|
||||||
|
|
||||||
logging.debug("<__parse_cmd>")
|
|
||||||
logging.debug(event)
|
|
||||||
logging.debug("<cmd> " + cmd)
|
logging.debug("<cmd> " + cmd)
|
||||||
logging.debug(args)
|
logging.debug(args)
|
||||||
|
|
||||||
if cmd.lower() == 'help':
|
if cmd.lower() == 'help':
|
||||||
room.send_text(self._help())
|
self.api.send_notice(event['room_id'], self._help())
|
||||||
|
|
||||||
elif cmd.lower() == 'set_access_token':
|
elif cmd.lower() == 'set_access_token':
|
||||||
token = args[0]
|
token = args[0]
|
||||||
r = Pnut(token).get_user('me')
|
pnutpy.api.add_authorization_token(token)
|
||||||
if r.status_code == 200:
|
try:
|
||||||
rdata = json.loads(r.text)
|
response, meta = pnutpy.api.get_user('me')
|
||||||
pnutid = rdata["data"]["username"]
|
|
||||||
user = MatrixUser(matrix_id=event['user_id'], room_id=event['room_id'],
|
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.add(user)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
reply = "Token verified, you are now linked as " + pnutid
|
reply = "Token verified, you are now linked as " + response['username']
|
||||||
else:
|
|
||||||
reply = "There was an error validating the account."
|
except pnut.api.PnutAuthAPIException as e:
|
||||||
room.send_text(reply)
|
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':
|
elif cmd.lower() == 'drop_access_token':
|
||||||
user = MatrixUser.query.filter_by(matrix_id=event['user_id']).first()
|
user = MatrixUser.query.filter_by(matrix_id=event['user_id']).first()
|
||||||
db.session.delete(user)
|
db.session.delete(user)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
reply = "Your token has been removed."
|
reply = "Your token has been removed."
|
||||||
room.send_text(reply)
|
self.api.send_notice(event['room_id'], reply)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
room.send_text(self._help())
|
self.api.send_notice(event['room_id'], self._help())
|
||||||
|
|
||||||
def _help(self):
|
def _help(self):
|
||||||
reply = "Visit the following URL to authorize pnut-matrix with your account on pnut.io.\n\n"
|
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"
|
reply += " - Drop your access token to remove puppeting\n\n"
|
||||||
return reply
|
return reply
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
|
||||||
|
|
||||||
bot = MonkeyBot()
|
|
||||||
|
|
||||||
bot.list_rooms()
|
|
||||||
|
|
Loading…
Reference in a new issue