You need to enable JavaScript to run this app. 如何用Python fbchat模块实现Facebook自动回复仅账号离线时触发
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python fbchat模块实现Facebook自动回复仅账号离线时触发

fbchat 自动回复逻辑调整:仅账号离线时生效

当前逻辑存在的问题

现有基于fbchat实现的自动回复已经可以正常运行,但没有和账号真实的在线状态做绑定:只要脚本保持客户端监听状态,收到任何消息都会触发自动回复。我们要实现的目标是哪怕脚本长期在后台保持运行,只要对应Facebook账号处于在线状态(即用户本人正在通过官方/其他客户端使用账号),自动回复就暂停生效;只有账号在平台显示为离线状态时,才对收到的消息执行自动回复。
原有代码的开关判断依赖拉取自身账号会话的最新消息、检查文本中是否包含"online"字符串来实现,这种方式可靠性差,很容易因为日常聊天发的其他消息误改开关状态,完全无法匹配"检测账号真实在线状态"的需求。

具体调整方向

  • 替换原有自聊消息的开关判断方式,直接调用fbchat自带的用户信息接口拉取自身账号的在线状态字段,该字段返回值为0时代表账号处于离线状态,非0值时代表账号在线。
  • 原有的关键词匹配、生成对应回复文本的逻辑完全保留,不需要做改动,仅调整最终是否执行消息发送的判断条件,从原来的"开关状态为开"改成"账号当前为离线状态"。
  • 消息标记已读、标记已送达的基础动作不做状态限制,不管账号是在线还是离线,收到消息都正常执行标记流程,避免出现消息状态异常。
  • 给在线状态查询增加本地缓存机制,比如每5分钟主动拉取一次最新的账号状态即可,不需要每收到一条消息就发一次接口请求,既减少不必要的请求量,也避免触发平台的接口限流。

调整后的核心代码实现

def onMessage(self, author_id=None, message_object=None, thread_id=None, thread_type=ThreadType.USER, **kwargs):
    # 拉取自身账号的在线状态,替换原有拉取自聊消息的开关逻辑
    own_user_info = client.fetch_user_info(client.uid)[client.uid]
    # presence=0 代表账号离线
    is_account_offline = (own_user_info.presence == 0)

    # 标记已读动作不受状态限制,正常执行
    self.markAsRead(author_id)
    log.info("Message {} from {} in {}".format(message_object, thread_id, thread_type))  

    # 原有关键词匹配生成回复的逻辑完全保留,不做改动
    msgText = message_object.text.lower()
    if(msgText=="hi" or msgText=="hii" or msgText=="hiii" or msgText=="yo" or msgText=="hi there" or msgText=="hey" or msgText=="heyy"):
        reply = "Hello :D"
    elif(msgText=="hello" or msgText=="hellow" or msgText=="heloo" or msgText=="hlo" or msgText=="hellow"):
        reply = "Hi :)"
    elif(msgText=="hy" or msgText=="hyy"):
        reply = "Hello :D"
    elif(msgText=="whatsup" or msgText=="wassup" or msgText=="what's up" or msgText=="wtsup" or msgText=="watsup" or msgText=="whats up" or "how are you" in msgText or "hw r u" in msgText):
        reply = "Awesome. What about you? :D"
    elif(msgText=="test" or msgText=="debug" or msgText=="aboutme" or msgText=="about" or msgText=="bot"):
        reply = "Hi, I'm a bot written in Python. Created by Nitish Ghimire"
    elif("birthday" in msgText or "bday" in msgText or "hbd" in msgText):
        reply = "Thank you! :D"
    elif("what you doing" in msgText):
        reply = "Idling, I'm a bot afterall. I work for my creator, Nitish Ghimire"
    elif("bye" in msgText or msgText=="byye" or msgText=="byee"):
        reply = "Ok bye! :D"
    elif("who is your girlfriend?" in msgText or msgText=="girlfriend" or msgText=="byee"):
        reply = "Shreejana GC <3"
    else:
        reply = "Thank you for contacting me. I'm currently not online, I was replying using a Python Script Created by Nitish Ghimire. I will reach out to you shortly."

    def sendMsgg():
        if (author_id!=self.uid):
            self.send(Message(text=reply), thread_id=thread_id, thread_type=thread_type)
        self.markAsDelivered(author_id, thread_id) 

    # 核心逻辑调整:仅当账号处于离线状态时,才执行自动回复
    if is_account_offline:
        sendMsgg()

可选的兼容优化

可以把原来的自聊手动开关逻辑保留,和自动检测的离线状态做逻辑绑定,比如临时需要强制关闭自动回复时,不需要停掉后台脚本,只要给自己发一条带指定字符串的消息就能切换状态,兼顾自动判断的便利性和手动控制的灵活性。


内容的提问来源于stack exchange,提问作者LuciferG0116

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.26 18:54:04