From 7379346fc153472146b9afa606b818ee52b06499 Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Mon, 11 Apr 2022 11:28:23 +0200 Subject: [PATCH] Generic webhook transform: allow specifying msgtype (#282) * Generic webhook transform: allow specifying msgtype * Update doc and changelog for msgtype webhook transform --- changelog.d/282.feature | 1 + docs/setup/webhooks.md | 1 + src/Connections/GenericHook.ts | 11 ++++++++--- 3 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 changelog.d/282.feature diff --git a/changelog.d/282.feature b/changelog.d/282.feature new file mode 100644 index 00000000..a3a85d4f --- /dev/null +++ b/changelog.d/282.feature @@ -0,0 +1 @@ +Allow specifying msgtype for generic webhook transformations. diff --git a/docs/setup/webhooks.md b/docs/setup/webhooks.md index 477d83bf..4e21fc41 100644 --- a/docs/setup/webhooks.md +++ b/docs/setup/webhooks.md @@ -109,6 +109,7 @@ The `v2` api expects an object to be returned from the `result` variable. "empty": true|false, // Should the webhook be ignored and no output returned. The default is false (plain must be provided). "plain": "Some text", // The plaintext value to be used for the Matrix message. "html": "Some text", // The HTML value to be used for the Matrix message. If not provided, plain will be interpreted as markdown. + "msgtype": "some.type", // The message type, such as m.notice or m.text, to be used for the Matrix message. If not provided, m.notice will be used. } ``` diff --git a/src/Connections/GenericHook.ts b/src/Connections/GenericHook.ts index 45a91138..e4b5b455 100644 --- a/src/Connections/GenericHook.ts +++ b/src/Connections/GenericHook.ts @@ -47,6 +47,7 @@ interface WebhookTransformationResult { version: string; plain?: string; html?: string; + msgtype?: string; empty?: boolean; } @@ -220,7 +221,7 @@ export class GenericHookConnection extends BaseConnection implements IConnection return msg; } - public executeTransformationFunction(data: unknown): {plain: string, html?: string}|null { + public executeTransformationFunction(data: unknown): {plain: string, html?: string, msgtype?: string}|null { if (!this.transformationFunction) { throw Error('Transformation function not defined'); } @@ -258,10 +259,14 @@ export class GenericHookConnection extends BaseConnection implements IConnection if (transformationResult.html && typeof transformationResult.html !== "string") { throw Error("Result returned from transformation didn't provide a string value for html"); } + if (transformationResult.msgtype && typeof transformationResult.msgtype !== "string") { + throw Error("Result returned from transformation didn't provide a string value for msgtype"); + } return { plain: plain, html: transformationResult.html, + msgtype: transformationResult.msgtype, } } @@ -272,7 +277,7 @@ export class GenericHookConnection extends BaseConnection implements IConnection */ public async onGenericHook(data: unknown): Promise { log.info(`onGenericHook ${this.roomId} ${this.hookId}`); - let content: {plain: string, html?: string}; + let content: {plain: string, html?: string, msgtype?: string}; let success = true; if (!this.transformationFunction) { content = this.transformHookData(data); @@ -295,7 +300,7 @@ export class GenericHookConnection extends BaseConnection implements IConnection await this.ensureDisplayname(); await this.messageClient.sendMatrixMessage(this.roomId, { - msgtype: "m.notice", + msgtype: content.msgtype || "m.notice", body: content.plain, formatted_body: content.html || md.renderInline(content.plain), format: "org.matrix.custom.html",