Particle Publish Passing Arguments

I’m consolidating some of my webhooks, having hit the limit.

So, I’m trying to test a webhook where I pass an argument like this:

{
    "event": "custom_weather",
    "url": "http://api.wunderground.com/api/<myAPIkey/forecast10day/q/{{myState}}/{{myCity}}.json",
    "requestType": "POST",
	"headers": null,
	"query": null,
	"responseTemplate": "{{#forecast}}{{#simpleforecast}}{{#forecastday}}{{high.fahrenheit}}~{{low.fahrenheit}}~{{conditions}}~{{pop}}~{{/forecastday}}{{/simpleforecast}}{{/forecast}}",
	"json": null,
	"auth": null,
	"mydevices": true
}

so, in the CLI, how do I pass the argument?

pseudo:

particle publish myState=NJ myCity=Princeton

cannot find a lot on publish in the CLI docs:

so this works with the webhook above...

static unsigned long publishTimer = millis();
  if (millis() - publishTimer > 15000)
  {

    Spark.publish("custom_weather", "{ \"myState\": \"NJ\", \"myCity\": \"Princeton\" }", 60, PRIVATE);
    publishTimer = millis();

output:

{"name":"custom_weather","data":"{ "myState": "NJ", "myCity": "Princeton" }","ttl":"60","published_at":"2015-07-25T01:17:01.282Z","coreid":"redacted"}

{"name":"hook-response/custom_weather/0","data":""83~62~Partly Cloudy~0~89~66~Clear~0~90~69~Clear~20~88~69~Chance of a Thunderstorm~60~92~70~Clear~10~96~71~Clear~10~95~71~Clear~20~90~68~Chance of a Thunderstorm~30~89~67~Clear~10~90~67~Clear~10~"","ttl":"60","published_at":"2015-07-25T01:17:01.653Z","coreid":"null"}

but i'd like a way to check in the CLI.... is it possible?

You can do a particle subscribe mine

Yes @kennethlimcp

But to pass an argument to a publish as to debug it?

particle publish xxx ?

But if you use myDevices : true… it’s not going to work.

Not making my point, I guess. I want to test a webhook as above, using the CLI by passing an argument to my webhook.

Heh. publish in CLI was more like a beta hack together but you can try it. I see that in the CLI it takes in a data argument.

but it’s really like particle publish eventName data probably not as detailed as it can get.

Go to https://dashboard.particle.io/user/logs and try doing particle publish eventName data and look at the event. :smile:

OK I’m going to try re re-define my opportunity here. I have multiple devices doing the same thing, using different webhooks… and I’m running out. So I am trying to take the like ones and pass variables to give them more utility across multiple devices.

For example I’m (successfully) getting Sunrise/Sunset times like this:

{
	"event": "sunRiseSet",
	"url": "http://api.wunderground.com/api/<myApiKey>/astronomy/q/{{my-var}}.json",
	"requestType": "POST",
	"headers": null,
	"query": null,
	"responseTemplate": "{{#sun_phase}}{{sunrise.hour}}~{{sunrise.minute}}~{{sunset.hour}}~{{sunset.minute}}~{{/sun_phase}}",
	"json": null,
	"auth": null,
	"mydevices": true
}

where I use a publish like this:

Spark.publish("sunRiseSet", "{ \"my-var\": \"NJ/Princeton\", }", 60, PRIVATE);

BUT!!! the state/city is not a component of the JSON returned by Weather Underground, so I cannot parse it back to filter it in the handler!!! Therefor I cannot discriminate when using two devices; each passing the location argument to the webhook :frowning:

Placing the argument into the webhook response template e.g.:

{
	"event": "sunRiseSet",
	"url": "http://api.wunderground.com/api/<myApiKey>/astronomy/q/{{my-var}}.json",
	"requestType": "POST",
	"headers": null,
	"query": null,
	"responseTemplate": "{{my-var}}{{#sun_phase}}{{sunrise.hour}}~{{sunrise.minute}}~{{sunset.hour}}~{{sunset.minute}}~{{/sun_phase}}",
	"json": null,
	"auth": null,
	"mydevices": true
}

doesn’t seem to do the trick :frowning:

any advice on getting that argument into the response??

@kennethlimcp,

Would you know how to insert the {{my-var}} argument in my example above into my responseTepmplate so that I can build a filter on the webhook handler to only grab data from the webhook response that contains the argument?

Again, I am trying to build a single webhook that may be used by different users of the webhook, each passing their argument (location in this example) and getting a custom response.

Hmm how about using {{SPARK_EVENT_VALUE}}? https://docs.particle.io/guide/tools-and-features/webhooks/#templates

yeah, I've read that section 100 times at least...

where to insert {{SPARK_EVENT_VALUE}} ?

well, what do you know... it looks like @Dave was thinking about this problem a little while back...

using responseTopic would fit the bill here, for sure!

1 Like