Skip to content

Instantly share code, notes, and snippets.

@salihgueler
Created August 31, 2023 10:42
Show Gist options
  • Save salihgueler/e9be9fa6cc9ece30ba05764146850f74 to your computer and use it in GitHub Desktop.
Save salihgueler/e9be9fa6cc9ece30ba05764146850f74 to your computer and use it in GitHub Desktop.
lambda_for_push_notifications
const { PinpointClient, CreateCampaignCommand } = require("@aws-sdk/client-pinpoint");
const pinpointClient = new PinpointClient({ region: "eu-central-1" });
/*
* @type { import('@types/aws-lambda').APIGatewayProxyHandler }
*/
exports.handler = async (event) => {
console.log(`EVENT: ${JSON.stringify(event)}`);
for (const record of event.Records) {
if (record.eventName === 'INSERT') {
try {
let recipeTitle = record.dynamodb.NewImage.title.S;
let recipeDescription = record.dynamodb.NewImage.description.S;
let recipeId = record.dynamodb.NewImage.id.S;
const createCampaingCommand = new CreateCampaignCommand({
ApplicationId: "<APP-ID>",
WriteCampaignRequest: {
AdditionalTreatments: [],
Name: "Campaign: " + recipeId.substring(0, 50) + "!",
SegmentId: "<SEGMENT-ID>",
SegmentVersion: 1,
HoldoutPercent: 0,
TemplateConfiguration: {},
Limits: {
MessagesPerSecond: 20000,
MaximumDuration: 10800,
Daily: 0,
Total: 0,
},
Schedule: {
IsLocalTime: false,
QuietTime: {},
StartTime: "IMMEDIATE",
Timezone: "UTC",
},
MessageConfiguration: {
DefaultMessage: {
Action: "OPEN_APP",
Title: "New Recipe!",
Body: "Check out: " + recipeTitle + "!",
JsonBody: JSON.stringify({
recipeId: recipeId,
recipeTitle: recipeTitle,
recipeDescription: recipeDescription,
}),
},
APNSMessage: {
Action: "OPEN_APP",
Title: "New Recipe!",
Body: "Check out: " + recipeTitle + "!",
JsonBody: JSON.stringify({
recipeId: recipeId,
recipeTitle: recipeTitle,
recipeDescription: recipeDescription,
}),
},
GCMMessage: {
Action: "OPEN_APP",
Title: "New Recipe!",
Body: "Check out: " + recipeTitle + "!",
JsonBody: JSON.stringify({
recipeId: recipeId,
recipeTitle: recipeTitle,
recipeDescription: recipeDescription,
}),
},
BaiduMessage: {
Action: "OPEN_APP",
Title: "New Recipe!",
Body: "Check out: " + recipeTitle + "!",
JsonBody: JSON.stringify({
recipeId: recipeId,
recipeTitle: recipeTitle,
recipeDescription: recipeDescription,
}),
},
ADMMessage: {
Action: "OPEN_APP",
Title: "New Recipe!",
Body: "Check out: " + recipeTitle + "!",
JsonBody: JSON.stringify({
recipeId: recipeId,
recipeTitle: recipeTitle,
recipeDescription: recipeDescription,
}),
}
}
},
});
const data = await pinpointClient.send(createCampaingCommand);
console.log('Send Message Respond: %j', data);
} catch (error) {
console.log('Pinpoint error: %j', error);
} finally {
console.log('Pinpoint finally finished.');
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment