phpxmppfacebook-chat

How to send facebook chat message via JAXL?


I am trying to send direct message with JAXL in the name of a user (authenticated application with xmpp_login). In the jaxl facebook chat example there exist a message echo structure, still it redirects received XMPPStanza to the sender. But trying to generate XMPPStanza to send performs nothing with

$client->send($stanza);

here is my XMPPStanza initialization code

$stanza = new XMPPMsg(null);
$stanza->from = $client->full_jid->to_string();
$stanza->to = 'example-facebook-id@chat.facebook.com';
$stanza->type = 'chat';
$stanza->body = 'test message';
$client->send($stanza);

To sum it up, How can i send message from server in the name of the client via JAXL?

EDIT

I forgot to mention i am using JAXL v3 and thought about appending full code and system configuration would be more helpful.

<?php

define('FACEBOOKBOT_PHP', TRUE);


// Include XMPP Engine 'JAXL'
// Path might be alter in time.
// require_once ROOT_DIR.'/JAXL/jaxl.php';
require_once '/var/www/JAXL/jaxl.php';
require_once '/var/www/JAXL/xmpp/xmpp_msg.php';


function sendMessage($client)
{

    //  // $stanza = new XMPPMsg(null);
//  // $stanza->from    = $client->full_jid->to_string();
//  // $stanza->to      = $to;
//  // $stanza->type    = 'chat';
//  // $stanza->body    = 'test message 1';

//  // foreach ($stanza->childrens as $value)
//  // {
//  //  $value->ns = 'jabber:client';
//  // }

//  // $client->send($stanza);

    $msg = new XMPPMsg(array('to'=>'example-user-id-and-it-got-to-work-i-checked-on-below-echo-stanza@chat.facebook.com'), 'test message');
    $client->send($msg);

    _info("test messages sent");
}


$user = 'gokhanbarisaker'; // my user id (www.facebook.com/gokhanbarisaker)
// $user = $argv[1];    // User name or facebook id
$jidSuffix = '@chat.facebook.com';  // Facebook chat account suffix
$appKey = 'example-app-key';    // Taken from developer.facebook.com
// $appKey = $argv[2];  // Facebook app token
$accessToken = 'example-access-token';  // Facebook user token - tried both app token tool on developer.facebook.com and token provided after user login both posses xmpp-login permission
// $accessToken = $argv[3];

$client = new JAXL( array(  // (required) credentials
                            'jid' => $user.$jidSuffix,
                            'fb_app_key' => $appKey,
                            'fb_access_token' => $accessToken,

                            // force tls (facebook require this now)
                            'force_tls' => true,
                            // (required) force facebook oauth
                            'auth_type' => 'X-FACEBOOK-PLATFORM',

                            // (optional)
                            //'resource' => 'resource',

                            'log_level' => JAXL_INFO,
                            'priv_dir'  => '.'
                        ));

$client->add_cb('on_auth_success', function()
{
    global $client;
    _info("got on_auth_success cb, jid ".$client->full_jid->to_string());
    $client->set_status("available!", "dnd", 10);

    // Here is the part where i tried to send message. In addition, i tried to call this function wherever i can on the code.
    sendMessage($client);
});

$client->add_cb('on_auth_failure', function($reason)
{
    global $client;
    $client->send_end_stream();
    _info("got on_auth_failure cb with reason $reason");

});

$client->add_cb('on_chat_message', function($stanza)
{
    global $client;

    // echo back incoming message stanza
    $stanza->to = $stanza->from;
    $stanza->from = $client->full_jid->to_string();
    $client->send($stanza);

    _info("echo message sent");

    sendMessage($client);
});

$client->add_cb('on_disconnect', function()
{
    _info("got on_disconnect cb");
});

$client->start();

echo "done\n";

?>

System configuration:

Terminal command to execute;


Solution

  • A few things you should try and see if they help you debug this:

    1. Set 'log_level' => JAXL_DEBUG, and check logs to see what exactly Jaxl sends out to facebook. If you can, update the question with outgoing stanza so that we can verify whats really wrong here.
    2. If you are using facebook id's to send out messages, remember you need to send message 'to'=>'-4@chat.facebook.com'. Note the negative sign.
    3. Facebook is quite smart in catching spammers in their network. If you are trying to send out too many similar messages within a short time, facebook servers will reply back with appropriate error to the bot (they can even disable your chat account temporarily). Check the logs to see if you are gettings any such error response back from the facebook servers.