replace pnutlib with pnutpy in appservice

issue #17
This commit is contained in:
Morgan McMillian 2018-01-09 16:28:53 -08:00
parent eef6224007
commit 27fc81d700

View file

@ -2,9 +2,10 @@ import json
import requests import requests
import logging import logging
import re import re
import pnutpy
from bot import MonkeyBot from bot import MonkeyBot
from pnutlib import Pnut # from pnutlib import Pnut
from flask import Flask, jsonify, request, abort from flask import Flask, jsonify, request, abort
from models import * from models import *
@ -93,9 +94,10 @@ def on_receive_events(transaction):
requests.put(url, headers={"Content-Type": "application/json"}, data=json.dumps({})) requests.put(url, headers={"Content-Type": "application/json"}, data=json.dumps({}))
else: else:
if text: if text:
r = Pnut(token).channel_post(chan_id, text, embed) pnutpy.api.add_authorization_token(token)
if r.status_code == 201: try:
msgid = json.loads(r.text)['data']['id'] msg, meta = pnutpy.api.create_message(chan_id, data={'text': text, 'raw': embed})
if token == app.config['MATRIX_PNUT_TOKEN']: if token == app.config['MATRIX_PNUT_TOKEN']:
puser = app.config['MATRIX_PNUT_USER'] puser = app.config['MATRIX_PNUT_USER']
else: else:
@ -105,12 +107,13 @@ def on_receive_events(transaction):
cname = get_channel_settings(chan_id)['name'] cname = get_channel_settings(chan_id)['name']
text = text[:-2] text = text[:-2]
text += '\n\n[' + cname + "](https://patter.chat/room.html?channel=" + chan_id + ")" text += '\n\n[' + cname + "](https://patter.chat/room.html?channel=" + chan_id + ")"
r = Pnut(token).post(text) r, meta = pnutpy.api.create_post(data={'text': text})
el = MatrixMsgEvents(event['event_id'], event['room_id'], msgid, puser, chan_id) el = MatrixMsgEvents(event['event_id'], event['room_id'], msg.id, puser, chan_id)
db.session.add(el) db.session.add(el)
db.session.commit() db.session.commit()
elif r.status_code == 401:
except pnutpy.errors.PnutAuthAPIException:
txId += 1 txId += 1
err_notice = event["user_id"] + ": The pnut.io channel this room is connected with is restricted to valid " err_notice = event["user_id"] + ": The pnut.io channel this room is connected with is restricted to valid "
err_notice += "pnut.io users only. Start a direct chat to link your account." err_notice += "pnut.io users only. Start a direct chat to link your account."
@ -119,8 +122,10 @@ def on_receive_events(transaction):
url += '/_matrix/client/r0/rooms/{0}/send/m.room.message/{1}'.format(event['room_id'], str(txId)) url += '/_matrix/client/r0/rooms/{0}/send/m.room.message/{1}'.format(event['room_id'], str(txId))
url += "?access_token=" + app.config['MATRIX_AS_TOKEN'] url += "?access_token=" + app.config['MATRIX_AS_TOKEN']
requests.put(url, headers={"Content-Type": "application/json"}, data=json.dumps(body)) requests.put(url, headers={"Content-Type": "application/json"}, data=json.dumps(body))
else:
logging.debug(r.text) except:
logging.debug('*** an error occured while posting a message ***')
elif event['type'] == 'm.room.redaction': elif event['type'] == 'm.room.redaction':
r_event = MatrixMsgEvents.query.filter_by(event_id=event['redacts'],room_id=event['room_id']).first() r_event = MatrixMsgEvents.query.filter_by(event_id=event['redacts'],room_id=event['room_id']).first()
@ -134,7 +139,8 @@ def on_receive_events(transaction):
else: else:
return jsonify({}) return jsonify({})
r = Pnut(token).delete_channel_post(r_event.pnut_chan, r_event.pnut_msgid) pnutpy.api.add_authorization_token(token)
r, meta = pnutpy.api.delete_message(r_event.pnut_chan, r_event.pnut_msgid)
elif event['type'] == 'm.room.member': elif event['type'] == 'm.room.member':