Created
July 10, 2014 15:22
-
-
Save uptown/6731ad698b67fabc793d to your computer and use it in GitHub Desktop.
Facebook Chat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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