skip to Main Content

I wrote a bot to delete some words from a telegram group.

function filter_messages() {
    global $telegram;

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

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

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

}

it works when a user sends a message like “hello”. Robot deletes the message.
but when he sends “hello guys”
the robot does not delete the messeage.

2

Answers


  1. Chosen as BEST ANSWER

    I used strpos() function.

    if(strpos($all_words,$bad_words)!==false){
        deletMessage();
    }
    

  2. 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();
        }
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search