amazon-web-servicesaws-lambdaaws-pinpoint

Example lambda function(s) for Campaign hook pinpoint


I was working on something that would modify my promotional sms messages. I read that it is possible via CampaignHook in Pinpoint. But from the documentation, I couldn't gather how this will actually work. I have followed it until adding permission and linking the pinpoint app id and with it. I have followed this link: https://github.com/Ryanjlowe/lambda-powered-pinpoint-templates

For some reason, I am not able to follow what I need to do on the Lambda (boto3) function side to try and make this work. Is there an example code (python) or well-documented example for this? It would help me a lot. Thanks!


Solution

  • The Pinpoint developer guide describes how to setup a CampaignHook under the chapter "Customizing segments with AWS Lambda".

    https://docs.aws.amazon.com/pinpoint/latest/developerguide/segments-dynamic.html

    "To assign a Lambda function to a campaign, you define the campaign's CampaignHook settings by using the Campaign resource in the Amazon Pinpoint API. These settings include the Lambda function name. They also include the CampaignHook mode, which specifies whether Amazon Pinpoint receives a return value from the function."

    The docs show an example Lambda function:

    'use strict';
     
    exports.handler = (event, context, callback) => {
        for (var key in event.Endpoints) {
            if (event.Endpoints.hasOwnProperty(key)) {
                var endpoint = event.Endpoints[key];
                var attr = endpoint.Attributes;
                if (!attr) {
                    attr = {};
                    endpoint.Attributes = attr;
                }
                attr["CreditScore"] = [ Math.floor(Math.random() * 200) + 650];
            }
        }
        console.log("Received event:", JSON.stringify(event, null, 2));
        callback(null, event.Endpoints);
    };
    
    

    "In this example, the handler iterates through each endpoint in the event.Endpoints object, and it adds a new attribute, CreditScore, to the endpoint. The value of the CreditScore attribute is simply a random number."