add broadcast support using a ## suffix issue #15

This commit is contained in:
Morgan McMillian 2017-07-28 16:16:36 -07:00
parent 632f957bd7
commit 309fc7e3c5
2 changed files with 22 additions and 2 deletions

View file

@ -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

View file

@ -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)