Add docs, add timeout

This commit is contained in:
Half-Shot 2022-09-05 10:37:02 +01:00
parent cdca069bec
commit 73181e1383
2 changed files with 36 additions and 2 deletions

View File

@ -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": `<img src="${mxc}" alt="My image"></img>`, // The HTML value to be used for the Matrix message. If not provided, plain will be interpreted as markdown.
};
```

View File

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