Skip to content

Commit 409e9a5

Browse files
feat: adds MS Teams and Telegram alert channels (#1028)
* partial implementation of issue #995 * implementation of telegram for #995 * cleanup * fixes for all grievences but tests * tests i think? * fixed formatting error * fix: fixes code style * fix: fixes typo * feat: adds MS teams and Telegram alert channels --------- Co-authored-by: RedOctober117 <dbcahilly3@gmail.com>
1 parent 45ca172 commit 409e9a5

File tree

5 files changed

+208
-1
lines changed

5 files changed

+208
-1
lines changed

packages/cli/e2e/__tests__/fixtures/test-project/src/alert-channels.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
EmailAlertChannel,
55
SlackAlertChannel,
66
WebhookAlertChannel,
7+
MSTeamsAlertChannel,
8+
TelegramAlertChannel,
79
} from 'checkly/constructs'
810

911
const sendDefaults = {
@@ -30,6 +32,17 @@ export const slackChannel = new SlackAlertChannel('slack-channel-1', {
3032
...sendDefaults,
3133
})
3234

35+
export const msTeamsChannel = new MSTeamsAlertChannel('msteams-channel-1', {
36+
name: 'MS Teams Channel',
37+
url: 'INSERT_WEBHOOK_HERE',
38+
})
39+
40+
export const telegramChannel = new TelegramAlertChannel('telegram-channel-1', {
41+
name: 'Telegram Channel',
42+
apiKey: 'API_TOKEN_HERE',
43+
chatId: 'CHAT_ID_HERE',
44+
})
45+
3346
export const webhookChannel = new WebhookAlertChannel('webhook-channel-1', {
3447
name: 'Pushover webhook',
3548
method: 'POST',

packages/cli/src/constructs/incidentio-alert-channel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface IncidentioAlertChannelProps extends AlertChannelProps {
1313
url: URL|string
1414
/**
1515
* The API key created by installing the Checkly integration in Incident.io.
16-
* {@link {@link https://www.checklyhq.com/docs/integrations/incidentio/}}
16+
* {@link https://www.checklyhq.com/docs/integrations/incidentio/}
1717
*/
1818
apiKey: string
1919
}

packages/cli/src/constructs/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ export * from './multi-step-check'
2727
export * from './alert-escalation-policy'
2828
export * from './tcp-check'
2929
export * from './incidentio-alert-channel'
30+
export * from './msteams-alert-channel'
31+
export * from './telegram-alert-channel'
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { WebhookAlertChannel } from './webhook-alert-channel'
2+
import { AlertChannelProps } from './alert-channel'
3+
4+
export interface MSTeamsAlertChannelProps extends AlertChannelProps {
5+
/**
6+
* Friendly name to recognise the integration.
7+
* */
8+
name: string
9+
/**
10+
* The unique URL created by creating an integration in Microsoft Teams.
11+
* {@link https://www.checklyhq.com/docs/integrations/msteams/}
12+
*/
13+
url: string
14+
}
15+
16+
/**
17+
* Creates a Microsoft Teams Alert Channel
18+
*
19+
* @remarks
20+
*
21+
* This class make use of the Alert Channel endpoints.
22+
*/
23+
export class MSTeamsAlertChannel extends WebhookAlertChannel {
24+
/**
25+
* Constructs the Microsoft Teams Alert Channel instance
26+
*
27+
* @param logicalId unique project-scoped resource name identification
28+
* @param props MSTeams alert channel configuration properties
29+
*
30+
* {@link https://checklyhq.com/docs/cli/constructs/#msteamsalertchannel Read more in the docs}
31+
*/
32+
constructor (logicalId: string, props: MSTeamsAlertChannelProps) {
33+
super(logicalId, props)
34+
this.webhookType = 'WEBHOOK_MSTEAMS'
35+
this.method = 'POST'
36+
this.template = `{
37+
"type":"message",
38+
"attachments":[
39+
{
40+
"contentType":"application/vnd.microsoft.card.adaptive",
41+
"contentUrl":null,
42+
"content":{
43+
"$schema":"http://adaptivecards.io/schemas/adaptive-card.json",
44+
"type":"AdaptiveCard",
45+
"version":"1.2",
46+
"body":[
47+
{
48+
"type": "Container",
49+
"items": [
50+
{
51+
"type": "TextBlock",
52+
"text": "{{ALERT_TITLE}}",
53+
"weight": "bolder",
54+
"size": "medium"
55+
},
56+
{
57+
"type": "ColumnSet",
58+
"columns": [
59+
{
60+
"type": "Column",
61+
"width": "stretch",
62+
"items": [
63+
{
64+
"type": "TextBlock",
65+
"text": "Response time: {{RESPONSE_TIME}}ms",
66+
"wrap": true
67+
},
68+
{
69+
"type": "TextBlock",
70+
"text": "Location: {{RUN_LOCATION}}",
71+
"wrap": true
72+
},
73+
{
74+
"type": "TextBlock",
75+
"text": "Timestamp: {{STARTED_AT}}",
76+
"wrap": true
77+
},
78+
{{#if GROUP_NAME}}
79+
{
80+
"type": "TextBlock",
81+
"text": "Group: {{GROUP_NAME}}",
82+
"wrap": true
83+
},
84+
{{/if}}
85+
{
86+
"type": "TextBlock",
87+
"text": "Tags: {{#each TAGS}} {{this}} {{#unless @last}},{{/unless}} {{/each}}",
88+
"wrap": true
89+
}
90+
]
91+
}
92+
]
93+
}
94+
]
95+
}
96+
],
97+
"actions":[
98+
{
99+
"type":"Action.OpenUrl",
100+
"title":"View in Checkly",
101+
"url":"{{RESULT_LINK}}"
102+
}
103+
]
104+
}
105+
}
106+
]
107+
}
108+
`
109+
}
110+
111+
synthesize () {
112+
return {
113+
...super.synthesize(),
114+
type: 'WEBHOOK',
115+
config: {
116+
name: this.name,
117+
webhookType: this.webhookType,
118+
url: this.url,
119+
template: this.template,
120+
method: this.method,
121+
headers: this.headers,
122+
queryParameters: this.queryParameters,
123+
webhookSecret: this.webhookSecret,
124+
},
125+
}
126+
}
127+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { WebhookAlertChannel } from './webhook-alert-channel'
2+
import { AlertChannelProps } from './alert-channel'
3+
4+
export interface TelegramAlertChannelProps extends AlertChannelProps {
5+
/**
6+
* Friendly name to recognise the integration.
7+
*/
8+
name: string
9+
/**
10+
* The chat ID of your Telegram bot.
11+
* {@link https://www.checklyhq.com/docs/integrations/telegram/}
12+
*/
13+
chatId: string
14+
/**
15+
* The API key for your Telegram bot.
16+
* {@link https://www.checklyhq.com/docs/integrations/telegram/}
17+
*/
18+
apiKey: string
19+
}
20+
21+
/**
22+
* Creates a Telegram Alert Channel
23+
*
24+
* @remarks
25+
*
26+
* This class make use of the Alert Channel endpoints.
27+
*/
28+
export class TelegramAlertChannel extends WebhookAlertChannel {
29+
/**
30+
* Constructs the Telegram Alert Channel instance
31+
*
32+
* @param logicalId unique project-scoped resource name identification
33+
* @param props Telegram alert channel configuration properties
34+
* Fix following url:
35+
* {@link https://checklyhq.com/docs/cli/constructs/#telegramalertchannel Read more in the docs}
36+
*/
37+
constructor (logicalId: string, props: TelegramAlertChannelProps) {
38+
// @ts-ignore
39+
super(logicalId, props)
40+
this.webhookType = 'WEBHOOK_TELEGRAM'
41+
this.method = 'POST'
42+
this.template = `chat_id=${props.chatId}&parse_mode=HTML&text=<b>{{ALERT_TITLE}}</b> at {{STARTED_AT}} in {{RUN_LOCATION}} ({{RESPONSE_TIME}}ms)
43+
Tags: {{#each TAGS}} <i><b>{{this}}</b></i> {{#unless @last}},{{/unless}} {{/each}}
44+
<a href="{{RESULT_LINK}}">View check result</a>
45+
`
46+
this.url = `https://api.telegram.org/bot${props.apiKey}/sendMessage`
47+
}
48+
49+
synthesize () {
50+
return {
51+
...super.synthesize(),
52+
type: 'WEBHOOK',
53+
config: {
54+
name: this.name,
55+
webhookType: this.webhookType,
56+
url: this.url,
57+
template: this.template,
58+
method: this.method,
59+
headers: this.headers,
60+
queryParameters: this.queryParameters,
61+
webhookSecret: this.webhookSecret,
62+
},
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)