Events

class fbchat.Listener(*, session, chat_on, foreground, mqtt=NOTHING, sync_token=None, sequence_id=None, tmp_events=NOTHING)[source]

Listen to incoming Facebook events.

Initialize a connection to the Facebook MQTT service.

Parameters
  • session (Session) – The session to use when making requests.

  • chat_on (bool) – Whether …

  • foreground (bool) – Whether …

Example

>>> listener = fbchat.Listener(session, chat_on=True, foreground=True)
listen()[source]

Run the listening loop continually.

This is a blocking call, that will yield events as they arrive.

This will automatically reconnect on errors, except if the errors are one of PleaseRefresh or NotLoggedIn.

Example

Print events continually.

>>> for event in listener.listen():
...     print(event)
Return type

Iterable[Event]

disconnect()[source]

Disconnect the MQTT listener.

Can be called while listening, which will stop the listening loop.

The Listener object should not be used after this is called!

Example

Stop the listener when receiving a message with the text “/stop”

>>> for event in listener.listen():
...     if isinstance(event, fbchat.MessageEvent):
...         if event.message.text == "/stop":
...             listener.disconnect()  # Almost the same "break"
Return type

None

set_foreground(value)[source]

Set the foreground value while listening.

Return type

None

set_chat_on(value)[source]

Set the chat_on value while listening.

Return type

None