Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
881aa9adce | ||
|
4714be5697 | ||
|
cb7f4a72d7 | ||
|
fb63ff0db8 | ||
|
c5f447e20b | ||
|
b4d3769fd5 |
@@ -1,5 +1,5 @@
|
||||
[bumpversion]
|
||||
current_version = 1.9.0
|
||||
current_version = 1.9.3
|
||||
commit = True
|
||||
tag = True
|
||||
|
||||
|
@@ -13,7 +13,7 @@ from ._client import Client
|
||||
from ._util import log # TODO: Remove this (from examples too)
|
||||
|
||||
__title__ = "fbchat"
|
||||
__version__ = "1.9.0"
|
||||
__version__ = "1.9.3"
|
||||
__description__ = "Facebook Chat (Messenger) for Python"
|
||||
|
||||
__copyright__ = "Copyright 2015 - 2019 by Taehoon Kim"
|
||||
|
@@ -2835,7 +2835,7 @@ class Client(object):
|
||||
self._mqtt.set_chat_on(self._markAlive)
|
||||
|
||||
# TODO: Remove on_error param
|
||||
return self._mqtt.loop_once(on_error=self.onListenError)
|
||||
return self._mqtt.loop_once(on_error=lambda e: self.onListenError(exception=e))
|
||||
|
||||
def stopListening(self):
|
||||
"""Stop the listening loop."""
|
||||
|
@@ -31,9 +31,9 @@ class Mqtt(object):
|
||||
transport="websockets",
|
||||
)
|
||||
mqtt.enable_logger()
|
||||
# mqtt.max_inflight_messages_set(20)
|
||||
# mqtt.max_queued_messages_set(0) # unlimited
|
||||
# mqtt.message_retry_set(5)
|
||||
# mqtt.max_inflight_messages_set(20) # The rest will get queued
|
||||
# mqtt.max_queued_messages_set(0) # Unlimited messages can be queued
|
||||
# mqtt.message_retry_set(20) # Retry sending for at least 20 seconds
|
||||
# mqtt.reconnect_delay_set(min_delay=1, max_delay=120)
|
||||
# TODO: Is region (lla | atn | odn | others?) important?
|
||||
mqtt.tls_set()
|
||||
@@ -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,
|
||||
@@ -250,16 +259,19 @@ class Mqtt(object):
|
||||
return False # Stop listening
|
||||
|
||||
if rc != paho.mqtt.client.MQTT_ERR_SUCCESS:
|
||||
err = paho.mqtt.client.error_string(rc)
|
||||
|
||||
# If known/expected error
|
||||
if rc in [paho.mqtt.client.MQTT_ERR_CONN_LOST]:
|
||||
log.warning(err)
|
||||
if rc == paho.mqtt.client.MQTT_ERR_CONN_LOST:
|
||||
log.warning("Connection lost, retrying")
|
||||
elif rc == paho.mqtt.client.MQTT_ERR_NOMEM:
|
||||
# This error is wrongly classified
|
||||
# See https://github.com/eclipse/paho.mqtt.python/issues/340
|
||||
log.warning("Connection error, retrying")
|
||||
else:
|
||||
log.warning("MQTT Error: %s", err)
|
||||
err = paho.mqtt.client.error_string(rc)
|
||||
log.error("MQTT Error: %s", err)
|
||||
# For backwards compatibility
|
||||
if on_error:
|
||||
on_error(exception=FBchatException("MQTT Error {}".format(err)))
|
||||
on_error(_exception.FBchatException("MQTT Error {}".format(err)))
|
||||
|
||||
# Wait before reconnecting
|
||||
self._mqtt._reconnect_wait()
|
||||
@@ -273,8 +285,8 @@ class Mqtt(object):
|
||||
paho.mqtt.client.socket.error,
|
||||
OSError,
|
||||
paho.mqtt.client.WebsocketConnectionError,
|
||||
):
|
||||
log.debug("MQTT reconnection failed")
|
||||
) as e:
|
||||
log.debug("MQTT reconnection failed: %s", e)
|
||||
|
||||
return True # Keep listening
|
||||
|
||||
|
@@ -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),
|
||||
)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user