我回答了一個鏈接到我寫的博客的鏈接,因爲它完美地描述瞭解決方案,但顯然這讓一些版主惱火。
雖然這顯然是荒謬的,這裏是轉發的答案。
import sleekxmpp
import logging
logging.basicConfig(level=logging.DEBUG)
class SendMsgBot(sleekxmpp.ClientXMPP):
def init(self, jid, recipient, message):
sleekxmpp.ClientXMPP.__init__(self, jid, 'ignore')
# The message we wish to send, and the JID that
# will receive it.
self.recipient = recipient
self.msg = message
# The session_start event will be triggered when
# the bot establishes its connection with the server
# and the XML streams are ready for use. We want to
# listen for this event so that we we can initialize
# our roster.
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')
# Using wait=True ensures that the send queue will be
#emptied before ending the session.
self.disconnect(wait=True)
我猛的,在一個名爲fbxmpp.py,然後在另一個文件(你的員工,你的命令行應用程序,您的水壺控制器,等等),你需要像下面這樣:
from fbxmpp import SendMsgBot
# The "From" Facebook ID
jid = '[email protected]'
# The "Recipient" Facebook ID, with a hyphen for some reason
to = '[email protected]'
# Whatever you're sending
msg = 'Hey Other Phil, how is it going?'
xmpp = SendMsgBot(jid, to, unicode(msg))
xmpp.credentials['api_key'] = '123456'
xmpp.credentials['access_token'] = 'your-access-token'
if xmpp.connect(('chat.facebook.com', 5222)):
xmpp.process(block=True)
print("Done")
else:
print("Unable to connect.")
我已經實現了反饋中提到,謝謝。 –