如何以编程方式检测和响应聊天消息中的关键字

How to programmatically detect and respond to a key word(s) in chat messages

我的问题是我对编码还很陌生,我不太确定该怎么做。我只知道制作响应以 ! 开头的命令的机器人的基础知识。但我想知道如何制作一个可以检测任何单词并做出响应的机器人。

例如:我有一个使用 ch.py 库的 chatango 聊天机器人,在 onMessage class 下我有

    try:
        cmd, args = message.body.split(" ", 1)
    except:
        cmd, args = message.body, ""

    if cmd[0] == "!":
        prfx = True
        cmd = cmd[1:]
    else:
        prfx = False

    if cmd.lower() == "test" and prfx:
        room.message("This is a test code.")

但我也希望 room.message 基于在发布的消息中找到的特定关键短语。

抱歉,我缺乏经验,我只是找不到任何关于我正在寻找的内容的合适信息。

很简单。

s = "It's not safe to go alone. Take this."
if 'safe' in s:
    print('The message is safe.')

if s.find('safe') != -1:
    print('This message is safe.')

使用 in 更快。使用 find 也可以,但 find 的真正目的是 return 找到的字符串的索引。