Jump to content
Sign in to follow this  
COMFIED

Reading a JSON POST

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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.com/blog/2017/01/06/facebook-apiai-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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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. 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×