diff --git a/packages/cli/e2e/__tests__/fixtures/test-project/src/alert-channels.ts b/packages/cli/e2e/__tests__/fixtures/test-project/src/alert-channels.ts index 9d37523f..fe4e3569 100644 --- a/packages/cli/e2e/__tests__/fixtures/test-project/src/alert-channels.ts +++ b/packages/cli/e2e/__tests__/fixtures/test-project/src/alert-channels.ts @@ -4,6 +4,8 @@ import { EmailAlertChannel, SlackAlertChannel, WebhookAlertChannel, + MSTeamsAlertChannel, + TelegramAlertChannel, } from 'checkly/constructs' const sendDefaults = { @@ -30,6 +32,18 @@ export const slackChannel = new SlackAlertChannel('slack-channel-1', { ...sendDefaults, }) +export const msTeamsChannel = new MSTeamsAlertChannel('msteams-channel-1', { + name: "MS Teams Channel", + url: "INSERT_WEBHOOK_HERE", +}) + +export const telegramChannel = new TelegramAlertChannel('telegram-channel-1', { + name: "Telegram Channel", + url: "URL_HERE", + apiToken: "API_TOKEN_HERE", + chatId: "CHAT_ID_HERE", +}) + export const webhookChannel = new WebhookAlertChannel('webhook-channel-1', { name: 'Pushover webhook', method: 'POST', diff --git a/packages/cli/src/constructs/index.ts b/packages/cli/src/constructs/index.ts index f1c1a3b5..735f6f09 100644 --- a/packages/cli/src/constructs/index.ts +++ b/packages/cli/src/constructs/index.ts @@ -25,3 +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' +export * from './telegram-alert-channel' 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..933bb439 --- /dev/null +++ b/packages/cli/src/constructs/msteams-alert-channel.ts @@ -0,0 +1,123 @@ +import { Session } from './project' +import { WebhookAlertChannel, WebhookAlertChannelProps } from './webhook-alert-channel' + +export interface MSTeamsAlertChannelProps extends WebhookAlertChannelProps { + /** + * The name of your MSTeams alert + */ + name: string + /** + * The URL webhook to which to send updates. + */ + url: string +} + +/** + * Creates an MSTeams Alert Channel + * + * @remarks + * + * This class make use of the Alert Channel endpoints. + */ +export class MSTeamsAlertChannel extends WebhookAlertChannel { + name: string + url: 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.url = props.url + this.template = props.template || `{ + "type": "message", + "attachments": [ + { + "contentType": "application/vnd.microsoft.card.adaptive", + "contentUrl": null, + "content": { + "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", + "type": "AdaptiveCard", + "version": "1.2", + "body": [ + { + "type": "Container", + "items": [ + { + "type": "TextBlock", + "text": "{{ALERT_TITLE}}", + "weight": "bolder", + "size": "medium" + }, + { + "type": "ColumnSet", + "columns": [ + { + "type": "Column", + "width": "stretch", + "items": [ + { + "type": "TextBlock", + "text": "Response time: {{RESPONSE_TIME}}ms", + "wrap": true + }, + { + "type": "TextBlock", + "text": "Location: {{RUN_LOCATION}}", + "wrap": true + }, + { + "type": "TextBlock", + "text": "Timestamp: {{STARTED_AT}}", + "wrap": true + }, + {{#if GROUP_NAME}} + { + "type": "TextBlock", + "text": "Group: {{GROUP_NAME}}", + "wrap": true + }, + {{/if}} + { + "type": "TextBlock", + "text": "Tags: {{#each TAGS}} {{this}} {{#unless @last}},{{/unless}} {{/each}}", + "wrap": true + } + ] + } + ] + } + ] + } + ], + "actions": [ + { + "type": "Action.OpenUrl", + "title": "View in Checkly", + "url": "{{RESULT_LINK}}" + } + ] + } + } + ] +}` + Session.registerConstruct(this) + } + + synthesize () { + return { + ...super.synthesize(), + type: 'WEBHOOK_MSTEAMS', + config: { + name: this.name, + url: this.url, + }, + } + } +} 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..688e9507 --- /dev/null +++ b/packages/cli/src/constructs/telegram-alert-channel.ts @@ -0,0 +1,66 @@ +import { HttpRequestMethod } from './api-check' +import { Session } from './project' +import { WebhookAlertChannel, WebhookAlertChannelProps } from './webhook-alert-channel' + +export interface TelegramAlertChannelProps extends WebhookAlertChannelProps { + /** + * The name of your Telegram alert + */ + name: string + /** + * The chat id of your Telegram bot. + */ + chatId: string + /** + * API token for your telegram bot. + */ + apiToken: string +} + +/** + * Creates an Telegram Alert Channel + * + * @remarks + * + * This class make use of the Webhook Alert Channel endpoints. + */ +export class TelegramAlertChannel extends WebhookAlertChannel { + name: string + chatId: string + apiToken: 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.chatId = props.chatId + this.apiToken = props.apiToken + this.template = props.template || `chat_id=${props.chatId}&parse_mode=HTML&text={{ALERT_TITLE}} at {{STARTED_AT}} in {{RUN_LOCATION}} ({{RESPONSE_TIME}}ms)\nTags: {{#each TAGS}} {{this}} {{#unless @last}},{{/unless}} {{/each}}\nView check result\n` + this.url = `https://api.telegram.org/bot${props.apiToken}/sendMessage?chat_id=${props.chatId}&text=${this.template}` + this.method = 'POST' + Session.registerConstruct(this) + } + + synthesize () { + return { + ...super.synthesize(), + type: 'WEBHOOK_TELEGRAM', + config: { + name: this.name, + chatId: this.chatId, + apiToken: this.apiToken, + url: this.url, + method: this.method, + }, + } + } +}