From 73181e1383168bb0d2dbc2804382a7cc75a999d4 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Mon, 5 Sep 2022 10:37:02 +0100 Subject: [PATCH] Add docs, add timeout --- docs/setup/webhooks.md | 25 +++++++++++++++++++++++++ src/Connections/GenericHook.ts | 13 +++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/docs/setup/webhooks.md b/docs/setup/webhooks.md index a1fbb7f5..5211ceef 100644 --- a/docs/setup/webhooks.md +++ b/docs/setup/webhooks.md @@ -177,3 +177,28 @@ if (data.counter > data.maxValue) { result = `*Everything is fine*, the counter is under by ${data.maxValue - data.counter}` } ``` + + +### Functions + +The scripting environment exposes some convienence functions for doing advanced transformations +of your data. + +#### `getMediaUrl(url: string)` + +This function will take an input URL and return a deferred MXC URL, which can then be inserted +into your response to display an image. If the input URL fails to be resolved, the deferred +URL will be left in place. + +There is a maximum timeout for the fetch request of 5 seconds. + +#### Example + +```js +const mxc = getMediaUrl(data.myimage); +return { + "version": "v2" // The version of the schema being returned from the function. This is always "v2". + "plain": `![My image](${mxc})`, // The plaintext value to be used for the Matrix message. + "html": `My image`, // The HTML value to be used for the Matrix message. If not provided, plain will be interpreted as markdown. +}; +``` \ No newline at end of file diff --git a/src/Connections/GenericHook.ts b/src/Connections/GenericHook.ts index 226c48c8..91b9d171 100644 --- a/src/Connections/GenericHook.ts +++ b/src/Connections/GenericHook.ts @@ -11,6 +11,8 @@ import { BaseConnection } from "./BaseConnection"; import { GetConnectionsResponseItem } from "../provisioning/api"; import { BridgeConfigGenericWebhooks } from "../Config/Config"; import { randomUUID } from "crypto"; +import axios from "axios"; +import UserAgent from "../UserAgent"; export interface GenericHookConnectionState extends IConnectionState { /** @@ -59,6 +61,7 @@ const md = new markdownit(); const TRANSFORMATION_TIMEOUT_MS = 500; const SANITIZE_MAX_DEPTH = 5; const SANITIZE_MAX_BREADTH = 25; +const GET_MEDIA_TIMEOUT = 5000; /** * Handles rooms connected to a generic webhook. @@ -353,13 +356,19 @@ export class GenericHookConnection extends BaseConnection implements IConnection continue; } try { - const mxcUri = await this.as.botClient.uploadContentFromUrl(mediaurl); + const content = await axios.get(mediaurl, { + timeout: GET_MEDIA_TIMEOUT, + headers: { + 'User-Agent': UserAgent, + } + }) + const mxcUri = await this.as.botClient.uploadContent(content.data, content.headers['content-type']); plain = plain.replace(uuid, mxcUri); if (transformationResult.html) { transformationResult.html = transformationResult.html.replace(transformationResult.html, mxcUri); } } catch (ex) { - log.warn(`Failed to fetch media for transformation ${mediaurl}`, ex.message); + log.warn(`Failed to fetch media for transformation`, {mediaurl}); log.debug(ex); } }