Unpacking request.parameters in a connector

Hi again, I have a simple question this time considering incoming get requests through a webhook. How do I unpack 'parameters': optional 'key value list', so I can use the values of the parameters individually?

The JSON file of the key value list looks as follows:

{
    "hub.mode": "subscribe",
    "hub.challenge": "123456789",
    "hub.verify_token": "token"
}

I tried this but that does not work:

	walk ^ $'request'.'parameters' as $ => {
		let $'mode' = $ .'hub.mode'
		let $'challenge' = $ .'hub.challenge'
		let $'token' = $ .'hub.verify_token'
	} || throw "error"

The parameters are optional, so the best way is to use something like this:

switch $'request'.'parameters' get ["hub.mode"] (
	| value as $ => {
		let $'hub.mode' = $
		// use the 'hub.mode' value
	}
	| error => {
		// handle a missing parameter
	}
)

However that becomes quite verbose when you have three parameters that are all required to proceed. So you can also do it like this:

try {
	let $'hub.mode' = ^ $'request'.'parameters' get [ "hub.mode" ] || throw "no hub.mode supplied"
	let $'hub.challenge' = ^ $'request'.'parameters' get [ "hub.challenge" ] || throw "no hub.challenge supplied"
	let $'hub.verify_token' = ^ $'request'.'parameters' get [ "hub.verify_token" ] || throw "no hub.verify_token supplied"

	// use the parameters
}
catch as $ => {
	// handle missing parameter(s)
}
1 Like

My webhook receives different kinds of data. Sometimes it includes statuses as a list, other times it includes contacts. I thought optional list would do the trick so, for example, the processor knows that 'statuses' might be unavailable and throw "no status found" when trying to define $‘status’. However, due to the unset state of 'statuses', I get an error instead.

let $'data' = ^ $'request'.'content' get 
    => call 'unicode'::'import' with ( $'encoding' = "UTF-8" ) 
    => parse as JSON 
    => decorate as {
        'entry': list {
            'changes': list {
                'value': {
                    'metadata': {
                        'display_phone_number': text
                        'phone_number_id': text
                    }
                    'statuses': optional list {
                        'id': text
                        'status': text
                        'timestamp': text
                        'recipient_id': text
                    }
                    'contacts': optional list {
                        'profile': {
                            'name': text
                        }
                        'wa_id': text
                    }
                }
            }
        }
    } || throw "unable to parse request data"

let $'data_JSON' = ^ $'request'.'content' => serialize as JSON
let $'name' = $'data'.'entry'[0].'changes'[0].'value'.'contacts' get [0].'profile'.'name' 
    || throw "no name text found"
let $'number' = $'data'.'entry'[0].'changes'[0].'value'.'contacts' get [0].'wa_id' 
    || throw "no number text found"
let $'status' = $'data'.'entry'[0].'changes'[0].'value'.'statuses' get [0].'status' 
    || throw "no status found"

To prevent the throw, you can use the switch value/error construction from my earlier message.

Or you can assign an empty string as fallback by doing this:

let $'status' = $'data'.'entry'[0].'changes'[0].'value'.'statuses' get [0].'status' || ""

However that might cause trouble further down the line as you can not see the difference between a missing value and a valid, empty string value.