2
連接到Facebook的聊天,我用這個代碼示例基於XMPP協議連接到Facebook聊天:使用SleekXMPP
#!/usr/bin/python
import sleekxmpp
import logging
logging.basicConfig(level=logging.DEBUG)
def session_start(event):
chatbot.send_presence()
print('Session started')
chatbot.get_roster()
def message(msg):
if msg['type'] in ('chat','normal'):
print('msg received')
print(msg['body'])
msg.reply('Thanks').send()
jid = '[email protected]'
password = 'mypassword'
server = ('chat.facebook.com', 5222)
chatbot = sleekxmpp.ClientXMPP(jid,password)
chatbot.add_event_handler('session_start', session_start)
chatbot.add_event_handler('message', message)
chatbot.auto_reconnect = True
chatbot.connect(server)
chatbot.process(block=True)
一切似乎都很正常,但是當我運行的代碼,我無法連接到Facebook服務器:
DEBUG:sleekxmpp.basexmpp:setting jid to [email protected]
DEBUG:sleekxmpp.basexmpp:Loaded Plugin (RFC-6120) STARTTLS Stream Feature
DEBUG:sleekxmpp.basexmpp:Loaded Plugin (RFC-6120) Resource Binding Stream Feature
DEBUG:sleekxmpp.basexmpp:Loaded Plugin (RFC-3920) Start Session Stream Feature
DEBUG:sleekxmpp.basexmpp:Loaded Plugin (RFC-6120) SASL Stream Feature
DEBUG:sleekxmpp.xmlstream.xmlstream:Trying to connect to chat.facebook.com:5222
DEBUG:sleekxmpp.xmlstream.xmlstream:Connecting to chat.facebook.com:5222
ERROR:sleekxmpp.xmlstream.xmlstream:Could not connect to chat.facebook.com:5222. Socket Error #111: Connection refused
DEBUG:sleekxmpp.xmlstream.xmlstream:Trying to connect to chat.facebook.com:5222
DEBUG:sleekxmpp.xmlstream.xmlstream:Waiting 1.97953654103 seconds before connecting.
...
我在這裏錯過了什麼嗎?
那麼,我仍然無法連接到'chat.facebook.com:5222',它嘗試失敗。任何想法可能是錯誤的?從pip安裝使用Python 2.7.2+和SleekXMPP 1.0。 –
關於'X-FACEBOOK-PLATFORM'身份驗證方法,我只需要在我的代碼上放置這些行,或者我錯過了一些東西?我已經有我的API密鑰和訪問令牌,但我不知道如何告訴sleekxmpp使用該身份驗證方法......無論如何,謝謝! :) –
好的,要嘗試查看問題出在哪裏,您可以嘗試使用telnet手動測試:telnet chat.facebook.com 5222,然後輸入一些字符並回車。如果您能夠連接到聊天服務器,您應該收到錯誤響應。我期望telnet命令失敗,說它無法連接。你是否支持任何可能禁止連接到Facebook聊天的防火牆? –