Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add MSTeams and Telegram channels to CLI #1004

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
EmailAlertChannel,
SlackAlertChannel,
WebhookAlertChannel,
MSTeamsAlertChannel,
TelegramAlertChannel,
} from 'checkly/constructs'

const sendDefaults = {
Expand All @@ -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',
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/constructs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
123 changes: 123 additions & 0 deletions packages/cli/src/constructs/msteams-alert-channel.ts
Original file line number Diff line number Diff line change
@@ -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,
},
}
}
}
66 changes: 66 additions & 0 deletions packages/cli/src/constructs/telegram-alert-channel.ts
Original file line number Diff line number Diff line change
@@ -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=<b>{{ALERT_TITLE}}</b> at {{STARTED_AT}} in {{RUN_LOCATION}} ({{RESPONSE_TIME}}ms)\nTags: {{#each TAGS}} <i><b>{{this}}</b></i> {{#unless @last}},{{/unless}} {{/each}}\n<a href=\"{{RESULT_LINK}}\">View check result</a>\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,
},
}
}
}