How to pass multiple data items from Particle device to webhook


#1

Hi. I have managed to get webhooks working from events triggered in my Photon to send an SMS.

The background is that the SMS server I am using needs an authorisation token that expires each hour to accept the SMS message. This means I need to query the server with Webhook#1 (GetToken), extract the token in code on the Photon, then via a second webhook (SendSMS), send an SMS, with the event data including the token string. Sort of a two stage process each time I want to trigger an SMS.

I have managed this all ok (that sounded a bit like a solo triumph, when it actually was a team effort - with lots of help given by the regular helpers on this forum…).

My question though is - how can I get the webhook to dissect and understand the parts of a data string in the particle.publish event, which contains both the extracted Token from a previous webhook POST as well as a variable message body (I imagine I’ll create the composite data string with a sprintf statement)? Is this possible? Is this through extraction at the webhook via a set of JSON statements?

Best regards to all Par-ticklers around the world…


#2

Yes, it’s possible if you encode the string you send in Particle.publish() as JSON.


#3

Thanks @rickkas7. That is a great tutorial. I haven’t yet put it in to practice but you have explained it very well. I will get started soon.


#4

Hi @rickkas7. I tried following the tutorial. But have hit a hurdle. I explained the general background to what I am trying to do in my first post. I have various debug events and variables that I am pretty sure the rest of the code is OK. The problem is just in the parsing of my data using mustache templatesisnt occurring.
Any help appreciated. Thanks.

After reading your tutorial, this is what I have:

Event/data code in Photon:

  Particle.publish("GetToken","",60,PRIVATE);
  delay(10000); //Wait for token to be received.
  if (token.length() > 0)  {
    String message = "{\"SMSTokString\": \"Bearer ";
    message.concat(token); \\token is returned from remote server, its a long string.  Remote server expects this returned in form "Bearer <token>"
    message.concat("\" \"body\": \""); 
    message.concat(body);\\this is the variable message body according to status of real world
    message.concat("\"}");
    //Particle.publish("message", message);
    Particle.publish("SendSMS",message,60,PRIVATE); //Create new event linked to webhook to send SMS via Telstra server

The webhook JSON text file I use to create the SendSMS webhook looks like this:

{
"event": "SendSMS",
"url": "http://requestb.in/zgsys5zg",
"requestType": "POST",
"headers": {
	"Authorization": "{{SMSTokString}}"
	}, 
"json": {
 	"to": "my cell number",
       	"body": "{{body}}"

    },
"mydevices": true,
"noDefaults": true
}

The webhook creates OK at the CLI with this response:

Sending webhook request  { uri: '/v1/webhooks',
  method: 'POST',
  json:
   { event: 'SendSMS',
     url: 'http://requestb.in/zgsys5zg',
     deviceid: undefined,
     requestType: 'POST',
     mydevices: true,
     headers: { Authorization: '{{SMSTokString}}' },
     json: { to: 'my cell number', body: '{{body}}' },
     noDefaults: true },
  headers: { Authorization: 'Bearer e8cdlongtokenstringbf2d319c7f' } }
Successfully created webhook with ID 57cb6.........c49d690

This just with URL to requestb.in for debugging - not to eventual target server.
At run time I get no errors or issues.
At requestb.in, it looks like (there is no parsing of my “Authorization” string into the field:

http://requestb.in
POST /zgsys5zg  application/json
 29 bytes 1m ago  
From 54.236.213.127, 173.245.56.170
FORM/POST PARAMETERS

None
HEADERS

Content-Type: application/json
Accept-Encoding: gzip
Host: requestb.in
Via: 1.1 vegur
Cf-Visitor: {"scheme":"http"}
User-Agent: ParticleBot/1.1 (https://docs.particle.io/webhooks)
Authorization:
X-Request-Id: ea61e29d-284d-46a4-b101-3a76ffdd5b08
Cf-Connecting-Ip: 54.236.213.127
Accept: application/json
Connection: close
Connect-Time: 1
Cf-Ipcountry: US
Content-Length: 29
Total-Route-Time: 0
Cf-Ray: 2dcd66c53227241a-IAD
RAW BODY

{"body":"","to":"my cell number"}

#5

I should have added the event response at Particle console. It looks like this:

{
  "event": "SendSMS",
  "data": "{\"SMSTokString\": \"Bearer FUEu4yAlongtokenstringoaBznY\" \"body\": \"My test string.\"}",
  "published_at": "2016-09-04T01:18:55.591Z",
  "coreid": "350043001547353236343033"
}

#6

Aren’t you missing a comma between the two key/value pairs?


#7

Thanks @ScruffR. I added the comma, but no change to the result in RequestBin.
Event text under webhook in Particle Console now reads:

{
“event”: “SendSMS”,
“data”: “{“SMSTokString”: “Bearer dK0longtokenstringCFl4Xd”, “body”: “My Test Message”}”,
“published_at”: “2016-09-04T06:47:41.793Z”,
“coreid”: “350…43033”
}


#8

@ScruffR you picked it. Thanks a lot. :+1: :clap: Thats what it was. A missing comma. I dont know how I missed the fact that the correction worked first time (maybe forgot to refresh request.bin)… oops. Thanks again to @rickkas7 for the tutorial.


#9

I was just banging my head why this wouldn’t work after the correction - thanks for putting me out of my misery :wink:


#10

@ScruffR, sorry for the bad call after trying your suggestion/correction. I came back to this after a break, and there it was busily logging to request.bin working beautifully…!! … :flushed: Really appreciate all your good advice and help. You really are a mainstay in this forum and must give a lot of your time to it. Thanks again!!

Sorry threw in a bit of sailing jargon. Mainstay = something keeping the mast in a vertical position ie very important to forward progress…


#11

Just googled that :wink: