php-telegram-bot / core

PHP Telegram Bot based on the official Telegram Bot API
MIT License
3.89k stars 955 forks source link

How to set 'default' command? #178

Closed deios0 closed 8 years ago

deios0 commented 8 years ago

I want to reply users on ANY text, not only with given command. How can I do it? Right now if the bot doesn't see a command, in the beginning, it replies nothing.

juananpe commented 8 years ago

When the user types something in your bot, that message will be handled by Telegram::handle. This method in turn will call Telegram::processUpdate. There, the code will check if the message is a command (starts with /) or a generic message.

In the first case, it will also check if it is a known command to your bot. If so, it will call it. If not, it will call SystemCommands/GenericCommand.php.

In the the second case (the user sent a regular message, not a command), the processUpdate method will call this class: SystemCommands/GenericmessageCommand.php (specifically, it's execute() method, here https://github.com/akalongman/php-telegram-bot/blob/master/src/Commands/SystemCommands/GenericmessageCommand.php#L47) As you can see, that method -by default- will return an empty Response.

You might want to overwrite both Commands to fit your needs (GenericCommand and GenericmessageCommand).

Hope it helps.

deios0 commented 8 years ago

Ok, great. I tried to copy both classes in my Commands directory in order to overwrite them. But nothing happened, should I modify the code right in src/Commands/SystemCommands/.... directory? Because in this case, I will not be able to get updates.

I already did the same trick with StartCommand and everything worked great, but it doesn't work with GenericmessageCommand.php.

Thank you for your help.

deios0 commented 8 years ago

And this trick works with GenericCommand.php, so I can overwrite it. But not for GenericmessageCommand.php for some reasons.

juananpe commented 8 years ago

This is how I configured my bot to do the same that you are trying to achieve:

   // Add an additional commands path
    $telegram->addCommandsPath($your_CustomCommands_directory_path);

error_log("It works: " . __METHOD__ . "\n", 3, "/tmp/error.log");

(just before the return )

and then, from your terminal, launch this command: $ tail -f /tmp/error.log

Now, interact with your bot. You should see something like this:

 ==> /tmp/error.log <==
It works: Longman\TelegramBot\Commands\SystemCommands\GenericmessageCommand::execute
deios0 commented 8 years ago

Ok, I see, I did the following:

$logger = new Logger('Telegram-Messenger-Bot-Logger');
        $logger->pushHandler(new StreamHandler('../storage/logs/telegram-messenger-bot.log', Logger::DEBUG));
        $logger->addInfo('Generic Request ', ["Message here."]);

        $data["text"] = "Let me show you.";
        return Request::sendMessage($data);

And I see the log message in my logs. But I don't get 'Let me show you' in my Telegram client.

deios0 commented 8 years ago

Sorry, my mistake. Thank you!