我使用此示例代码通过 XMPP 连接到 Facebook Chat:
#!/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 = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b06121e180e19050a060e2b08030a1f450d0a080e0904040045080406" rel="noreferrer noopener nofollow">[email protected]</a>'
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 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f4998d818791869a959991b4979c9580da92959791969b9b9fda979b99" rel="noreferrer noopener nofollow">[email protected]</a>
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.
...
我在这里遗漏了什么吗?
最佳答案
您在脚本中所做的一切都是正确的。这似乎是 Facebook 端的一个临时问题(如 Socket Error #111:连接被拒绝
所暗示)。针对 Sleek 的主分支和开发分支进行测试,您的脚本对我来说连接和登录都很好。
查看Facebook XMPP开发者关系小组,最近没有任何中断报告,所以我不确定目前的服务情况如何。
额外说明一下,由于您正在与 Facebook 打交道,如果您想使用 X-FACEBOOK-PLATFORM
身份验证方法,那么您可以设置:
chatbot.credentials['api_key'] = '...API_KEY...'
chatbot.credentials['access_token'] = '...TOKEN...'
别忘了还有 [email protected]如果您需要 Sleek 相关帮助,请联系房间。
-- 兰斯
关于python - 使用 SleekXMPP 连接到 Facebook 聊天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10732616/