Jump to content


Photo

Reading a JSON POST

json post messenger bot params parameters

  • Please log in to reply
5 replies to this topic

#1 COMFIED

COMFIED
  • Members
  • 44 posts

Posted 08 April 2017 - 03:28 PM

I'm trying to build a Facebook messenger bot.  It requires reading and sending JSON data from messenger.

 

Sending a json post message is no problem.

Anyone with an idea on how to READ incoming JSON POST data using SMARTMOBILESTUDIO?  In other words can a SMARTMOBILESTUDIO html webapp work as a webhook?


  • UternRax likes this

#2 Nico Wouterse

Nico Wouterse
  • Moderators
  • 233 posts
  • LocationAustralia

Posted 09 April 2017 - 09:57 AM

You'll have to work with some limiting conditions if you want to do this

 

For one, post-ing means you'll need a server side component to handle that

so node.js or php, not client-side 

 

Also Facebook requires you to set up both get and post mechanisms and I think they enforce https as well

 

I assume you're familiar with posts like this

http://www.girliemac...iai-bot-nodejs/

which detail how to make a webhook enabled facebook bot

 

To answer your question

I think ... it would be possible to use Smart Pascal to produce your node.js webhook handler (webhook.js)

I haven't done it myself though.

 

Its an interesting project,

Keep us informed if you like


  • COMFIED likes this
Nico Wouterse

#3 COMFIED

COMFIED
  • Members
  • 44 posts

Posted 09 April 2017 - 10:22 AM

Thanks Nico

 

I guess the trick is to get Smart Mobile Studio to read contents of a POST from https://graph.facebo...2.6/me/messages



#4 Nico Wouterse

Nico Wouterse
  • Moderators
  • 233 posts
  • LocationAustralia

Posted 09 April 2017 - 01:48 PM

A normal ajax request looking like this

  FHttp := TW3HttpRequest.Create; 
  FHttp.OnDataReady := RetrieveInfo;
  FHttp.open("POST","https://graph.facebook.com/v2.6/me/messages");
  FHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  FHttp.send();
end;
 
procedure TForm1.RetrieveInfo(Sender: TW3HttpRequest);
begin
var messagecursor : Variant;

  console.log(FHttp.ResponseText);
  messagecursor := JSON.parse(FHttp.ResponseText);
  console.log(messagecursor.error.message);
  console.log(messagecursor.error.type);
end;
 

or alternatively using the W3C.XMLHttpRequest syntax :

var FHttp := JXMLHttpRequest.Create;  
FHttp.open("POST","https://graph.facebook.com/v2.6/me/messages"); 
FHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");  
FHttp.send();  
 
var messagecursor : Variant;  
 
JGlobalEventHandlers(FHttp).onLoad := lambda(e:JEvent)  
  console.log(FHttp.ResponseText);  
  messagecursor := JSON.parse(FHttp.ResponseText);  
  console.log(messagecursor.error.message);  
  console.log(messagecursor.error.type); end;

gives you access to whatever is outputted by the graph url

 

It is not a webhook though, as you're trying to read facebook messages initiated from the client rather than initiated by facebook


Nico Wouterse

#5 Czar

Czar
  • Members
  • 131 posts

Posted 22 May 2017 - 01:25 AM

We have been using a newer version of the RTL

 

This code used to work
REST['http://md5.jsontest.com'''].Post('text=' + source, [], [], LogHttpDetails).OnDone(LogMD5);

 

we changed it to 

 

REST.Api['http://md5.jsontest.com'''].call.Post('text=' + 'asd').OnDone(LogMD5);

 

to seemingly work with the new RTL

 

however, it does not appear to post correctly as the ondone procedure returns undefined error

 

procedure TForm1.LogMD5(http: TW3HttpRequest);
begin
  var resp := TMD5Response(JSON.Parse(http.ResponseText));
  Log("MD5('" + resp.original + "') = " + resp.md5);
end;
 
Comes back as an error. 


#6 Czar

Czar
  • Members
  • 131 posts

Posted 22 May 2017 - 01:03 PM

Yes, Jon has been ill all weekend.

It makes sense to concentrate questions and answers on this forum as it the place people will find everything.

We are using SMS for an on-boarding application for new customers - we hope to get a feel for what we can expect from SMS as a whole.







Also tagged with one or more of these keywords: json, post, messenger, bot, params, parameters

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users