Getting data from the Cloud (specifically webhooks to slack.com)

My electron doodad is now Particle.publishing to the cloud. I now need to Get? that data. Essentially I just need to write a program that can listen for and then pull from the cloud the simple text/string events as they are published by the device. I will need to be able to tell which device is sending a message. Not sure if I should try a google doc route and keep track of a status message by device id or exactly how to arrange the information… but big picture I need to get the data from the cloud to ultimately update a javascript app as the events happen . (which can be at irregular intervals, and the app will track a lot of these sensor reported events. I do not need to keep/store the data… only interested in the most recent status update for each sensor).

Based on searching through the forums and reading documentation… I think I’ll want to getEventStream doc_link ?
But I also see words like “JSON” - which I’m about to google…

Requesting advice on how to go about doing what I’m trying to do in a simple sensible way.

You first want to catch the server sent eventstream as you correctly noticed.
And only after you can catch that successfully, you go on to parsing the incoming data - this is where JSON would become interesting.

JSON is a convenient way to structure a data payload into key/value pairs and access each value by its key and the best part, that’s natively supported by JS.
Once you have a JSON object you can treat it like a struct and/or array.

1 Like

Have a look:

2 Likes

Thank you Gentlemen. You sent me in the right direction. After more researching - it turned out that the solution was a hybrid involving using webhooks to send a JSON to get to the backend.

1 Like

Webhooks to Slack.com will NOT work on default settings, so read on…
I updated the title of this thread to reflect the data movement I chose and provide a lessons learned about when I did it. Hopefully this leaves a useful thread.

  • On the slack side you need to create an app that listens for webhooks. Assuming you have a Slack.com channel…
    - First create a slack app CREATE_SLACK_APP.
    - Then make it listen for webhooks SLACK_WEBHOOKS
    • (this generates the URL that the webhook coming from the particle cloud needs)
  • Then create a webhook integration in Particle PARTICLE_WEBHOOK

Quality points for this webhook integration setup in Particle:

  • eventName: make sure it matches exactly the
    Particle.publish("eventName", ...) in your code

  • url: the URL provided by the slack app setup

  • Request type: POST

  • Request format: JSON

  • Device: set as you desire to the device to listen for

  • How to avoid HTTP 400 Bad Request invalid_payload (“no_text”):

    • slack_error_link

    • Error 400 will be returned by slack upon each incoming webhook packet from Particle. Slack is specifically looking for a field named “text” but Particle supplies a field named “data” instead so you need to go into the Advanced Settings menu (copy the entire default JSON struct) in the Particle webhook builder under the “Custom” selection (now paste) and in the text box and rename the “data” field “text”. Then Slack will print ONLY what is in that text field, it will not print the rest of the fields - though it does receive them.

    • If you want all the normal data in the JSON printed then put this in the custom text box: (you can then parse or format as desired in slack - see their webhook tutorial for formatting options) :
      { "event": "{{{PARTICLE_EVENT_NAME}}}",
      "text": "{{{PARTICLE_EVENT_NAME}}}, {{{PARTICLE_EVENT_VALUE}}}, {{{PARTICLE_DEVICE_ID}}}, {{{PARTICLE_PUBLISHED_AT}}} ",
      "coreid": "{{{PARTICLE_DEVICE_ID}}}",
      "published_at": "{{{PARTICLE_PUBLISHED_AT}}}" }

3 Likes