2012-10-12 263 views
2

我正在按照http://dalibornasevic.com/posts/35-how-to-send-private-messages-with-facebook-api中的说明将消息发送给我的朋友,并且正在通过命令行进行测试。以下是我正在使用的代码。当我打开我的Facebook帐户并检查发送的消息时,我没有看到我试图通过命令行发送的消息。我只是不明白我所犯的错误。无法使用xmpp4r_facebook发送Facebook消息

1.9.3-p194 :017 > require 'xmpp4r_facebook' 
=> true 

1.9.3-p194 :00 3 > id = '-<my facebook id>@chat.facebook.com' 
=> "-<my facebook id>@chat.facebook.com" 

1.9.3-p194 :00 4 > to = '-<friend facebook id>@chat.facebook.com' 
=> "-< friend's facebook id>@chat.facebook.com" 

1.9.3-p194 :00 5 > body = "hello, Im not spam!" 
=> "hello, Im not spam!" 

1.9.3-p194 :00 6 > subject = 'message from ruby' 
=> "message from ruby" 

1.9.3-p194 :00 7 > message = Jabber::Message.new to, body 
=> <message xmlns='jabber:client' to='-<friend facebook id>@chat.facebook.com'> ... </> 

1.9.3-p194 :00 8 > message.subject = subject 
=> "message from ruby" 

1.9.3-p194 :00 9 > 
1.9.3-p194 :010 > client = Jabber::Client.new Jabber::JID.new(id) 
=> #<Jabber::Client:0x007fc47e4d8498 @fd=nil, @status=1, @xmlcbs=#<Jabber::CallbackList:0x007fc47e4d8470 @list=[]>, @stanzacbs=#<Jabber::CallbackList:0x007fc47e4d8420 @list=[]>, @messagecbs=#<Jabber::CallbackList:0x007fc47e4d83d0 @list=[]>, @iqcbs=#<Jabber::CallbackList:0x007fc47e4d8380 @list=[]>, @presencecbs=#<Jabber::CallbackList:0x007fc47e4d8330 @list=[]>, @send_lock=#<Mutex:0x007fc47e4d82e0>, @last_send=2012-10-12 23:29:34 +0530, @exception_block=nil, @tbcbmutex=#<Mutex:0x007fc47e4d8290>, @threadblocks=[], @wakeup_thread=nil, @streamid=nil, @streamns="jabber:client", @features_sem=#<Jabber::Semaphore:0x007fc47e4d8218 @tickets=0, @lock=#<Mutex:0x007fc47e4d81f0>, @cond=#<ConditionVariable:0x007fc47e4d81c8 @waiters=[], @waiters_mutex=#<Mutex:0x007fc47e4d8178>>>, @parser_thread=nil, @processing=0, @host=nil, @port=nil, @allow_tls="constant", @tls=false, @ssl_capath=nil, @ssl_verifycb=nil, @features_timeout=10, @keepalive_interval=60, @use_ssl=false, @jid=-<my facebook id>@chat.facebook.com> 

1.9.3-p194 :011 > client.connect 
=> #<Jabber::Client:0x007fc47e4d8498 @fd=#<OpenSSL::SSL::SSLSocket:0x007fc47c852260>, @status=2, @xmlcbs=#<Jabber::CallbackList:0x007fc47e4d8470 @list=[]>, @stanzacbs=#<Jabber::CallbackList:0x007fc47e4d8420 @list=[]>, @messagecbs=#<Jabber::CallbackList:0x007fc47e4d83d0 @list=[]>, @iqcbs=#<Jabber::CallbackList:0x007fc47e4d8380 @list=[]>, @presencecbs=#<Jabber::CallbackList:0x007fc47e4d8330 @list=[]>, @send_lock=#<Mutex:0x007fc47e4d82e0>, @last_send=2012-10-12 23:29:35 +0530, @exception_block=nil, @tbcbmutex=#<Mutex:0x007fc47e4d8290>, @threadblocks=[], @wakeup_thread=nil, @streamid="10BAB5BB", @streamns="jabber:client", @features_sem=#<Jabber::Semaphore:0x007fc47e4d8218 @tickets=0, @lock=#<Mutex:0x007fc47e4d81f0>, @cond=#<ConditionVariable:0x007fc47e4d81c8 @waiters=[], @waiters_mutex=#<Mutex:0x007fc47e4d8178>>>, @parser_thread=#<Thread:0x007fc47c852008 sleep>, @processing=0, @host="chat.facebook.com", @port=5222, @allow_tls="constant", @tls=true, @ssl_capath=nil, @ssl_verifycb=nil, @features_timeout=10, @keepalive_interval=60, @use_ssl=false, @[email protected], @socket=#<OpenSSL::SSL::SSLSocket:0x007fc47c852260>, @stream_mechanisms=["X-FACEBOOK-PLATFORM", "DIGEST-MD5"], @stream_features={}, @parser=#<Jabber::StreamParser:0x007fc47c852030 @stream=#<OpenSSL::SSL::SSLSocket:0x007fc47c852260>, @listener=#<Jabber::Client:0x007fc47e4d8498 ...>, @current=nil, @started=true>, @keepaliveThread=#<Thread:0x007fc47c899868 run>> 
1.9.3-p194 :012 > client.auth_sasl(Jabber::SASL::XFacebookPlatform.new(client, '<App ID>', '<access token>', 'App Secret'), nil) => <iq xmlns='jabber:client' type='result' id='2071'> ... </> 

1.9.3-p194 :013 > client.send message 
=> nil 

1.9.3-p194 :014 > client.close 
=> #<Thread:0x007fc47c899868 dead> 

回答

2

这种宝石的格式是

-<id>@chat.facebook.comtoid

<id>@chat.facebook.com

通知初始破折号-(不知道为什么)

所以

message = Jabber::Message.new to, body 

后的反应应该是

<message xmlns='jabber:client' to='-<friend facebook id>@chat.facebook.com'> ... </>

我不知道用户名是否是可以互换的,所以我也相信你能,你必须使用Facebook用户的ID号只能使用聊天API在Facebook好友之间进行通信,而不能与Gmail中的任何人进行通信。 Facebook消息是与Facebook Chat完全不同的平台。

你需要使用ID,例如,如果配置文件是facebook.com/zuck和ID是4那么它将是to = '[email protected]'用户名将无法正常工作,我相信这个宝石。

+0

我改变了它,但仍然无法修复它。这些是我正在使用的更新说明要求'xmpp4r_facebook' id ='[email protected]' to ='[email protected]' body =“你好,我不是垃圾邮件!” 受试者 消息= '从红宝石消息'= Jabber的:: Message.new于身体 message.subject =受试者 客户= Jabber的:: Client.new的Jabber :: JID.new(ID) client.connect client.auth_sasl(Jabber :: SASL :: XFacebookPlatform.new(客户端,'517770868251374','<访问令牌>','933a3a61307af64f773460b4a7dc2c4e'),无) client.send消息 client.close – raju

+1

@santu我不是确定用户名是否可以互换,因此您必须使用Facebook用户的身份证号码,我相信您只能使用聊天API在Facebook好友之间进行通信,而不能与Gmail中的任何人进行通信。 Facebook消息是与Facebook Chat完全不同的平台。 – phwd

+0

我将它改为@ chat.facebook.com',但它仍然不工作:( – raju