-
-
Save BrianSkipworth/5e588e7d2c63266affac52e699e604f2 to your computer and use it in GitHub Desktop.
AWS Lambda function to Slack Channel hookup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var services = '/services/...'; // Update this with your Slack webhook URL path | |
var channel = '#fleet-status'; // Update this with the Slack channel to post in | |
var username = 'Vehicle GeoFence Alert'; //Update this with the Slack username to show on the posts | |
var https = require('https'); | |
var util = require('util'); | |
function vehicleName(vehicleId) { | |
switch (vehicleId) { | |
case 9999991:return "Rex"; | |
case 9999992:return "Toro"; | |
case 9999993:return "Ruby"; | |
case 9999994:return "Wally"; | |
case 9999995:return "Deuxy"; | |
case 9999996:return "Bravo"; | |
case 9999997:return "Duke"; | |
case 9999998:return "eHawk"; | |
default:"Mystery Mobile" | |
} | |
}; | |
exports.handler = function(event, context) { | |
console.log(JSON.stringify(event, null, 2)); | |
var vehicleId = event.vehicle_id; | |
var message = vehicleName(vehicleId) + ' is arriving at a stop! See it on the fleet dashboard: https://fleet.tesloop.com/dashboard/vehicles/' + vehicleId; | |
var postData = { | |
"channel": channel, | |
"username": username, | |
"text": message, | |
"icon_emoji": ":round_pushpin:" | |
}; | |
var options = { | |
method: 'POST', | |
hostname: 'hooks.slack.com', | |
port: 443, | |
path: 'services' | |
}; | |
var req = https.request(options, function(res) { | |
res.setEncoding('utf8'); | |
res.on('data', function (chunk) { | |
context.done(null); | |
}); | |
}); | |
req.on('error', function(err) { | |
console.log('Houston, we have a problem: ' + err.message); | |
}); | |
req.write(util.format("%j", postData)); | |
req.end(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment