A nodejs Azure Function to post to Slack
- You will need an Azure subscription. You can get one for free here
- your brain
- Fork this project
- Create an
Azure function App
from the portal (as at this time Azure Functions are still in preview you may want to go here - Set the forked repo as your deployment source (gear icon)
- Create a function called
Slackbot
(or whatever name you have changed the function into in your fork) - wait for the repo to sync
- Tricky part: for whatever reason Azure Functions do not run
npm install
. Therefore you need to go toFunction app Settings - Kudu
and open the console, go tosite/wwwroot/Slackbot
and runnpm install
from the console command-line - go to the
Function app settings
- Create a Slack Webhook
- Add an
App Setting
namedSLACK_URL
to your Azure Function, pointing to the Slack Webhook - Test the function by adding a payload (HTTP POST) to the functions's
Run
body
{
"channel": "<webhook-channel>",
"username": "<bot-name>",
"text": "This is your last notice",
"icon_url": "",
"icon_emoji": ":lightning_cloud:",
"fallback": "Upgrade your client",
"notifications": [
{
"type": "error",
"title": "Error title",
"text": "This is an error notification"
},
{
"type": "warn",
"title": "Warn title",
"text": "This is a warning notification"
},
{
"type": "info",
"title": "Info title",
"text": "This is an info notification"
}]
}
#3 Invoking
Pick your poison. Here a Powershell-snippet if you need some help to get going:
# tested with PS 5.1
$url = "<your-azure-function-endpoint-goes-here>"
$body = '{ "channel": "<channel>", "username": "<botname>", "text": "This is your last notice", "icon_url": "", "icon_emoji": ":lightning_cloud:", "fallback": "Upgrade your client", "notifications": [{ "type": "error", "title": "Error title", "text": "This is an error notification" }, { "type": "warn", "title": "Warn title", "text": "This is a warning notification" }, { "type": "info", "title": "Info title", "text": "This is an info notification" }] }'
Invoke-RestMethod $url -Body $body -Method Post -ContentType 'application/json'