Compare commits

...

8 Commits

Author SHA1 Message Date
Mads Marquart
19c875c18a Bump version: 1.9.4 → 1.9.5 2020-01-20 09:32:30 +01:00
Mateusz Soszyński
12bbc0058c Add onPendingMessage (#512) 2020-01-20 09:28:41 +01:00
Mads Marquart
9c81806b95 Bump version: 1.9.3 → 1.9.4 2020-01-14 23:29:58 +01:00
Mads Marquart
45303005b8 Fix onFriendRequest 2020-01-14 23:27:50 +01:00
Mads Marquart
881aa9adce Bump version: 1.9.2 → 1.9.3 2020-01-08 09:38:18 +01:00
Mads Marquart
4714be5697 Fix MQTT JSON decoding 2020-01-08 09:35:26 +01:00
Mads Marquart
cb7f4a72d7 Bump version: 1.9.1 → 1.9.2 2020-01-08 08:47:16 +01:00
Mads Marquart
fb63ff0db8 Fix cookie header extraction
Only worked when the cookies were loaded from file, hence the reason I
didn't spot it the first time. Thanks to @gave92 for the suggestion.

Fixes #495
2020-01-08 08:46:22 +01:00
5 changed files with 60 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
[bumpversion]
current_version = 1.9.1
current_version = 1.9.5
commit = True
tag = True

View File

@@ -13,7 +13,7 @@ from ._client import Client
from ._util import log # TODO: Remove this (from examples too)
__title__ = "fbchat"
__version__ = "1.9.1"
__version__ = "1.9.5"
__description__ = "Facebook Chat (Messenger) for Python"
__copyright__ = "Copyright 2015 - 2019 by Taehoon Kim"

View File

@@ -2271,6 +2271,16 @@ class Client(object):
elif delta_class == "ForcedFetch":
mid = delta.get("messageId")
if mid is None:
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"])
@@ -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)
@@ -2768,9 +2786,15 @@ class Client(object):
msg=m,
)
elif topic == "jewel_requests_add":
# Other notifications
elif topic == "/legacy_web":
# Friend request
if m["type"] == "jewel_requests_add":
from_id = m["from"]
# TODO: from_id = str(from_id)
self.onFriendRequest(from_id=from_id, msg=m)
else:
self.onUnknownMesssageType(msg=m)
# Chat timestamp / Buddylist overlay
elif topic == "/orca_presence":
@@ -2943,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,

View File

@@ -74,8 +74,8 @@ class Mqtt(object):
def _on_message_handler(self, client, userdata, message):
# Parse payload JSON
try:
j = _util.parse_json(message.payload)
except _exception.FBchatFacebookError:
j = _util.parse_json(message.payload.decode("utf-8"))
except (_exception.FBchatFacebookError, UnicodeDecodeError):
log.exception("Failed parsing MQTT data on %s as JSON", message.topic)
return
@@ -122,6 +122,13 @@ class Mqtt(object):
raise
def _on_connect_handler(self, client, userdata, flags, rc):
if rc == 21:
raise _exception.FBchatException(
"Failed connecting. Maybe your cookies are wrong?"
)
if rc != 0:
return # Don't try to send publish if the connection failed
# configure receiving messages.
payload = {
"sync_api_version": 10,
@@ -228,7 +235,9 @@ class Mqtt(object):
headers = {
# TODO: Make this access thread safe
"Cookie": _util.get_cookie_header(self._state._session, self._HOST),
"Cookie": _util.get_cookie_header(
self._state._session, "https://edge-chat.facebook.com/chat"
),
"User-Agent": self._state._session.headers["User-Agent"],
"Origin": "https://www.facebook.com",
"Host": self._HOST,

View File

@@ -70,10 +70,11 @@ def strip_json_cruft(text):
raise FBchatException("No JSON object found: {!r}".format(text))
def get_cookie_header(session, host):
def get_cookie_header(session, url):
"""Extract a cookie header from a requests session."""
# The cookies are extracted this way to make sure they're escaped correctly
return requests.cookies.get_cookie_header(
session.cookies, requests.Request("GET", host),
session.cookies, requests.Request("GET", url),
)