Skip to content

Instantly share code, notes, and snippets.

@uptown
Created July 10, 2014 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uptown/6731ad698b67fabc793d to your computer and use it in GitHub Desktop.
Save uptown/6731ad698b67fabc793d to your computer and use it in GitHub Desktop.
Facebook Chat
import sleekxmpp
import logging
logging.basicConfig(level=logging.DEBUG)
class SendMsgBot(sleekxmpp.ClientXMPP):
"""
A basic SleekXMPP bot that will log in, send a message,
and then log out.
"""
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')
# The "From" Facebook ID
jid = 'xxxx@chat.facebook.com'
# The "Recipient" Facebook ID, with a hyphen for some reason
to = '-xxxxx@chat.facebook.com'
# Whatever you're sending
msg = 'Hey Other Phil, how is it going?'
xmpp = SendMsgBot(jid, to, unicode(msg))
xmpp.credentials['api_key'] = 'xxxxx'
xmpp.credentials['access_token'] = "xxxxx"
if xmpp.connect(('chat.facebook.com', 5222)):
xmpp.process(block=True)
print("Done")
else:
print("Unable to connect.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment