Python 3 : find item in list and answer based on that

标签 python python-3.x

我正在尝试制作一个 python 脚本,使用 fbchat 监听 Facebook 聊天并搜索单词 'cf'。如果在聊天中检测到该单词,我想发送一条预定义消息 Answer1。请参阅下面的代码和我得到的错误:

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

wordImLookingFor = ['cf']

Answer1 =['Hello! how can i help you']

# 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:
            if word in message_object:
                print("The word is in the list!")
                self.send(Message(text=answer1), thread_id=thread_id, thread_type=ThreadType.USER)


             else:
                print("The word is not in the list!")



client = EchoBot('user', 'pass')


client.listen()

Exception in parsing ... Traceback (most recent call last): ... File "C:/Users/John/Downloads/fbd2.py", line 22, in onMessage if word in message_object: TypeError: argument of type 'Message' is not iterable

最佳答案

这是 fbchat.models.Message 的 API

我相信您正在寻找文本字段

if word in message_object.text:
    print("The word is in the list!")

编辑:

简单的解决方案

对于您的下一个错误,in 关键字需要单个字符串,而不是字符串列表。由于您要在消息中查找多个关键字,因此请对列表中的每个单词执行该 case 语句。

if author_id != self.uid:
    for word in wordImLookingFor:
        if word in message_object.text:
            print("The word is in the list!")
            self.send(Message(text=answer1), thread_id=thread_id, thread_type=ThreadType.USER)
        else:
            print("The word is not in the list!")

规模化解决方案

由于您最终将搜索一组多个关键字,并且可能每个关键字都应得出不同的答案,因此您可以通过创建关键字:答案字典来节省一些复杂性。

keywords = {'cf': 'Hello! how can i help you', 'key2': 'Answer2'}

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 author_id != self.uid:
            for word in message_object.text.split():
                try:
                    self.send(Message(text=keywords[word]), thread_id=thread_id, thread_type=ThreadType.USER)
                    print(word + "is in the list!")
                except KeyError:
                    print(word + "is not in the list!")

关于Python 3 : find item in list and answer based on that,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51731698/

相关文章:

python - pandas to_csv 将 str 列转换为 int(或 float)

python - 检查用户是否有discord.py的权限

嵌套循环内的python变量重新分配不起作用

html - 使用 Beautifulsoup 提取下一个和不同标签的内容

python - 如何统计某一列的所有值?

python - 合并元组列表中的元组

python - 存储来自多行的唯一子字符串

python - numpy 保存/加载损坏数组

python - 使用正则表达式按字符拆分

python - 你如何在 Python 中使用 input 输入整数