【问题标题】:How to send facebook chat message via JAXL?如何通过 JAXL 发送 Facebook 聊天消息?
【发布时间】:2012-12-29 14:30:31
【问题描述】:

我正在尝试以用户的名义使用 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

要执行的终端命令;

  • > php facebookbot.php

【问题讨论】:

    标签: php xmpp facebook-chat


    【解决方案1】:

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

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

    【讨论】:

    • 很好的信息。我成功向 facebook id 发送消息(仍然无法发送到 user.name@chat.facebook.com)。除此之外,我必须在消息发送成功或失败时结束 php 脚本。但是,我找不到正确的回调。此外,有时 facebook 会拒绝连接并出现错误 111,这要归功于 JAXL,它可以很好地处理重新连接,但延迟很小。对于所有这些成功、错误和失败的可能性,是否有正确的回调?
    • 当连接被目标 jabber 服务器拒绝时,'on_connect_error' 回调会被errnoerrstr 触发。但是,如果 errno 是 61、110、111 之一,则不会触发此回调,因为在这种情况下,JAXL 会使用指数退避策略进行连接重试。当 JAXL 机器人终止时,就在脚本结束之前它会触发 'on_disconnect' 回调,这可能就是你要找的。​​span>
    • 无法捕获错误或断开回调,但是我使用“sleep(15); exit;”进行了修补发送消息命令后。我知道这不是一个可靠的补丁,但会一直工作到我在发送消息后找到一种健康终止的方法。
    【解决方案2】:

    尝试:

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

    【讨论】:

    • 我仍然无法使用您的代码 sn-p 发送。你能解释更多关于使用的先决条件吗?例如,端口 1234 应该是开放的,等等......
    猜你喜欢
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 1970-01-01
    相关资源
    最近更新 更多