pnut-matrix/bot.py

94 lines
3.3 KiB
Python
Raw Normal View History

2017-05-05 03:23:02 +00:00
import requests
import logging
import yaml
import sys
import time
import shlex
import json
import re
import pnutpy
2017-05-05 03:23:02 +00:00
from matrix_client.client import MatrixClient
from matrix_client.api import MatrixHttpApi
from matrix_client.api import MatrixError, MatrixRequestError
from models import *
2017-05-05 03:23:02 +00:00
class MonkeyBot:
txId = 0
def __init__(self):
with open("config.yaml", "rb") as config_file:
self.config = yaml.load(config_file)
2017-05-05 03:23:02 +00:00
self.api = MatrixHttpApi(self.config['MATRIX_HOST'], self.config['MATRIX_AS_TOKEN'])
self.pnut_token = self.config['MATRIX_PNUT_TOKEN']
pnutpy.api.add_authorization_token(self.pnut_token)
2017-05-05 03:23:02 +00:00
2017-08-30 20:43:43 +00:00
def on_invite(self, event):
logging.debug("<__on_invite__>")
2017-08-30 20:43:43 +00:00
logging.debug(event)
room = self.api.join_room(event['room_id'])
2017-08-30 20:43:43 +00:00
2017-05-05 03:23:02 +00:00
def on_message(self, event):
logging.debug("<__on_message__>")
2017-05-05 03:23:02 +00:00
logging.debug(event)
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(event, cmd, args)
2017-05-05 03:23:02 +00:00
def _parse_cmd(self, event, cmd, args):
logging.debug("<__parse_cmd__>")
2017-05-05 03:23:02 +00:00
logging.debug("<cmd> " + cmd)
logging.debug(args)
if cmd.lower() == 'help':
self.api.send_notice(event['room_id'], self._help())
2017-05-05 03:23:02 +00:00
elif cmd.lower() == 'set_access_token':
token = args[0]
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=response['username'], pnut_token=token)
db.session.add(user)
db.session.commit()
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."
self.api.send_notice(event['room_id'], reply)
2017-05-05 03:23:02 +00:00
else:
self.api.send_notice(event['room_id'], self._help())
2017-05-05 03:23:02 +00:00
def _help(self):
reply = "Visit the following URL to authorize pnut-matrix with your account on pnut.io.\n\n"
2017-07-28 21:34:46 +00:00
reply += "https://pnut.io/oauth/authenticate?client_id=6SeCRCpCZkmZOKFLFGWbcdAeq2fX1M5t&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=write_post,presence,messages&response_type=token\n\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"
2017-05-05 03:23:02 +00:00
return reply