如何通过JAXL发送Facebook聊天消息?

时间:2013-01-15 13:17:54

标签: php xmpp facebook-chat

我正在尝试使用JAXL以用户名(使用xmpp_login进行身份验证的应用程序)发送直接消息。在jaxl facebook chat example中存在消息回显结构,它仍然将收到的XMPPStanza重定向到发送方。但是尝试生成XMPPStanza以发送

并不执行任何操作
$client->send($stanza);

这是我的XMPPStanza初始化代码

$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);

总结一下,如何通过JAXL以客户端的名义从服务器发送消息?

修改

我忘了提到我正在使用JAXL v3并考虑添加完整代码和系统配置会更有帮助。

<?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";

?>

系统配置:

  • Ubuntu 12.04.01 LTS
  • Apache 2.2.22
  • PHP 5.3.10

要执行的终端命令;

  • &GT; php facebookbot.php

2 个答案:

答案 0 :(得分:1)

尝试:

$msg = new XMPPMsg(array('to'=>'example-facebook-id@chat.facebook.com'), 'test message');
$client->send($msg);

答案 1 :(得分:1)

您应该尝试一些事情,看看它们是否可以帮助您进行调试:

  1. 设置'log_level' => JAXL_DEBUG,并检查日志以查看Jaxl向Facebook发送的确切内容。如果可以,请使用传出节更新问题,以便我们可以验证这里是否存在错误。
  2. 如果您使用Facebook ID发送消息,请记住您需要发送消息'to'=>'-4@chat.facebook.com'。请注意否定符号。
  3. Facebook在网络中捕获垃圾邮件发送者非常聪明。如果您试图在短时间内发送太多类似的消息,Facebook服务器将回复相应的错误到机器人(他们甚至可以暂时禁用您的聊天帐户)。检查日志以查看是否从facebook服务器返回任何此类错误响应。