简体   繁体   中英

Facebook : How to send a private message as a fanpage using xmpp

After days searching unsuccessfully for a solution to my problem, I've decided to ask for help.

I'm trying to send a private message to a facebook user as my fan page using XMPP protocol with python.

I'm using the code below to create the client:

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)

This is the code where I use the client created above to send the message:

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

Using my facebook login and password in the code above, I don't get any error. But I need to send the message using my fan page .

I got successfully authenticated with my api_key and access_token, but the console freezes for a while with the following message:

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>

And after the timeout I got this other message:

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

Any idea of what am I doing wrong?

Thank you!

正如在Github上提到的 ,此问题应在sleekxmpp 1.2.4中修复。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM