首页 > 解决方案 > 删除电报上带有特定单词的消息

问题描述

我写了一个机器人从电报组中删除一些单词。

function filter_messages() {
    global $telegram;

    $bad_words = [
        'hello',
        'hi'
    ];

    $all_words = end( explode( ' ', $telegram->Text() ) );

    if ( in_array( $all_words, $bad_words ) ) {
        deletMessage();
    }

}

当用户发送“你好”之类的消息时,它会起作用。机器人删除消息。但是当他发送“大家好”时,机器人不会删除该消息。

标签: phptelegram-bot

解决方案


function filter_messages() {
    global $telegram;

    $bad_words = [
        'hello',
        'hi'
    ];

    //Make the array $badword to a (commaseparated) string
    $badwords_to_string = implode(",", $bad_words);

    //And then check if the any of the bad words in the telegramText exists
    //based on that $telegram->Text() is a string
    if (strpos($badwords_to_string, $telegram->Text()) !== false) {
        deletMessage();
    }

}

推荐阅读