Core: How to parse a message for a keyword(s)

Created on 29 Jul 2016  路  5Comments  路  Source: php-telegram-bot/core

Hey,

I can't see how I would parse a message for a keyword. For example if I wanted to warn users about language it could parse there text for a keyword / sentance then be able to do {whatever} and reply to them.

Thanks

Most helpful comment

Something like this:

<?php

namespace Longman\TelegramBot\Commands\SystemCommands;

use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Request;

class GenericmessageCommand extends SystemCommand
{
    private $bannedWords = ['what', 'is', 'love'];

    public function execute()
    {
        $message = $this->getMessage();
        $message_id = $message->getMessageId();
        $chat_id = $message->getChat()->getId();
        $user_mention = $message->getFrom()->tryMention();
        $text = $message->getText(true);

        if (!$message->getChat()->isPrivateChat()) {
            foreach ($this->bannedWords as $bannedWord) {
                if (strpos($bannedWord, $text)) {
                    return Request::sendMessage(['chat_id' => $chat_id, 'text' => $user_mention . ', please do not use bad language!', 'reply_to_message_id' => $message_id]);
                }
            }
        }

        return Request::emptyResponse();
    }
}

All 5 comments

In what context?
Could you give concrete examples please?

You can manually override and handle any case.

GenericmessageCommand.php is where you should make such keyword checks.

I'll look into generic message now, (Is there any data on it?)

but for an example say I wanted the bot to reply to anyone who uses the work f*ck with "please refrain from that language or you'll be banned"

I'm fine with the bot reading all the messages in the chat.

@jacklul Is it possible to extend the generic message Command or will I need to just add to it. (Sorry PHP is not my first language)

Something like this:

<?php

namespace Longman\TelegramBot\Commands\SystemCommands;

use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Request;

class GenericmessageCommand extends SystemCommand
{
    private $bannedWords = ['what', 'is', 'love'];

    public function execute()
    {
        $message = $this->getMessage();
        $message_id = $message->getMessageId();
        $chat_id = $message->getChat()->getId();
        $user_mention = $message->getFrom()->tryMention();
        $text = $message->getText(true);

        if (!$message->getChat()->isPrivateChat()) {
            foreach ($this->bannedWords as $bannedWord) {
                if (strpos($bannedWord, $text)) {
                    return Request::sendMessage(['chat_id' => $chat_id, 'text' => $user_mention . ', please do not use bad language!', 'reply_to_message_id' => $message_id]);
                }
            }
        }

        return Request::emptyResponse();
    }
}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NabiKAZ picture NabiKAZ  路  3Comments

nesttle picture nesttle  路  4Comments

Recouse picture Recouse  路  3Comments

mohsenshahab picture mohsenshahab  路  4Comments

NabiKAZ picture NabiKAZ  路  4Comments