Search code examples
javascriptfacebook

Send message with Facebook Javascript API


I have already included this in my page html:

<div id="fb-root"></div>
<script type="text/javascript" language="javascript">
    (function(d, s, id) {
      var js, fjs = d.getElementsByTagName(s)[0];
      if (d.getElementById(id)) return;
      js = d.createElement(s); js.id = id;
      js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
</script>

And that I have facebook-like box in my page already, however I need to do now a way to send messages to email using the Facebook Javascript API. Is this configuration enough to call a "send message"?

I have a custom HTML form in which when I click send I like it to send a message on my Facebook page's behalf. I have my page up in Facebook.

  • I need to send message without any server side component (pure Javascript)
  • I have a existing Facebook page (which I will use to send messages with)
  • I have a custom form in which will be used to type in message.
  • The message will be sent to some specific email address
  • I can send to any email with Facebook so I assume this will work

Solution

  • I will answer your questions separately:

    • I need to send message without any server side component (pure Javascript)

    If it can be a simple message to a facebook user, you have to use the FB.ui send dialog: https://developers.facebook.com/docs/reference/dialogs/send/ That´s pretty much the only way for you, you can just try putting in an email address in the popup. According to the documentation, it should work.

    • I have a custom form in which will be used to type in message.

    That´s where even the send dialog will not work anymore. You cannot prefill the message parameter, what you want would only be possible with the PHP SDK of Facebook. But then again: Why would you program the textfield on your own, if Facebook already offers everything in that dialog?

    Without that dialog, there is no way to just send a message in the background without a server side language like PHP.