From 309fc7e3c54cf89b9b01f79db7b22a82769bd80d Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Fri, 28 Jul 2017 16:16:36 -0700 Subject: [PATCH] add broadcast support using a ## suffix issue #15 --- appservice.py | 21 ++++++++++++++++++++- pnut-bridge.py | 3 ++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/appservice.py b/appservice.py index d881b5f..96c8f00 100644 --- a/appservice.py +++ b/appservice.py @@ -1,6 +1,7 @@ import json import requests import logging +import re from bot import MonkeyBot from pnutlib import Pnut @@ -21,7 +22,7 @@ def on_receive_events(transaction): events = request.get_json()["events"] for event in events: - logging.info(event) + # logging.info(event) user = MatrixUser.query.filter_by(matrix_id=event["user_id"]).first() if (event['type'] == 'm.room.message' @@ -99,6 +100,12 @@ def on_receive_events(transaction): puser = app.config['MATRIX_PNUT_USER'] else: puser = user.pnut_id + cctag = re.search('##$', text) + if cctag: + cname = get_channel_settings(chan_id)['name'] + text = text[:-2] + text += '\n\n[' + cname + "](https://patter.chat/room.html?channel=" + chan_id + ")" + r = Pnut(token).post(text) el = MatrixMsgEvents(event['event_id'], event['room_id'], msgid, puser, chan_id) db.session.add(el) @@ -238,3 +245,15 @@ def get_displayname(userid): if 'displayname' in data: return data["displayname"] return userid + +def get_channel_settings(channel_id): + chan_settings = {} + r = requests.get('https://api.pnut.io/v0/channels/' + str(channel_id) + '?include_raw=1') + if r.status_code == 200: + cdata = r.json()['data'] + raw = cdata['raw'] + for item in raw: + if item['type'] == 'io.pnut.core.chat-settings': + chan_settings = item['value'] + + return chan_settings diff --git a/pnut-bridge.py b/pnut-bridge.py index 72a9cbb..5865f5d 100644 --- a/pnut-bridge.py +++ b/pnut-bridge.py @@ -109,7 +109,8 @@ class ChannelMonitor(threading.Thread): def poll_channel(self, room): try: r = Pnut(app.config['MATRIX_PNUT_TOKEN']).get_channel_stream(room.pnut_chan, room.pnut_since) - # print(r.headers['X-RateLimit-Remaining'], r.headers['X-RateLimit-Reset']) + if r.headers['X-RateLimit-Remaining'] < 40: + print(r.headers['X-RateLimit-Remaining'], r.headers['X-RateLimit-Reset']) except requests.exceptions.ConnectionError: logging.info('*** Problem connecting to pnut.io! Waiting to retry. ***') time.sleep(30)