Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed listening #357

Merged
merged 2 commits into from
Dec 9, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 17 additions & 26 deletions fbchat/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1956,55 +1956,47 @@ def unmuteThreadMentions(self, thread_id=None):
LISTEN METHODS
"""

def _ping(self, sticky, pool):
def _ping(self):
data = {
'channel': self.user_channel,
'clientid': self.client_id,
'partition': -2,
'cap': 0,
'uid': self.uid,
'sticky_token': sticky,
'sticky_pool': pool,
'sticky_token': self.sticky,
'sticky_pool': self.pool,
'viewer_uid': self.uid,
'state': 'active',
}
self._get(self.req_url.PING, data, fix_request=True, as_json=False)

def _fetchSticky(self):
"""Call pull api to get sticky and pool parameter, newer api needs these parameters to work"""

data = {
"msgs_recv": 0,
"channel": self.user_channel,
"clientid": self.client_id
}

j = self._get(self.req_url.STICKY, data, fix_request=True, as_json=True)

if j.get('lb_info') is None:
raise FBchatException('Missing lb_info: {}'.format(j))

return j['lb_info']['sticky'], j['lb_info']['pool']

def _pullMessage(self, sticky, pool, markAlive=True):
def _pullMessage(self, markAlive=True):
"""Call pull api with seq value to get message data."""

data = {
"msgs_recv": 0,
"sticky_token": sticky,
"sticky_pool": pool,
"sticky_token": self.sticky,
"sticky_pool": self.pool,
"clientid": self.client_id,
'state': 'active' if markAlive else 'offline',
}

j = self._get(ReqUrl.STICKY, data, fix_request=True, as_json=True)
j = self._get(self.req_url.STICKY, data, fix_request=True, as_json=True)

self.seq = j.get('seq', '0')
return j

def _parseMessage(self, content):
"""Get message and author name from content. May contain multiple messages in the content."""

if 'lb_info' in content:
self.sticky = content['lb_info']['sticky']
self.pool = content['lb_info']['pool']

if 'batches' in content:
for batch in content['batches']:
self._parseMessage(batch)

if 'ms' not in content: return

for m in content["ms"]:
Expand Down Expand Up @@ -2350,7 +2342,6 @@ def startListening(self):
:raises: FBchatException if request failed
"""
self.listening = True
self.sticky, self.pool = self._fetchSticky()

def doOneListen(self, markAlive=True):
"""
Expand All @@ -2364,8 +2355,8 @@ def doOneListen(self, markAlive=True):
"""
try:
if markAlive:
self._ping(self.sticky, self.pool)
content = self._pullMessage(self.sticky, self.pool, markAlive)
self._ping()
content = self._pullMessage(markAlive)
if content:
self._parseMessage(content)
except KeyboardInterrupt:
Expand Down