-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfbchat.py
More file actions
99 lines (76 loc) · 3.18 KB
/
fbchat.py
File metadata and controls
99 lines (76 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import sleekxmpp
import logging
import socket
import Queue
class FBChat(sleekxmpp.ClientXMPP):
def __init__( self, app_id, access_token, port=5222 ):
# initialize ClientXMPP superclass
# jid and password is not requried in FB XMPP server
super(FBChat, self).__init__( jid='', password='', sasl_mech='X-FACEBOOK-PLATFORM')
self.auto_reconnect = True
# following facebook chat api...
self.credentials['api_key'] = app_id
self.credentials['access_token'] = access_token
# add event handlers
self.add_event_handler('session_start', self.start)
self.add_event_handler('no_auth', self.authFailed)
# auth_queue to check auth failure
self.auth_queue = Queue.Queue()
self.xmppPort = port
def authFailed(self, event):
# sending message has failed..
self.auth_queue.put('authFailed')
self.disconnect(wait=True)
def start(self, event):
# authentication is successful!
print 'authentication successfull..'
self.auth_queue.put('success')
# NOTE: sensing presence and getting roster follows of XMPP guidline
self.send_presence()
self.get_roster()
# NOTE: I'm not sure how we can check is message is succesfully sent..
self.send_message( mto=self.fb_id,
mbody=self.msg,
mtype='chat')
print 'message has been sent (but was it succesfully sent?)'
print 'disconnecting...'
self.disconnect(wait=True)
def sendMessage(self, fb_id, msg):
# NOTE: Currently, FBChat needs to make new XMPP connection every time user calls sendMessage
# That is pretty terrible...
# clear success/failure state
self.auth_queue.queue.clear()
# save fb_id and msg as internal var
self.fb_id = str(fb_id) + '@chat.facebook.com'
self.msg = msg
# get IP address of facebook chat
# for some reason, internal dns doesnt seem to work
try:
server = socket.gethostbyname('chat.facebook.com')
except socket.error:
print e.strerror
print 'sendMessage fails'
return False
port = self.xmppPort
# since sleekxmpp is multithreaded
# we should process send_message at atConnection
self.connected = self.connect( (server, port ) )
if not self.connected:
print 'connection failed'
return False
# initiate xmpp protocol (?)
self.process(block=True)
# we poll our queue to retrieve its content (overkill?)
try:
result = fbChat.auth_queue.get(timeout=10)
except queue.Empty:
result = 'failed'
if result == 'authFailed':
raise Exception('authorization failed. perhaps access token expired?')
# return true on success
return result=='success'
if __name__ == '__main__':
# enable xmpp logs by removing the comment
#logging.basicConfig(level=logging.DEBUG,format='%(levelname)-8s %(message)s')
fbChat = FBChat( APP_ID, ACCESS_TOKEN )
print fbChat.sendMessage( FBID, 'hello world' )