Commit a92a127f authored by Paweł Barszcz's avatar Paweł Barszcz

Code for the first post from the series

parents
/.idea/*
!/.idea/codeStyleSettings.xml
*.iml
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectCodeStyleSettingsManager">
<option name="PER_PROJECT_SETTINGS">
<value>
<JSCodeStyleSettings>
<option name="SPACE_BEFORE_PROPERTY_COLON" value="true" />
<option name="ALIGN_OBJECT_PROPERTIES" value="2" />
<option name="USE_DOUBLE_QUOTES" value="false" />
<option name="SPACES_WITHIN_OBJECT_LITERAL_BRACES" value="true" />
<option name="SPACES_WITHIN_IMPORTS" value="true" />
</JSCodeStyleSettings>
<codeStyleSettings language="JavaScript">
<option name="ALIGN_MULTILINE_CHAINED_METHODS" value="true" />
</codeStyleSettings>
</value>
</option>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
</component>
</project>
\ No newline at end of file
lambda-slack-example
====================
Code related to blog posts series on
[timbercode.pl]( http://timbercode.pl ) under
`lambda-slack` tag.
Structure
-----------
* [step-1]( step-1/ ) – scripts helpful while reading
[Lambda + Slack = health-check #1 — konfiguracja]( http://timbercode.pl/blog/2017/03/25/lambda-slack-health-check-1/ )
\ No newline at end of file
lambda-slack-example / step-1
=============================
In this directory you can find scripts helpful while reading
[Lambda + Slack = health-check #1 — konfiguracja]( http://timbercode.pl/blog/2017/03/25/lambda-slack-health-check-1/ )
blog post (polish language).
Slack webhook
-------------
To test your Slack webhook run:
```
$ ./test_slack_webhook.sh <your_slack_webhook_url_here>
```
AWS Lambda function
-------------------
You can find code of the function in
file [hello-to-slack.lambda.js]( hello-to-slack.lambda.js ).
Copy it's all content and paste on AWS Lambda as function body.
Remember to set your Slack webhook URL as `SLACK_WEBHOOK`
environment variable.
\ No newline at end of file
'use strict';
exports.handler = (event, context, callback) => {
sendMessageToSlackChannel('Hello from Lambda! :tada:')
.then(() => callback(null, 'OK'))
.catch(error => callback(error, null));
};
function sendMessageToSlackChannel(message) {
const requestData = JSON.stringify({
text : message
});
return makeHttpsRequest({
method : 'POST',
url : process.env.SLACK_WEBHOOK,
data : requestData,
headers : {
'Content-Type' : 'application/json',
'Content-Length' : requestData.length
}
});
}
function makeHttpsRequest({ method, url, headers = {}, data }) {
return new Promise((resolve, reject) => {
const parsedUrl = require('url').parse(url);
const nativeHttpLibrary = parsedUrl.protocol === 'https:' ? require('https') : require('http');
const request = nativeHttpLibrary.request({
hostname : parsedUrl.hostname,
port : parsedUrl.protocol === 'https:' ? 443 : 80,
path : parsedUrl.path,
method : method,
headers : headers
}, response => {
response.setEncoding('utf8');
let responseBody = '';
response.on('error', (error) => reject(error));
response.on('data', (chunk) => responseBody = responseBody + chunk);
response.on('end', () => resolve({
status : response.statusCode,
body : responseBody
}));
});
request.on('error', (error) => reject(error));
if (data) {
request.write(data);
}
request.end();
});
}
#!/usr/bin/env bash
slack_webhook=$1
if [ -z "${slack_webhook}" ]; then
echo "You have to provide Slack webhook URL as the first parameter"
exit 1
fi
curl \
-X POST \
-H 'Content-Type: application/json' \
--data '{ "text": "Hello!" }' \
${slack_webhook}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment