Skip to content

Instantly share code, notes, and snippets.

@gbvanrenswoude
Created June 20, 2022 07:39
Show Gist options
  • Save gbvanrenswoude/d4fd412be84493fd4308930b1eccfba5 to your computer and use it in GitHub Desktop.
Save gbvanrenswoude/d4fd412be84493fd4308930b1eccfba5 to your computer and use it in GitHub Desktop.
Send an Alert to MS Teams from AWS SNS -> Lambda (axios & superagent example)
import { SNSEvent } from 'aws-lambda';
import superagent from 'superagent';
function envkey(key: string): string {
if (!process.env[key]) {
throw new Error(`Missing mandatory environment variable: ${key}`);
}
return process.env[key] as string;
}
const teamsCard = {
'@type': 'MessageCard',
'@context': 'http://schema.org/extensions',
themeColor: 'd70000',
summary: 'something',
sections: [
{
activityTitle: 'something',
activitySubtitle: 'something',
activityImage: 'https://www.antstack.io/static/0ebb2099f6918877f421c198d07cc5ba/f3583/CDK.png',
facts: [
{
name: 'Event',
value: 'something',
},
],
markdown: true,
},
],
potentialAction: [
{
'@type': 'ActionCard',
name: 'Change status',
inputs: [
{
'@type': 'MultichoiceInput',
id: 'list',
title: 'Select a status',
isMultiSelect: 'false',
choices: [
{
display: 'Acknowledged',
value: '1',
},
{
display: 'New',
value: '2',
},
{
display: 'Closed',
value: '3',
},
],
},
],
},
],
};
const url = envkey('url');
export const handler = async (event: SNSEvent) => {
// eslint-disable-next-line no-console
console.log('Received SNS event:', JSON.stringify(event, null, 2));
const records = event.Records.map(async (record) => {
const card = teamsCard;
card.sections[0].facts.push({
name: 'Kind',
value: 'ALERT',
});
card.sections[0].facts.push({
name: 'Message',
value: record.Sns.Message,
});
// NOTE if certain fields exist on message, you could add them to the facts or change themeColor / image asset
// eslint-disable-next-line no-console
console.log('Constructed Teams card:', JSON.stringify(card, null, 2));
await postMessageToTeamsSuperAgent(card, url);
});
await Promise.all(records);
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async function postMessageToTeams(card: unknown, url: string) {
try {
const response = await axios.post(url, card, {
headers: {
'content-type': 'application/vnd.microsoft.teams.card.o365connector',
},
});
return `${response.status} - ${response.statusText}`;
} catch (err) {
// eslint-disable-next-line no-console
console.log(err);
return err;
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async function postMessageToTeamsSuperAgent(card: any, url: string) {
try {
const response = await superagent.post(url).set('content-type', 'application/vnd.microsoft.teams.card.o365connector').send(JSON.stringify(card));
return response;
} catch (err) {
// eslint-disable-next-line no-console
console.log(err);
return err;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment