Search code examples
twiliotwilio-functions

getting a value from a twilio webhook


I'm trying to get "my test message" from this webhook event data. I'm kinda new to this but below is what I've tried

webhook received... 
{"interactionDateUpdated":"2021-12-18T18:08:34.222Z","interactionData":"{\"body\":\"my test message\"}","interactionDateCreated":"2021-12-18T18:08:34.222Z","interactionType":"Message",....

my code....

const interactionType = event.interactionType;
  console.log("interaction type: ", interactionType)
//interaction type: Message (as expected)
  
  const eventText = event.interactionData;
  console.log("event text: ", eventText);
//event text: {"body":"my test message"}

  const message = Object.values(eventText);
  console.log("message: ", message)
//message: [ '{', '"', 'b', 'o', 'd', 'y', '"', ':', '"', 'm', 'y', ' ', 't', 'e', 's', 't', ' ', 'm', 'e', 's', 's', 'a', 'g', 'e', '"', '}' ] (I'm trying to get "my test message")

also tried this
  const messageBody = eventText["body"]
  console.log("message body?: ", messageBody)
//message body?: null (I'm trying to get "my test message")

I'm clearly not understanding correctly how to get this.

Appreciate it if someone could point out what i'm missing.

Thanks, Jim


Solution

  • You have to parse it the string to object before extracting value with Object.values

    const message = Object.values(JSON.parse(eventText));