xmpppy和Facebook聊天集成

时间:2011-01-19 06:03:06

标签: python facebook chat xmpppy

我正在尝试创建一个非常简单的脚本,它使用python的xmpppy通过Facebook聊天发送消息。

import xmpp
FACEBOOK_ID = "username@chat.facebook.com"
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("friend@chat.facebook.com","Hello world",))

此代码适用于通过gchat发送消息,但是当我尝试使用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的任何时候运行时都会收到以下两条消息:

/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

我很擅长解决这些问题,并建议或链接到可以帮助我前进解决这些问题的资源,我们将不胜感激。谢谢你的阅读!

2 个答案:

答案 0 :(得分:2)

我也开始了同样的项目,并陷入了同样的问题。我也找到了解决方案。你必须写一个facebook的UserName(因此你必须选择一个用户名),这也是小的Caps。这是最重要的部分。很可能你也喜欢我不会用小帽子写它。

答案 1 :(得分:1)

import xmpp

FACEBOOK_ID = "username@chat.facebook.com"
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;)