Skip to content

Instantly share code, notes, and snippets.

View joawan's full-sized avatar

Joakim Wånggren joawan

View GitHub Profile
@joawan
joawan / handler.js
Created April 27, 2022 08:51
Configuration of SES event destination
const response = require('cfn-response');
const AWS = require('aws-sdk');
const propertiesToParams = (props) => ({
ConfigurationSetName: props.ConfigSetName,
EventDestination: {
Name: props.ConfigSetEventDestinationName,
Enabled: props.Enabled || true,
MatchingEventTypes: ['send', 'reject', 'bounce', 'complaint', 'delivery', 'open'],
SNSDestination: {
@joawan
joawan / p1.js
Created December 1, 2021 11:41
adventofcode2021
const fs = require('fs');
const file = fs.readFileSync(`./${process.argv[2]}`);
const numbers = file.toString().split("\n").map(e => +e);
const increases = numbers.map((n, i, a) => n > a[i-1]);
const count = increases.filter(e => e).length;
console.log(count);
@joawan
joawan / pagerduty.js
Created October 29, 2021 10:11
Get oncall from PagerDuty
const getOncallForSchedule = async (id, pd) => {
const userOnCall = await pd.schedules.listUsersOnCall(id, {
time_zone: 'UTC',
since: new Date().toISOString(),
until: new Date(Date.now() + 1000).toISOString(),
});
const user = JSON.parse(userOnCall.body).users.pop();
const contactMethods = await pd.users.listContactMethods(user.id);
const userDetails = JSON.parse(contactMethods.body);
@joawan
joawan / verify.js
Created October 29, 2021 10:07
Verify Twilio signatures
const qs = require('querystring');
const twilio = require('twilio');
module.exports.process = async (event, config) => {
const { 'X-Forwarded-Proto': proto, 'X-Twilio-Signature': sign } = event.headers;
const { path, domainName } = event.requestContext;
const url = `${proto}://${domainName}${path}`;
if (!twilio.validateRequest(config.twilioToken, sign, url, qs.parse(event.body))) {
throw new Error('Invalid signature');
@joawan
joawan / serverless.yml
Created October 29, 2021 10:04
Serverless fil
service: sls-oncall-twilio
provider:
name: aws
runtime: nodejs14.x
stage: ${opt:stage, 'dev'}
region: ${opt:region, 'eu-west-1'}
timeout: 10
memorySize: 256
logRetentionInDays: 14
@joawan
joawan / handler.js
Created September 16, 2021 08:55
Grabbing contact for people oncall
const getOncallForSchedule = async (team, id, pd) => {
const userOnCall = await pd.schedules.listUsersOnCall(id, {
time_zone: 'UTC',
since: new Date().toISOString(),
until: new Date(Date.now() + 1000).toISOString(),
});
const user = JSON.parse(userOnCall.body).users.pop();
const contactMethods = await pd.users.listContactMethods(user.id);
const userDetails = JSON.parse(contactMethods.body);
@joawan
joawan / handler.js
Created September 16, 2021 08:41
Grabbing matching teams from input
const teamsFromText = (text, teams) => {
const tags = teams.reduce((acc, team) => [...acc, ...team.tags, team.name], []);
const keywords = text.toLowerCase().split(' ').filter((word) => tags.includes(word));
if (!keywords.length) {
const names = teams.map((team) => team.name).join(' ');
throw new Error(`Don't know any team to match that too. Available teams are \`${names}\`.`);
}
return teams.filter((team) => [team.name, ...team.tags].some((tag) => keywords.includes(tag)));
@joawan
joawan / message_verification.js
Created September 14, 2021 20:04
Method to verify slack messages
const crypto = require('crypto');
const signSecret = process.env.SLACK_SIGN_SECRET;
const validateRequest = (requestSignature, requestTime, rawBody, validFor = 300) => {
const requestValidFrom = Math.floor(Date.now() / 1000) - validFor;
if (requestTime < requestValidFrom) {
throw new Error(`Request outdated: !(${requestTime} < ${requestValidFrom})`);
}
@joawan
joawan / handler.js
Last active September 14, 2021 17:51
Handler with callback to Slack
const { WebClient } = require('@slack/web-api');
const wc = new WebClient(process.env.SLACK_TOKEN);
const handler = async (event) => {
console.log('Received request', event);
const { body } = event;
if (body.challenge) {
return {
@joawan
joawan / handler.js
Created September 14, 2021 13:12
Initial handler
const handler = async (event) => {
console.log('Received request', event);
const body = JSON.parse(event.body);
if (body.challenge) {
return {
statusCode: 200,
headers: { 'Content-Type': 'text/plain' },
body: body.challenge
};