fbchat icon indicating copy to clipboard operation
fbchat copied to clipboard

AttributeError: 'NoneType' object has no attribute '_chat_on' in doOneListen

Open nielsvaes opened this issue 4 years ago • 1 comments

Description of the problem

AttributeError when using doOneListen()

What I'm trying to do is the following: I have an application running with Qt. When the application starts I create an fbchat bot. A QTimer fires every 60 minutes to update a couple of widgets and that's where I also want to check if the fbchat bot has received any messages. I don't necessarily need it to listen() all the time, since this blocks the UI. I assumed that doOneListen() is exactly what I need, but it seems to be throwing this error.

Does anyone know why?

Code to reproduce

from fbchat import Client, log
from fbchat.models import *
from const import FB
import logging

# log.setLevel(logging.ERROR)


# Subclass fbchat.Client and override required methods
from fbchat import Client, log
from fbchat.models import *
from const import FB

# Subclass fbchat.Client and override required methods
class EchoBot(Client):
    def onMessage(self, author_id, message_object, thread_id, thread_type, **kwargs):
        self.markAsDelivered(thread_id, message_object.uid)
        self.markAsRead(thread_id)

        log.info("{} from {} in {}".format(message_object, thread_id, thread_type.name))

        # If you're not the author, echo
        if author_id != self.uid:
            self.send(message_object, thread_id=thread_id, thread_type=thread_type)


client = EchoBot(FB.email, FB.pwd)
client.doOneListen()

Traceback

Traceback (most recent call last):
 File "C:\Google Drive\Tools\venv\lib\site-packages\fbchat\_client.py", line 2858, in doOneListen
    if self._markAlive != self._mqtt._chat_on:
AttributeError: 'NoneType' object has no attribute '_chat_on'

Environment information

Python 3.8.3 fbchat 1.9.7

nielsvaes avatar Jun 13 '20 07:06 nielsvaes

Looks like you need to call startListening() before you do a listen. See https://github.com/carpedm20/fbchat/blob/v1.9.7/fbchat/_client.py#L2883 for how doOneListen() gets used in listen(). Specifically, note that in startListening() here: https://github.com/carpedm20/fbchat/blob/v1.9.7/fbchat/_client.py#L2831 self._mqtt gets set. It isn't set in the Client's init so it's None when you call doOneListen(), hence you get the AttributeError you posted about NoneType not having _chat_on.

wetmore avatar Jun 15 '20 03:06 wetmore