Jump to content


Photo

Reading a JSON POST

json post messenger bot params parameters

  • Please log in to reply
3 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?



#2 Nico Wouterse

Nico Wouterse
  • Members
  • 197 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
  • Members
  • 197 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






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

IPB Skin By Virteq