Allow initializing the MQTT Listener without making external requests
This commit is contained in:
@@ -97,32 +97,11 @@ def fetch_sequence_id(session: _session.Session) -> int:
|
|||||||
return int(sequence_id)
|
return int(sequence_id)
|
||||||
|
|
||||||
|
|
||||||
@attr.s(slots=True, kw_only=kw_only, repr=False, eq=False)
|
@attr.s(slots=True, kw_only=kw_only, eq=False)
|
||||||
class Listener:
|
class Listener:
|
||||||
"""Helper, to listen for incoming Facebook events."""
|
"""Helper, to listen for incoming Facebook events.
|
||||||
|
|
||||||
session = attr.ib(type=_session.Session)
|
Initialize a connection to the Facebook MQTT service.
|
||||||
_chat_on = attr.ib(type=bool)
|
|
||||||
_foreground = attr.ib(type=bool)
|
|
||||||
_sequence_id = attr.ib(type=int)
|
|
||||||
_mqtt = attr.ib(factory=mqtt_factory, type=paho.mqtt.client.Client)
|
|
||||||
_sync_token = attr.ib(None, type=str)
|
|
||||||
_tmp_events = attr.ib(None, type=Optional[Iterable[_events.Event]])
|
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
|
||||||
# An alternative repr, to illustrate that you can't create the class directly
|
|
||||||
return "<fbchat.Listener session={} chat_on={} foreground={}>".format(
|
|
||||||
self.session, self._chat_on, self._foreground
|
|
||||||
)
|
|
||||||
|
|
||||||
def __attrs_post_init__(self):
|
|
||||||
# Configure callbacks
|
|
||||||
self._mqtt.on_message = self._on_message_handler
|
|
||||||
self._mqtt.on_connect = self._on_connect_handler
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def connect(cls, session: _session.Session, chat_on: bool, foreground: bool):
|
|
||||||
"""Initialize a connection to the Facebook MQTT service.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
session: The session to use when making requests.
|
session: The session to use when making requests.
|
||||||
@@ -130,14 +109,21 @@ class Listener:
|
|||||||
foreground: Whether ...
|
foreground: Whether ...
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
>>> listener = fbchat.Listener.connect(session, chat_on=True, foreground=True)
|
>>> listener = fbchat.Listener(session, chat_on=True, foreground=True)
|
||||||
"""
|
"""
|
||||||
return cls(
|
|
||||||
session=session,
|
session = attr.ib(type=_session.Session)
|
||||||
chat_on=chat_on,
|
_chat_on = attr.ib(type=bool)
|
||||||
foreground=foreground,
|
_foreground = attr.ib(type=bool)
|
||||||
sequence_id=fetch_sequence_id(session),
|
_mqtt = attr.ib(factory=mqtt_factory, type=paho.mqtt.client.Client)
|
||||||
)
|
_sync_token = attr.ib(None, type=Optional[str])
|
||||||
|
_sequence_id = attr.ib(None, type=Optional[int])
|
||||||
|
_tmp_events = attr.ib(factory=list, type=Iterable[_events.Event])
|
||||||
|
|
||||||
|
def __attrs_post_init__(self):
|
||||||
|
# Configure callbacks
|
||||||
|
self._mqtt.on_message = self._on_message_handler
|
||||||
|
self._mqtt.on_connect = self._on_connect_handler
|
||||||
|
|
||||||
def _handle_ms(self, j):
|
def _handle_ms(self, j):
|
||||||
"""Handle /t_ms special logic.
|
"""Handle /t_ms special logic.
|
||||||
@@ -326,6 +312,9 @@ class Listener:
|
|||||||
>>> for event in listener.listen():
|
>>> for event in listener.listen():
|
||||||
... print(event)
|
... print(event)
|
||||||
"""
|
"""
|
||||||
|
if not self._sequence_id:
|
||||||
|
self._sequence_id = fetch_sequence_id(self.session)
|
||||||
|
|
||||||
# Make sure we're connected
|
# Make sure we're connected
|
||||||
while True:
|
while True:
|
||||||
# Beware, internal API, may have to change this to something more stable!
|
# Beware, internal API, may have to change this to something more stable!
|
||||||
|
Reference in New Issue
Block a user