From 64bdde8f3316cebda333da9624dbbe5e565231b6 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 7 Nov 2018 20:06:10 +0100 Subject: [PATCH 1/2] Sticky and pool parameters can be set after the inital `_fetchSticky` --- fbchat/client.py | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/fbchat/client.py b/fbchat/client.py index 9481e4af..022850bf 100644 --- a/fbchat/client.py +++ b/fbchat/client.py @@ -1956,48 +1956,32 @@ 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 @@ -2005,6 +1989,10 @@ def _pullMessage(self, sticky, pool, markAlive=True): 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 'ms' not in content: return for m in content["ms"]: @@ -2350,7 +2338,6 @@ def startListening(self): :raises: FBchatException if request failed """ self.listening = True - self.sticky, self.pool = self._fetchSticky() def doOneListen(self, markAlive=True): """ @@ -2364,8 +2351,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: From 160386be62599b689a4c01b5bac1a55ccb553e0a Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Fri, 9 Nov 2018 20:08:26 +0100 Subject: [PATCH 2/2] Added support for `request_batch` parsing in `_parseMessage` --- fbchat/client.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fbchat/client.py b/fbchat/client.py index 022850bf..e6e04fc0 100644 --- a/fbchat/client.py +++ b/fbchat/client.py @@ -1993,6 +1993,10 @@ def _parseMessage(self, 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"]: