2011-01-19 86 views
3

我試圖創建一個使用Python的xmpppy超過Facebook的聊天消息發送一個非常簡單的腳本。xmpppy和Facebook聊天集成

 
import xmpp 
FACEBOOK_ID = "[email protected]" 
PASS = "password" 
SERVER = "chat.facebook.com" 
jid=xmpp.protocol.JID(FACEBOOK_ID) 
C=xmpp.Client(jid.getDomain(),debug=[]) 
if not C.connect((SERVER,5222)): 
    raise IOError('Can not connect to server.') 
if not C.auth(jid.getNode(),PASS): 
    raise IOError('Can not auth with server.') 
C.send(xmpp.protocol.Message("[email protected]","Hello world",)) 

此代碼通過Google即時通訊發送消息,但是當我嘗試用facebook我收到此錯誤:

在查找出錯_xmpp-client._tcp.chat.facebook.com

當我從FACEBOOK_ID刪除@ chat.facebook.com我得到這個代替:

 
File "gtalktest.py", line 11, in 
    if not C.connect((SERVER,5222)): 
    File "/home/john/xmpppy-0.3.1/xmpp/client.py", line 195, in connect 
    if not CommonClient.connect(self,server,proxy,secure,use_srv) or secureNone and not secure: return self.connected 
    File "/home/john/xmpppy-0.3.1/xmpp/client.py", line 179, in connect 
    if not self.Process(1): return 
    File "/home/john/xmpppy-0.3.1/xmpp/dispatcher.py", line 302, in dispatch 
    handler['func'](session,stanza) 
    File "/home/john/xmpppy-0.3.1/xmpp/dispatcher.py", line 214, in streamErrorHandler 
    raise exc((name,text)) 
xmpp.protocol.HostUnknown: (u'host-unknown', '') 

我也注意到任何時候我導入XMPP我碰到下面當r兩個消息unning:

 
/home/john/xmpppy-0.3.1/xmpp/auth.py:24: DeprecationWarning: the sha module is deprecated; use the hashlib module instead 
    import sha,base64,random,dispatcher 
/home/john/xmpppy-0.3.1/xmpp/auth.py:26: DeprecationWarning: the md5 module is deprecated; use hashlib instead 
    import md5 

我是相當新的解決這類問題,並建議,或資源的鏈接,可以幫助我前進中解決這些問題,將不勝感激。謝謝閱讀!

+0

看起來你是在一個正確的方式做所有。嘗試將`always`添加到Client構造函數的`debug`參數中:`C = xmpp.Client(jid.getDomain(),debug = ['always'])``。也許這些日誌會說多一點。 :) – eigenein 2011-01-19 09:32:21

回答

2

我也開始在同一個項目,並陷入同樣的​​問題。我也找到了解決方案。您必須編寫Facebook的用戶名(因此您必須選擇一個用戶名),並且這也適用於小型Caps。這是最重要的部分。很可能你也和我一樣不會將它寫在小帽子裏。

1
import xmpp 

FACEBOOK_ID = "[email protected]" 
PASS = "password" 
SERVER = "chat.facebook.com" 

jid=xmpp.protocol.JID(FACEBOOK_ID) 

client=xmpp.Client(jid.getDomain(),debug=['always']) 

if not client.connect((SERVER,5222)): 
    raise IOError('Can not connect to server.') 
if not client.auth(jid.getNode(),PASS): 
    raise IOError('Can not auth with server.') 


message = xmpp.protocol.Message(frm=client.Bind.bound[0], to="-<#_ID_OF_FRIEND>@chat.facebook.com", typ="chat", body="Hello world",) 

client.SendAndWaitForResponse(message) 

這對我有效。無論如何,如果你想知道服務器響應您的查詢,使用Client.SendAndWaitForResponse代替Client.send;)