add oembed to channel post

This commit is contained in:
Morgan McMillian 2017-05-03 07:23:56 -07:00
parent 7b8e7d946c
commit 543e186dc8
2 changed files with 19 additions and 3 deletions

View file

@ -25,6 +25,8 @@ def on_receive_events(transaction):
and not app.config['MATRIX_PNUT_PREFIX'] in event["user_id"] and not app.config['MATRIX_PNUT_PREFIX'] in event["user_id"]
and not 'pnut-bridge' in event["user_id"]): and not 'pnut-bridge' in event["user_id"]):
embed = None
if (event['content']['msgtype'] == 'm.text' if (event['content']['msgtype'] == 'm.text'
or event['content']['msgtype'] == 'm.notice'): or event['content']['msgtype'] == 'm.notice'):
if user: if user:
@ -42,6 +44,16 @@ def on_receive_events(transaction):
text = "* " + get_displayname(event["user_id"]) + " " + event['content']['body'] text = "* " + get_displayname(event["user_id"]) + " " + event['content']['body']
elif event['content']['msgtype'] == 'm.image': elif event['content']['msgtype'] == 'm.image':
imgurl = app.config['MATRIX_HOST'] + '/_matrix/media/r0/download/' + event['content']['url'][6:] imgurl = app.config['MATRIX_HOST'] + '/_matrix/media/r0/download/' + event['content']['url'][6:]
value = {"type": "photo", "version": "1.0"}
value["title"] = event['content']['body']
value["url"] = imgurl
value["width"] = event['content']['info']['w']
value["height"] = event['content']['info']['h']
value["thumbnail_width"] = event['content']['info']['thumbnail_info']['w']
value["thumbnail_height"] = event['content']['info']['thumbnail_info']['h']
value["thumbnail_url"] = self.api.get_download_url(event['content']['info']['thumbnail_url'])
rawitem = {"type": "io.pnut.core.oembed", "value": value}
embed = [rawitem]
if user: if user:
token = user.pnut_token token = user.pnut_token
text = "" text = ""
@ -67,7 +79,7 @@ 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) r = Pnut(token).channel_post(chan_id, text, embed)
if r.status_code == 201: if r.status_code == 201:
msgid = json.loads(r.text)['data']['id'] msgid = json.loads(r.text)['data']['id']
if token == app.config['MATRIX_PNUT_TOKEN']: if token == app.config['MATRIX_PNUT_TOKEN']:

View file

@ -145,13 +145,17 @@ class Pnut:
r = requests.post(url, data=body, headers=headers) r = requests.post(url, data=body, headers=headers)
return r return r
def channel_post(self, chan_id, text): def channel_post(self, chan_id, text, raw):
url = "https://api.pnut.io/v0/channels/" + str(chan_id) + "/messages" url = "https://api.pnut.io/v0/channels/" + str(chan_id) + "/messages"
headers = { headers = {
"Authorization": "Bearer " + self.token, "Authorization": "Bearer " + self.token,
"Content-Type": "application/json" "Content-Type": "application/json"
} }
body = json.dumps({"text": text }) data = {"text": text }
if raw:
url += "?include_raw=1"
data["raw"] = raw
body = json.dumps(data)
r = requests.post(url, data=body, headers=headers) r = requests.post(url, data=body, headers=headers)
return r return r