0
votes

I have a lambda function in AWS. I use it to retrieve information from a REST API. When I test it runs returns a 200 status code, but an "ERROR TypeError: Cannot read property 'Message' of undefined at smsResponder (/var/task/smsResponder.js:33:35)" also shows. I have googled, and tried to use .responseText.

My code is below. Should I be using return or something of the sort?

'use strict'

const AWS = require('aws-sdk')
AWS.config.update({ region: process.env.AWS_REGION || 'us-east-1' })

const { getStock } = require('./getStock')
const KEYWORD = 'stock'

const validateStock = function (elementValue){
  let stockTest = AAPL
   return stockTest.test(elementValue)
}

const sendSMS = async function (params) {
    const pinpoint = new AWS.Pinpoint()
    console.log('sendSMS called: ', params)

    return new Promise((resolve, reject) => {
        pinpoint.sendMessages(params, function(err, data) {
            if(err) {
                console.error(err)
                reject(err)
            } else {
                console.log("Message sent. Data: ", data)
                resolve(data)
            }
        })
    })
}

const smsResponder = async (event) => {

    const msg = JSON.parse(event.Sns.Message)
    const msgWords = msg.messageBody.split(" ")

    // Check the first word of the text message is the keyword
    if (msgWords[0].toLowerCase() !== KEYWORD) return console.log('No keyword found - exiting')

    // Validate stock name and get price
    let message =''
    const stockCode = msgWords[1]

    if (validateStock(stockCode)) {
        message = await getStock(stockCode)
    } else {
        message = 'Invalid stock symbol - text me in the format "stock stocksymbol".'
    }

    // Send the SMS response
    const params = {
        ApplicationId: process.env.ApplicationId,
        MessageRequest: {
            Addresses: {
                [msg.originationNumber]: {
                    ChannelType: 'SMS'
                }
            },
            MessageConfiguration: {
                SMSMessage: {
                    Body: message,
                    MessageType: 'PROMOTIONAL',
                    OriginationNumber: msg.destinationNumber
                }
            }
        }
    }

    return console.log(await sendSMS(params))
}


module.exports = { smsResponder }
1
It seems that in smsResponder, event.Sns is undefined, so event doesn't have an Sns property. - Luke Woodward

1 Answers

1
votes

The SNS-Event is differently structured, it should be event.Records[0].Sns.Message .

Here are the docs:

https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html