Webhook Event Handler Being Ignored

I have a problem with the event handler of Spark.publish().

Even though I can receive the webhook response through a command line(using particle publish and particle subscribe mine
, I cannot use it in my code. It doesn’t even go in to the event handler. I was waiting it for 10 minutes, but it still doesn’t work.

Here is my code.

#include "SparkButton/SparkButton.h"
SparkButton b = SparkButton();

String ret("");

void twitHandler(const char *name, const char *data)
{
    ret = data;
    Serial.println(name);
    Serial.println(data);
}

void setup()
{
    b.begin();
    Serial.begin(9600);
    while (!Serial.available()){
      SPARK_WLAN_Loop();
    }
    Serial.println("setup start!");
    Spark.subscribe("hook-response/twitter", twitHandler);
    delay(1000);
}

void loop() {
    Serial.println("twitter starts!");
    Spark.publish("twitter");
    Serial.println("twitter ends!");
    delay(180000);
}

Just to make sure, you do get the other printout (e.g. “setup start!”, …)?

Yeah, I tried to print other words in handler function, and use spark button either, but it didn’t work.

+Oh I misunderstood your question… I got “setup start!” from serial monitor…

The part above waits for you to type a key on the keyboard of the serial terminal emulator, looping forever until you do.

So I wasn't sure I understood your answer to @ScruffR's question: Do you get any output on the serial terminal over USB?

1 Like

Yeah. I got “setup start!”, “twitter starts!” and “twitter ends!” from serial monitor.
The thing that doesn’t work is only the event handler.

Your webhook is definetly working correctly?

Since you publish twitter and subscribe to hook-response/twitter a not working webhook could explain that behaviour too but could not be pinned down by just looking at your code.

Maybe you could also show how you’ve set up your webhook.

  {
    "event": "twitter",
    "url": "https://api.twitter.com/1.1/search/tweets.json?q=%40MY_TWIT_ID&count=1",
    "requestType": "GET",
    "headers": {
      "Authorization": "Bearer XXX"
    },
    "mydevices": true
  }

This is the json file of my webhook, and I can see the response of webhook when my board is running by using particle subscribe mine in command line.

I'm having the same problem. I can see the hook-response on the command line and in the dashboard but cannot subscribe to it.

void setup() {
Spark.subscribe("hook-response/pulltext_/0", hookResonseHandler);
}

void loop() {

}

void hookResonseHandler(const char *event, const char *data)
{
Spark.publish("hello_", String(data));
}

You subscribe “pulltext_”, but publish “hello_”.

Anyway, my problem was about the network.

Sorry, I pulled the failing process out of a larger body of code... I've should have provided more detail.

I'm using IFTTT to trigger an event named "sunrise" at sunrise every morning. A device subscribed to "sunrise" handles the event and publishes an event named "pulltext_" which triggers a webhook. The same device subscribes to "hook-response/pulltext_/0" which is looking for the response from the webhook. I'm simply publishing "hello_" in the handler so I can easily monitor if the handler gets called using "particle subscribe mine" on the CLI. However, the handler never executes (ie. I never see "hello_" come across as an event).

void setup() {

Spark.subscribe("sunrise", weatherHandler, MY_DEVICES);
Spark.subscribe("hook-response", hookResonseHandler);

}

void loop() {

}

void weatherHandler(const char *event, const char *data)
{
Spark.publish("pulltext_", String(data));
digitalWrite(D7, HIGH);
delay(3000);
digitalWrite(D7, LOW);
delay(1000);
}

void hookResonseHandler(const char *event, const char *data)
{
Spark.publish("hello_", String(data));
}