add broadcast support using a ## suffix issue #15
This commit is contained in:
parent
632f957bd7
commit
309fc7e3c5
2 changed files with 22 additions and 2 deletions
|
@ -1,6 +1,7 @@
|
||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
import logging
|
import logging
|
||||||
|
import re
|
||||||
|
|
||||||
from bot import MonkeyBot
|
from bot import MonkeyBot
|
||||||
from pnutlib import Pnut
|
from pnutlib import Pnut
|
||||||
|
@ -21,7 +22,7 @@ def on_receive_events(transaction):
|
||||||
|
|
||||||
events = request.get_json()["events"]
|
events = request.get_json()["events"]
|
||||||
for event in events:
|
for event in events:
|
||||||
logging.info(event)
|
# logging.info(event)
|
||||||
user = MatrixUser.query.filter_by(matrix_id=event["user_id"]).first()
|
user = MatrixUser.query.filter_by(matrix_id=event["user_id"]).first()
|
||||||
|
|
||||||
if (event['type'] == 'm.room.message'
|
if (event['type'] == 'm.room.message'
|
||||||
|
@ -99,6 +100,12 @@ def on_receive_events(transaction):
|
||||||
puser = app.config['MATRIX_PNUT_USER']
|
puser = app.config['MATRIX_PNUT_USER']
|
||||||
else:
|
else:
|
||||||
puser = user.pnut_id
|
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)
|
el = MatrixMsgEvents(event['event_id'], event['room_id'], msgid, puser, chan_id)
|
||||||
db.session.add(el)
|
db.session.add(el)
|
||||||
|
@ -238,3 +245,15 @@ def get_displayname(userid):
|
||||||
if 'displayname' in data:
|
if 'displayname' in data:
|
||||||
return data["displayname"]
|
return data["displayname"]
|
||||||
return userid
|
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
|
||||||
|
|
|
@ -109,7 +109,8 @@ class ChannelMonitor(threading.Thread):
|
||||||
def poll_channel(self, room):
|
def poll_channel(self, room):
|
||||||
try:
|
try:
|
||||||
r = Pnut(app.config['MATRIX_PNUT_TOKEN']).get_channel_stream(room.pnut_chan, room.pnut_since)
|
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:
|
except requests.exceptions.ConnectionError:
|
||||||
logging.info('*** Problem connecting to pnut.io! Waiting to retry. ***')
|
logging.info('*** Problem connecting to pnut.io! Waiting to retry. ***')
|
||||||
time.sleep(30)
|
time.sleep(30)
|
||||||
|
|
Loading…
Reference in a new issue