【问题标题】:How can I loop over though all functions in a class after a server response?服务器响应后,如何循环遍历类中的所有函数?
【发布时间】:2025-12-01 03:40:01
【问题描述】:

一旦服务器检测到新消息,我需要一种方法让我的代码循环通过 onMessage() 函数。目前,它会发送一次响应,然后什么都不会发生。

如果我要手动循环返回 onMessage() 函数,它只会使用与第一次相同的存储响应并重复输出类似的响应。

请忽略代码中的随机变量:我删除了计算响应的长逻辑,因为它在这里应该无关紧要。

from fbchat import Client, log
from fbchat.models import *
import fileinput

import nltk
from nltk.stem.lancaster import LancasterStemmer
stemmer = LancasterStemmer()

import numpy
import tflearn
import tensorflow
import random
import json
import pickle

class chatbot(Client):

    def onMessage(self, author_id=None, message_object=None, thread_id=None, thread_type=ThreadType.USER, **kwargs):
        toggle = client.fetchThreadMessages(thread_id=client.uid, limit=3) # client.uid means its our own acc
        for message in toggle:
            pText=message.text.lower()
            print("test1")
        self.markAsRead(author_id)
        log.info("Message {} from {} in {}".format(message_object, thread_id, thread_type))  
        msgText = message_object.text.lower()
        print("test2")

    def getResponse(self, message, model, words, labels, data, thread_id, thread_type, author_id):
        print("test9")
        while True:
            results = model.predict([self.bag_of_words(message, words)])
            results_index = numpy.argmax(results)
            tag = labels[results_index]

            for tg in data["intents"]:
                if tg['tag'] == tag:
                    responses = tg['responses']

            finalResponse = random.choice(responses)
            run_once = 0
            while 1:
                if run_once == 0:
                    self.sendMessage(finalResponse, thread_id, thread_type, author_id)        
                    run_once = 1


    def sendMessage(self, response, thread_id, thread_type, author_id):
        print("test10")
        if 1 == 1:# (author_id!=self.uid):
                self.send(Message(text=response), thread_id=thread_id, thread_type=thread_type)
                self.markAsDelivered(author_id, thread_id)


client = chatbot("********", "****************")
client.listen()

【问题讨论】:

  • getResponse()sendMessage() 方法是否相关?您能改为显示listen() 的代码吗?
  • 我不知道你问了什么,但你可以通过dir(ClassName)dir(instance) 获取一个类中所有函数的列表。
  • @quamrana .listen() 是在 fbchat 中等待响应的一种形式。在此页面底部查找更多信息fbchat.readthedocs.io/en/stable/intro.html#listening-events

标签: python python-3.x server


【解决方案1】:

通过在代码底部添加更改来解决问题:

while True:
            if run_once == 0:
                if author_id != 'YOUR USER ID':
                    self.sendMessage(finalResponse, thread_id, thread_type, author_id)        
                    run_once = 1
                    print("testA")
                    return False
                return False
        return False



def sendMessage(self, response, thread_id, thread_type, author_id):
    print("test10")
    # (author_id!=self.uid):
    print(response)
    self.send(Message(text=response), thread_id=thread_id, thread_type=thread_type)
    self.markAsDelivered(author_id, thread_id)


client = chatbot("username", "password")
while True:
    client.listen()

【讨论】: