From 85db267deaa2f5f77b7deebcfab50a27fd8757c4 Mon Sep 17 00:00:00 2001 From: RedOctober117 Date: Sun, 15 Dec 2024 19:08:26 -0600 Subject: [PATCH 1/3] partial implementation of issue #995 --- packages/cli/src/constructs/index.ts | 1 + .../src/constructs/msteams-alert-channel.ts | 109 ++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 packages/cli/src/constructs/msteams-alert-channel.ts diff --git a/packages/cli/src/constructs/index.ts b/packages/cli/src/constructs/index.ts index f1c1a3b5..19c8eeca 100644 --- a/packages/cli/src/constructs/index.ts +++ b/packages/cli/src/constructs/index.ts @@ -25,3 +25,4 @@ export * from './phone-call-alert-channel' export * from './retry-strategy' export * from './multi-step-check' export * from './alert-escalation-policy' +export * from './msteams-alert-channel' \ No newline at end of file diff --git a/packages/cli/src/constructs/msteams-alert-channel.ts b/packages/cli/src/constructs/msteams-alert-channel.ts new file mode 100644 index 00000000..00481cb4 --- /dev/null +++ b/packages/cli/src/constructs/msteams-alert-channel.ts @@ -0,0 +1,109 @@ +import { AlertChannel, AlertChannelProps } from './alert-channel' +import { Session } from './project' + +export interface MSTeamsAlertChannelProps extends AlertChannelProps { + /** + * The name of your MSTeams alert + */ + name: string + /** + * The name of your MSTeams alert channel. + */ + channel_name: string + /** + * The URL webhook to which to send updates. + */ + url: string + template?: string +} + +/** + * Creates an MSTeams Alert Channel + * + * @remarks + * + * This class make use of the Alert Channel endpoints. + */ +export class MSTeamsAlertChannel extends AlertChannel { + name: string + channel_name: string + url: string + template?: string + + /** + * Constructs the MSTeams Alert Channel instance + * + * @param logicalId unique project-scoped resource name identification + * @param props MSTeams alert channel configuration properties + * Fix following url: + * {@link https://checklyhq.com/docs/cli/constructs/#MSTeamsalertchannel Read more in the docs} + */ + constructor(logicalId: string, props: MSTeamsAlertChannelProps) { + super(logicalId, props) + this.name = props.name; + this.channel_name = props.channel_name; + this.url = props.url; + this.template = props.template || `{ + "type": "message", + "attachments": [ + { + "contentType": "application/vnd.microsoft.card.adaptive", + "content": { + "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", + "type": "AdaptiveCard", + "version": "1.2", + "body": [ + { + "size": "large", + "type": "TextBlock", + "text": "{{ALERT_TITLE}} has failed", + "wrap": true + }, + { + "type": "TextBlock", + "text": "Response time: {{RESPONSE_TIME}}", + "wrap": true + }, + { + "type": "TextBlock", + "text": "Location: {{RUN_LOCATION}}", + "wrap": true + }, + { + "type": "TextBlock", + "text": "Timestamp: {{STARTED_AT}}", + "wrap": true + }, + { + "type": "TextBlock", + "text": "Tags: {{TAGS}}", + "wrap": true + } + ], + "actions": [ + { + "type": "Action.OpenUrl", + "title": "Learn More", + "url": "https://adaptivecards.io" + } + ] + } + } + ] +}`; + Session.registerConstruct(this) + } + + synthesize() { + return { + ...super.synthesize(), + type: 'WEBHOOK', + config: { + name: this.name, + channel_name: this.channel_name, + url: this.url, + template: this.template, + }, + } + } +} From 15de32c64f202c7872815035fc9b2335e0d88b02 Mon Sep 17 00:00:00 2001 From: RedOctober117 Date: Sun, 15 Dec 2024 23:10:53 -0600 Subject: [PATCH 2/3] implementation of telegram for #995 --- packages/cli/src/constructs/index.ts | 3 +- .../src/constructs/telegram-alert-channel.ts | 78 +++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 packages/cli/src/constructs/telegram-alert-channel.ts diff --git a/packages/cli/src/constructs/index.ts b/packages/cli/src/constructs/index.ts index 19c8eeca..ee332cab 100644 --- a/packages/cli/src/constructs/index.ts +++ b/packages/cli/src/constructs/index.ts @@ -25,4 +25,5 @@ export * from './phone-call-alert-channel' export * from './retry-strategy' export * from './multi-step-check' export * from './alert-escalation-policy' -export * from './msteams-alert-channel' \ No newline at end of file +export * from './msteams-alert-channel' +export * from './telegram-alert-channel' \ No newline at end of file diff --git a/packages/cli/src/constructs/telegram-alert-channel.ts b/packages/cli/src/constructs/telegram-alert-channel.ts new file mode 100644 index 00000000..4e897849 --- /dev/null +++ b/packages/cli/src/constructs/telegram-alert-channel.ts @@ -0,0 +1,78 @@ +import { AlertChannel, AlertChannelProps } from './alert-channel' +import { HttpHeader } from './http-header' +import { HttpRequestMethod } from './api-check' +import { QueryParam } from './query-param' +import { Session } from './project' +import { WebhookAlertChannel, WebhookAlertChannelProps } from './webhook-alert-channel' + +export interface TelegramAlertChannelProps extends AlertChannelProps { + /** + * The name of your Telegram alert + */ + name: string + /** + * The chat id of your Telegram bot. + */ + chat_id: string + /** + * API token for your telegram bot. + */ + api_token: string + + /** + * An optional template for use in the GET URL. + */ + template?: string +} + +/** + * Creates an Telegram Alert Channel + * + * @remarks + * + * This class make use of the Alert Channel endpoints. + */ +export class TelegramAlertChannel extends AlertChannel { + name: string + chat_id: string + api_token: string + template?: string + url: string + method: HttpRequestMethod + + /** + * Constructs the Telegram Alert Channel instance + * + * @param logicalId unique project-scoped resource name identification + * @param props Telegram alert channel configuration properties + * Fix following url: + * {@link https://checklyhq.com/docs/cli/constructs/#Telegramalertchannel Read more in the docs} + */ + constructor(logicalId: string, props: TelegramAlertChannelProps) { + super(logicalId, props) + this.name = props.name; + this.chat_id = props.chat_id; + this.api_token = props.api_token; + this.template = props.template || `{{ALERT_TITLE}} at {{STARTED_AT}} in {{RUN_LOCATION}} ({{RESPONSE_TIME}}ms) Tags: {{#each TAGS}} {{this}} {{#unless @last}},{{/unless}} {{/each}} View check result: {{RESULT_LINK}}`; + this.url = `https://api.telegram.org/bot${props.api_token}/sendMessage?chat_id=${props.chat_id}&text=${this.template}`; + this.method = 'GET'; + Session.registerConstruct(this) + } + + synthesize() { + return { + ...super.synthesize(), + type: 'WEBHOOK', + config: { + name: this.name, + chat_id: this.chat_id, + api_token: this.api_token, + url: this.url, + template: this.template, + method: this.method, + }, + } + } +} + + From 3e5e67f06b6db28d8b741d55835ffa95569e737b Mon Sep 17 00:00:00 2001 From: RedOctober117 Date: Mon, 16 Dec 2024 11:10:14 -0600 Subject: [PATCH 3/3] cleanup --- packages/cli/src/constructs/msteams-alert-channel.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/cli/src/constructs/msteams-alert-channel.ts b/packages/cli/src/constructs/msteams-alert-channel.ts index 00481cb4..5935bdbc 100644 --- a/packages/cli/src/constructs/msteams-alert-channel.ts +++ b/packages/cli/src/constructs/msteams-alert-channel.ts @@ -14,6 +14,9 @@ export interface MSTeamsAlertChannelProps extends AlertChannelProps { * The URL webhook to which to send updates. */ url: string + /** + * Optional template for Teams Card. + */ template?: string }