From 12bbc0058cfbcc6537e84f8f99b7616f814aeb65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Soszy=C5=84ski?= Date: Mon, 20 Jan 2020 09:28:41 +0100 Subject: [PATCH 1/5] Add onPendingMessage (#512) --- fbchat/_client.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/fbchat/_client.py b/fbchat/_client.py index e491d19..fc85605 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -2271,7 +2271,17 @@ class Client(object): elif delta_class == "ForcedFetch": mid = delta.get("messageId") if mid is None: - self.onUnknownMesssageType(msg=delta) + if delta["threadKey"] is not None: + # Looks like the whole delta is metadata in this case + thread_id, thread_type = getThreadIdAndThreadType(delta) + self.onPendingMessage( + thread_id=thread_id, + thread_type=thread_type, + metadata=delta, + msg=delta, + ) + else: + self.onUnknownMesssageType(msg=delta) else: thread_id = str(delta["threadKey"]["threadFbId"]) fetch_info = self._forcedFetch(thread_id, mid) @@ -2727,6 +2737,14 @@ class Client(object): msg=delta, ) + # New pending message + elif delta_class == "ThreadFolder" and delta.get("folder") == "FOLDER_PENDING": + # Looks like the whole delta is metadata in this case + thread_id, thread_type = getThreadIdAndThreadType(delta) + self.onPendingMessage( + thread_id=thread_id, thread_type=thread_type, metadata=delta, msg=delta + ) + # Unknown message type else: self.onUnknownMesssageType(msg=delta) @@ -2949,6 +2967,21 @@ class Client(object): """ log.info("{} from {} in {}".format(message_object, thread_id, thread_type.name)) + def onPendingMessage( + self, thread_id=None, thread_type=None, metadata=None, msg=None + ): + """Called when the client is listening, and somebody that isn't + connected with you on either Facebook or Messenger sends a message. + After that, you need to use fetchThreadList to actually read the message. + + Args: + thread_id: Thread ID that the message was sent to. See :ref:`intro_threads` + thread_type (ThreadType): Type of thread that the message was sent to. See :ref:`intro_threads` + metadata: Extra metadata about the message + msg: A full set of the data received + """ + log.info("New pending message from {}".format(thread_id)) + def onColorChange( self, mid=None, From 19c875c18aa598cac8605823f1c324009058d101 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Mon, 20 Jan 2020 09:32:30 +0100 Subject: [PATCH 2/5] =?UTF-8?q?Bump=20version:=201.9.4=20=E2=86=92=201.9.5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .bumpversion.cfg | 2 +- fbchat/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 9938290..b8f82b7 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 1.9.4 +current_version = 1.9.5 commit = True tag = True diff --git a/fbchat/__init__.py b/fbchat/__init__.py index eb8153f..5591481 100644 --- a/fbchat/__init__.py +++ b/fbchat/__init__.py @@ -13,7 +13,7 @@ from ._client import Client from ._util import log # TODO: Remove this (from examples too) __title__ = "fbchat" -__version__ = "1.9.4" +__version__ = "1.9.5" __description__ = "Facebook Chat (Messenger) for Python" __copyright__ = "Copyright 2015 - 2019 by Taehoon Kim" From 22c6c82c0e210d9cae5e1d91b6d44aef48ea6146 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Mon, 20 Jan 2020 14:54:25 +0100 Subject: [PATCH 3/5] Disable /t_rtc MQTT topic --- fbchat/_mqtt.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/fbchat/_mqtt.py b/fbchat/_mqtt.py index a9f6e31..3b4f5f1 100644 --- a/fbchat/_mqtt.py +++ b/fbchat/_mqtt.py @@ -171,6 +171,10 @@ class Mqtt(object): "/br_sr", # Response to /br_sr "/sr_res", + # Data about user-to-user calls + # TODO: Investigate the response from this! (A bunch of binary data) + # "/t_rtc", + # TODO: Find out what this does! # TODO: Investigate the response from this! (A bunch of binary data) # "/t_p", # TODO: Find out what this does! @@ -186,7 +190,6 @@ class Mqtt(object): "/messaging_events", "/orca_message_notifications", "/pp", - "/t_rtc", "/webrtc_response", ] From b4618739f30d9f227070c140297406fa746f78da Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Tue, 21 Jan 2020 18:51:32 +0100 Subject: [PATCH 4/5] Fix MQTT errors after being offline for too long --- fbchat/_mqtt.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/fbchat/_mqtt.py b/fbchat/_mqtt.py index 3b4f5f1..9bb361a 100644 --- a/fbchat/_mqtt.py +++ b/fbchat/_mqtt.py @@ -79,6 +79,8 @@ class Mqtt(object): log.exception("Failed parsing MQTT data on %s as JSON", message.topic) return + log.debug("MQTT payload: %s, %s", message.topic, j) + if message.topic == "/t_ms": # Update sync_token when received # This is received in the first message after we've created a messenger @@ -86,18 +88,31 @@ class Mqtt(object): if "syncToken" in j and "firstDeltaSeqId" in j: self._sync_token = j["syncToken"] self._sequence_id = j["firstDeltaSeqId"] + return # Update last sequence id when received if "lastIssuedSeqId" in j: self._sequence_id = j["lastIssuedSeqId"] if "errorCode" in j: - # Known types: ERROR_QUEUE_OVERFLOW | ERROR_QUEUE_NOT_FOUND - # 'F\xfa\x84\x8c\x85\xf8\xbc-\x88 FB_PAGES_INSUFFICIENT_PERMISSION\x00' - log.error("MQTT error code %s received", j["errorCode"]) - # TODO: Consider resetting the sync_token and sequence ID here? - - log.debug("MQTT payload: %s, %s", message.topic, j) + error = j["errorCode"] + # TODO: 'F\xfa\x84\x8c\x85\xf8\xbc-\x88 FB_PAGES_INSUFFICIENT_PERMISSION\x00' + if error in ("ERROR_QUEUE_NOT_FOUND", "ERROR_QUEUE_OVERFLOW"): + # ERROR_QUEUE_NOT_FOUND means that the queue was deleted, since too + # much time passed, or that it was simply missing + # ERROR_QUEUE_OVERFLOW means that the sequence id was too small, so + # the desired events could not be retrieved + log.error( + "The MQTT listener was disconnected for too long," + " events may have been lost" + ) + self._sync_token = None + self._sequence_id = self._fetch_sequence_id(self._state) + self._messenger_queue_publish() + # TODO: Signal to the user that they should reload their data! + return + log.error("MQTT error code %s received", error) + return # Call the external callback self._on_message(message.topic, j) @@ -129,6 +144,9 @@ class Mqtt(object): if rc != 0: return # Don't try to send publish if the connection failed + self._messenger_queue_publish() + + def _messenger_queue_publish(self): # configure receiving messages. payload = { "sync_api_version": 10, From b9b4d57b25d3d81353eb2c25083a01d619c43ed8 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Tue, 21 Jan 2020 19:50:57 +0100 Subject: [PATCH 5/5] =?UTF-8?q?Bump=20version:=201.9.5=20=E2=86=92=201.9.6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .bumpversion.cfg | 2 +- fbchat/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index b8f82b7..8464e12 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 1.9.5 +current_version = 1.9.6 commit = True tag = True diff --git a/fbchat/__init__.py b/fbchat/__init__.py index 5591481..17e55d6 100644 --- a/fbchat/__init__.py +++ b/fbchat/__init__.py @@ -13,7 +13,7 @@ from ._client import Client from ._util import log # TODO: Remove this (from examples too) __title__ = "fbchat" -__version__ = "1.9.5" +__version__ = "1.9.6" __description__ = "Facebook Chat (Messenger) for Python" __copyright__ = "Copyright 2015 - 2019 by Taehoon Kim"