Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save landonconover/bfc7b665ce73f59cb313b41cfb90444d to your computer and use it in GitHub Desktop.

Select an option

Save landonconover/bfc7b665ce73f59cb313b41cfb90444d to your computer and use it in GitHub Desktop.
Javascript for posting a message to a Microsoft Teams channel
'use strict';
const axios = require('axios'); // axios must be installed via npm i axios
const webhookURL = "https://outlook.office.com/webhook/1234567890"; // Replace with a valid webhool URL obtained by adding an "Incomming Webhook" connector to your teams channel
if (process.argv.length === 4) {
const title = process.argv[2];
const message = process.argv[3];
console.log(`${title}, ${message}`);
postMessageToTeams(title, message);
} else {
console.log('Usage: node post-message-to-teams.js title messageBody');
}
async function postMessageToTeams(title, message) {
const card = {
'@type': 'MessageCard',
'@context': 'http://schema.org/extensions',
'themeColor': "0072C6", // light blue
summary: 'Summary description',
sections: [
{
activityTitle: title,
text: message,
},
],
};
try {
const response = await axios.post(webhookURL, card, {
headers: {
'content-type': 'application/vnd.microsoft.teams.card.o365connector'
},
});
return `${response.status} - ${response.statusText}`;
} catch (err) {
return err;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment