xmpppy 和 Facebook 聊天集成

发布于 2024-10-12 21:06:38 字数 1959 浏览 3 评论 0原文

我正在尝试创建一个非常简单的脚本,使用 python 的 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",))

此代码可以通过 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

我对解决此类问题相当陌生,并提供建议或链接到可以帮助我继续解决这些问题的资源问题将不胜感激。感谢您的阅读!

I'm trying to create a very simple script that uses python's xmpppy to send a message over facebook chat.

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",))

This code works to send a message via gchat, however when I try with facebook I recieve this error:

An error occurred while looking up _xmpp-client._tcp.chat.facebook.com

When I remove @chat.facebook.com from the FACEBOOK_ID I get this instead:

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', '')

I also notice any time I import xmpp I get the following two messages when running:

/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

I'm fairly new to solving these kinds of problems, and advise, or links to resources that could help me move forward in solve these issues would be greatly appreciated. Thanks for reading!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅黛梨妆こ 2024-10-19 21:06:38

我也开始了同样的项目,并陷入了同样的问题。我也找到了解决方案。您必须写下 facebook 的用户名(因此您必须选择一个用户名),并且也以小写字母书写。这是最重要的部分。很可能你也像我一样不会用小型大写字母写它。

I also started the same project, and was trapped into same problem. I found the solution too. You have to write the UserName of facebook (Hence You must opt one Username) and that too in small Caps. This is the most important part. Most probably you too like me would not be writing it in small Caps.

鸠书 2024-10-19 21:06:38
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 ;)

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)

This worked for me. Anyway, if you want to know the server response to your query, use Client.SendAndWaitForResponse instead of Client.send ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文