fbchat中client.listen失效,自动回复聊天机器人无响应求助
client.listen() Let's walk through the most common reasons why your bot isn't responding to messages anymore, even though it shows "Listening...":
1. Outdated fbchat Library
Facebook frequently updates its API endpoints, and older versions of fbchat might not support the latest changes. This is the most likely culprit if your bot worked before but stopped suddenly.
Fix:
Upgrade fbchat to the latest version using pip:
pip install --upgrade fbchat
2. Session/Authentication Issues
Even though the log says "Login successful," your session might not have the proper permissions to listen for messages—especially if you have two-factor authentication (2FA) enabled on your Facebook account.
Fixes:
- If you use 2FA, generate an app password in your Facebook account settings and use that instead of your regular login password.
- Alternatively, log into Facebook manually in a browser, extract your session cookies, and use
fbchat.Client.login_with_session_cookies()instead of password-based login. This often resolves silent authentication permission gaps.
3. Debug Your onMessage Callback
It's possible the onMessage method isn't being triggered at all, or your author ID check is failing silently. Let's add detailed logs to diagnose this:
Modified Code with Debug Logs:
from fbchat import log, Client 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) # Add debug logs to verify execution flow log.info(f"Bot UID: {self.uid} | Message from Author ID: {author_id}") log.info(f"Message content: {message_object.text} | Thread: {thread_id} ({thread_type.name})") if author_id != self.uid: log.info("Preparing to send echo response...") self.send(message_object, thread_id=thread_id, thread_type=thread_type) log.info("Echo response sent successfully!") client = EchoBot("myaccount", "mypassword") log.info(f"Logged in as UID: {client.uid}") client.listen()
Run this updated code and check the logs when you send a message:
- If you don't see the "Bot UID | Message from Author ID" line, the callback isn't being triggered (pointing to API or session issues).
- If you see that line but not "Preparing to send echo response," your
author_id != self.uidcheck is failing (maybeself.uidisn't being set correctly).
4. Facebook Platform Restrictions
Sometimes Facebook flags automated tools as suspicious, even for personal use. Check if your account has any messaging or access restrictions.
Fix:
- Log into your Facebook account manually to confirm there are no pending security alerts or restrictions.
- Send a message manually from the bot account to ensure it hasn't been blocked from sending messages.
内容的提问来源于stack exchange,提问作者Jon Kim

