Return published data in Webhook Response Topic

Is it possible return part of the published data in a webhook response topic?

For example, I have a webhook that gets the forecast for a particular city from Wunderground. The call looks like this:

https://api.wunderground.com/api/my_key/forecast/q/{{state}}/{{city}}.json

I have multiple photons that may or may not want the data from any particular city. If there are two devices in the same city, and one of them triggers this webhook, it’d be nice if the other one could receive the data by subscribing a response event for that city. It might look something like this:

hook-response/getWeather/NewYorkCity

However, when I try to create a response topic like this:

hook-response/getWeather/{{city}}

I get hook-response/getWeather//0 with the city name omitted.

With those types of requests, I’m using the DeviceID to sort all that.

URL is called like this:

http://api.wunderground.com/api/yourAPIkeyHere/conditions/q/{{my-state}}/{{my-city}}.json

That is, a “common” webhook that expects a City and State is called, but you also pass along the Device ID.

the response topic then looks like this:

{{PARTICLE_DEVICE_ID}}_current_weather

so then just have the device with a handler that subscribes to itself:

  String deviceID = System.deviceID();
  deviceID.toCharArray(responseTopic,125);
  Particle.subscribe(responseTopic, webhookHandler, MY_DEVICES);

and sort it all in the handler:

void webhookHandler(const char *event, const char *data)
{
  if (strstr(event, "gmail_count"))
  {
    gotGmailFeed(event, data);
  }
  else if(strstr(event, "current_weather"))
  {
    gotWeather(event, data);
  }
  else if (strstr(event, "sun_time"))
  {
    gotSunTime(event, data);
  }
}

in essence, you are setting up a webhook to which only the caller will respond.

call it like this:

Particle.publish("current_weather", publishString, 60, PRIVATE);

where:

sprintf(publishString, "{\"my-city\": \"%s\", \"my-state\": \"%s\" }", cityLocation, stateLocation);

That works and if it’s the closest I can get than I’ll live with it, but if you had 100 devices in the same city, then potentially, only 1 of the devices needs to send the request and all of them can get the data.

It avoid redundant calls which would lessen the load on Particle’s servers and limit the number of calls to the API leading to lower costs.

then you can simply have the location in the response topic, instead. "I'll respond to any NewYork_NewYork_weather update."

Unfortunately, the API response doesn’t include the location in the JSON and I can’t access the variables that my photon published for the request.

I don't know what you are trying to say.

Sorry, that was confusing. I don’t have access to a {{city}} variable in the response topic from either the API response or the original data in the request so I can’t create a response topic that specifies the city.

The closest I have been able to get is to create a Response Topic of:

hook-response/getWeather/{{{PARTICLE_EVENT_VALUE}}}

which returns

"hook-response/getWeather/{\"city\":\"Albany\",\"state\":\"NY\"}/0"

if I try to further interrogate PARTICLE_EVENT_VALUE with:

hook-response/getWeather/{{{PARTICLE_EVENT_VALUE.city}}}

i get nothing:

"hook-response/getWeather//0"

can you just proxy the event through your own server?

That would be an option, but I don’t have a server at this time. I’ve been using google spreadsheet with a script acting as a psuedo-server and I may be able to script something there to do some further data parsing.

Ideally, the mustache templating could handle this and keep things simple (and not force me to maintain additional code) but if the functionality isn’t there, than I suppose I’ll have to take a differenct tact.

if you're talking about Photons, then the juice probably isn't worth the squeeze. if you are trying to manage data for an Electron, yeh, you've got to find an approach that works for that.

Could you not first fire a webhook to get the location of the device via Google Maps, perhaps long and lat, and then make that the device’s responsetopic? No guarantees of accuracy or that several devices won’t have the same long-lat but it might reduce the overlap considerably.

That might work. How would I get that data into the ResponseTopic though? As far as I know, you can only parse the returned JSON from the webhook or use the predefined PARTICLE variables without parsing.