Webhook responseTemplate not working correctly?

I’m trying to use a webhook to retrieve weather data from forecast.io. I have create the webhook and am able to publish it and get a response, but the response is always the full json text, rather than the filtered version I want using the responseTemplate I provided.

Assuming my own code was to blame, I tried an example I found here: Tutorial: Webhooks and Responses with Parsing (JSON, Mustache, Tokens)

Creating a webhook with the json from that page and testing it with particle-cli also yields the entire json rather than the mustache-filtered version.

I’ve tested my responseTemplate string using trymustache.com and it seems correct.

@Dave or @peekay123, is there an issue with the webhook responseTemplate field or is it just something I’m doing wrong?

1 Like

any chance you can share an example of the response you’re getting / the template you have written?

Thanks!
David

Sure thing. Here’s my webhook:

{
"event": "forecastio_webhook",
"url": "https://api.forecast.io/forecast/<api key>/45.464698,-98.486483",
"requestType": "GET",
"headers": {
	"Content-Type": "application/json"
},
"responseTemplate": "{{#currently}}{{temperature}}~{{humidity}}~{{pressure}}~{{precipProbability}}~{{windSpeed}}~{{/currently}}{{#alerts.0}}{{time}}~{{/alerts.0}}",
"query": "exclude=minutely,hourly,daily,flags",
"mydevices": true
}

And the response text is very long and identical to what you see if you browse to https://api.forecast.io/forecast/<api key>/45.464698,-98.486483

The webhook isn’t even adding the parameters I specify in “query” to the url when it makes the request (so I get the full text of minutely, hourly, daily and flags in my json).

@Dave, wondering if you’ve had a chance to check this out. I’m working on this as part of what I hope will be a Christmas present. Haha. At this point I’m thinking of doing the parsing using some intermediary and then using the webhook to retrieve that but I’d really like to just use the webhook directly. Thanks!

hey @bennettj1087 have you seen this it may be of help. I ended up using hook.io with this tutorial by @jvanier

1 Like

Thanks for the tutorial link. That would definitely work and I may have to go that route if I can’t get the webhook working the way it should. If really rather not have an extra piece of code hanging out at hook.io though since the webhook should really be able to do what I need. We’ll see. Hopefully I’ll get the webhook straightened out.

Thanks again for providing input. I’ve been following your thread for ideas also.

1 Like

FYI I’m using forecast.io through hook.io for my what should I wear clock. I just prefer understanding what happens when fetching and parsing the data instead of relying on the slightly mysterious webhook configuration. The code is open source if you want to look.

Hi @bennettj1087,

Sorry about my slow reply! I didn’t realize the urgency, lemme double-check this now… re-reading.

Thanks,
David

Aha, okay, so, your template is:

{{#currently}}{{temperature}}~{{humidity}}~{{pressure}}~{{precipProbability}}~{{windSpeed}}~{{/currently}}

{{#alerts.0}}{{time}}~{{/alerts.0}}

but the JSON is:

{
	"latitude": 45.464698,
	"longitude": -98.486483,
...
	"currently": {
		"time": ...,
}

So instead I think you want something like…

{{currently.temperature}}~{{currently.humidity}}~{{currently.pressure}}~{{currently.precipProbability}}~{{currently.windSpeed}}

I didn’t see “alerts” in there, but I hope that helps!

Thanks,
David

@Dave, thanks for the suggestion. My understanding of Mustache is that you use #currently to “open” the currently element at the root of the JSON and then you can reference all of the elements under it directly. Then you use /currently to “close” the currently section.

I previously tested this template on trymustache.com and this webhook is actually directly taken from a project another user made and I have confirmed that it is indeed working correctly for him.

Also, in addition to the responseTemplate not working, the webhook seems to also be ignoring what I have specified in the query parameters as well (“exclude=…” never gets appended to the request URL).

Also, just to clarify… if elements in the responseTemplate don’t match the structure of the JSON, what would the expected behavior be? Would you anticipate that the entire JSON would be returned? Or wouldn’t it just fail to find the elements specified in the template and not return anything?

I’m having the same problem. The responseTemplate just doesn’t do anything. I get the standard JSON instead.

I’m having the problem as well. I just spent the last few hours battling what I thought was a syntax error. Glad I searched here.

@J0nas and @mjr, thanks for chiming in. Hopefully with a handful of people having the same issue someone will be able to help us pinpoint the problem.

I ended up writing and hosting my own PHP script to access the Forecast.io API and return a formatted string.

I think the CLI already messes up the creation of a webhook with a responseTemplate but I don’t know how to debug that.

@J0nas, I’m actually just finishing my own PHP script to do the same.

@bennettj1087 the response template is a mustache template. If a key doesn’t match, that section will be blank. You could use an inverted section {{^person}} to insert content if person is missing. See here for the full spec.

Of course that doesn’t help you right now since webhook creation seems broken.

Mine are also ignoring the query and responseTemplate altogether. I’ve tried both ways:

{{#contacts}}{{surname}}{{/contacts}}

and

{{contacts.surname}}

I’ve noticed when I create the hook using the “particle webhook create” neither query or responseTemplate are echoed back to me, like the command is ignoring them.

I think using a 3rd party hook service or creating your own parser defeats the purpose of the device.

Looking at the createHook function, it is indeed ignoring query and responseTemplate:

var webhookData = {
	event: eventName,
	url: url,
	deviceid: deviceID,
	requestType: requestType || data.requestType,
	mydevices: data.mydevices === undefined ? true : data.mydevices
};

If you change the function to include those variables it will work:

	var webhookData = {
		event: eventName,
		url: url,
		deviceid: deviceID,
		responseTemplate: data.responseTemplate,
		query: data.query,
		requestType: requestType || data.requestType,
		mydevices: data.mydevices === undefined ? true : data.mydevices
	};
1 Like

Whoa, what!? When did that disappear!?

edit: playing the git blame game, brb.