Facebook:如何使用xmpp将私人消息作为粉丝页面发送

时间:2014-02-13 14:52:11

标签: python facebook api xmpp

经过数天搜索未能成功解决问题,我决定寻求帮助。

我正在尝试使用带有python的XMPP协议向Facebook用户发送私人消息作为我的粉丝页面。

我正在使用以下代码创建客户端:

import sleekxmpp

class SendMsgBot(sleekxmpp.ClientXMPP):
    def __init__(self, jid, recipient, message):
        sleekxmpp.ClientXMPP.__init__(self, jid, 'ignore')
        self.recipient = recipient
        self.msg = message
        self.add_event_handler("session_start", self.start, threaded=True)

    def start(self, event):
        self.send_presence()
        self.get_roster()
        self.send_message(mto=self.recipient,
        mbody=self.msg,
        mtype='chat')
        self.disconnect(wait=True)

这是我使用上面创建的客户端发送消息的代码:

from fbxmpp import SendMsgBot

jid = '1000021...@chat.facebook.com'
to = '1351...@chat.facebook.com'
msg = 'Hey  anything , how is it going?'

xmpp = SendMsgBot(jid, to, unicode(msg))

xmpp.credentials['api_key'] = '599641.....' 
xmpp.credentials['access_token'] = 'CAACEdEose0cBACVTezXXZATDZAZBucX8fTUB0a....'

if xmpp.connect(('chat.facebook.com', 5222)):
    xmpp.process(block=True)
    print("Done")
else:
    print("Unable to connect.")

在上面的代码中使用我的facebook登录名和密码,我没有收到任何错误。但我需要使用粉丝专页发送消息。

我使用api_key和access_token成功验证了,但是控制台冻结了一段时间,并显示以下消息:

DEBUG:sleekxmpp.xmlstream.xmlstream:RECV: <success xmlns="urn:ietf:params:xml:ns:xmpp-sasl" />
DEBUG:sleekxmpp.xmlstream.xmlstream:Event triggered: auth_success
DEBUG:sleekxmpp.xmlstream.xmlstream:SEND (IMMED): <stream:stream to='chat.facebook.com' xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' xml:lang='en' version='1.0'>
DEBUG:sleekxmpp.xmlstream.xmlstream:RECV: <stream:stream version="1.0" from="chat.facebook.com" id="1" xml:lang="en">
DEBUG:sleekxmpp.xmlstream.xmlstream:RECV: <stream:features xmlns="http://etherx.jabber.org/streams"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind" /><session xmlns="urn:ietf:params:xml:ns:xmpp-session" /></stream:features>
DEBUG:sleekxmpp.features.feature_bind.bind:Requesting resource: 
DEBUG:sleekxmpp.xmlstream.xmlstream:SEND (IMMED): <iq type="set" id="6e6fe59f-1683-41a9-9a32-dc8a952cdecd-2"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind" /></iq>
DEBUG:sleekxmpp.xmlstream.xmlstream:RECV: <iq type="result" from="-100002126842229@chat.facebook.com" id="6e6fe59f-1683-41a9-9a32-dc8a952cdecd-2"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>-100002126842229@chat.facebook.com/xymAXUKM</jid></bind></iq>

超时后,我收到了另一条消息:

WARNING:sleekxmpp.xmlstream.handler.waiter:Timed out waiting for IqWait_6e6fe59f-1683-41a9-9a32-dc8a952cdecd-2
ERROR:sleekxmpp.xmlstream.xmlstream:Error processing stream handler: Stream Features
Traceback (most recent call last):
  File "/Users/felipecabral/.virtualenvs/facebook/lib/python2.7/site-packages/sleekxmpp/xmlstream/xmlstream.py", line 1669, in _event_runner
    handler.run(args[0])
  File "/Users/felipecabral/.virtualenvs/facebook/lib/python2.7/site-packages/sleekxmpp/xmlstream/handler/callback.py", line 76, in run
    self._pointer(payload)
  File "/Users/felipecabral/.virtualenvs/facebook/lib/python2.7/site-packages/sleekxmpp/clientxmpp.py", line 280, in _handle_stream_features
    if handler(features) and restart:
  File "/Users/felipecabral/.virtualenvs/facebook/lib/python2.7/site-packages/sleekxmpp/features/feature_bind/bind.py", line 50, in _handle_bind_resource
    response = iq.send(now=True)
  File "/Users/felipecabral/.virtualenvs/facebook/lib/python2.7/site-packages/sleekxmpp/stanza/iq.py", line 230, in send
    raise IqTimeout(self)
IqTimeout
ERROR:sleekxmpp.xmlstream.stanzabase:Error handling {jabber:client}features stanza
Traceback (most recent call last):
  File "/Users/felipecabral/.virtualenvs/facebook/lib/python2.7/site-packages/sleekxmpp/xmlstream/xmlstream.py", line 1669, in _event_runner
    handler.run(args[0])
  File "/Users/felipecabral/.virtualenvs/facebook/lib/python2.7/site-packages/sleekxmpp/xmlstream/handler/callback.py", line 76, in run
    self._pointer(payload)
  File "/Users/felipecabral/.virtualenvs/facebook/lib/python2.7/site-packages/sleekxmpp/clientxmpp.py", line 280, in _handle_stream_features
    if handler(features) and restart:
  File "/Users/felipecabral/.virtualenvs/facebook/lib/python2.7/site-packages/sleekxmpp/features/feature_bind/bind.py", line 50, in _handle_bind_resource
    response = iq.send(now=True)
  File "/Users/felipecabral/.virtualenvs/facebook/lib/python2.7/site-packages/sleekxmpp/stanza/iq.py", line 230, in send
    raise IqTimeout(self)
IqTimeout

知道我做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:1)

作为mentioned over at Github,这应该在sleekxmpp 1.2.4中修复。