php-telegram-bot / core

PHP Telegram Bot based on the official Telegram Bot API
MIT License
3.87k stars 951 forks source link

how to get any message when send by user #299

Closed poyaz closed 7 years ago

poyaz commented 7 years ago

Hi,

first question

I want when user send message (without /commands) like:

test test1 test2

then handle this message.

I just find handle message when user send like

/name test

noplanman commented 7 years ago

The command you need to override is GenericmessageCommand

In the execute method you can then get the text and do with it what you want 👍

poyaz commented 7 years ago

Sry i change file GenericmessageCommand

return Request::emptyResponse();

to return

Request::sendMessage($data);

but can't get message without '/'

noplanman commented 7 years ago

This example execute method in GenericmessageCommand will simply return the entered text (which you can get with $this->getMessage()->getText();):

public function execute()
{
    $message = $this->getMessage();

    return Request::sendMessage([
        'chat_id' => $message->getChat()->getId(),
        'text'    => $message->getText(),
    ]);
}
poyaz commented 7 years ago

sry dude, but nothing change.

should change any file like hook.php or ...

my GenericmessageCommand file:

namespace Longman\TelegramBot\Commands\SystemCommands;

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

class GenericmessageCommand extends SystemCommand
{
    protected $name = 'Genericmessage';
    protected $description = 'Handle generic message';
    protected $version = '1.0.2';
    protected $need_mysql = true;

    public function executeNoDb()
    {
        //Do nothing
        return Request::emptyResponse();
    }

    public function execute()
    {
        $message = $this->getMessage();

        return Request::sendMessage([
            'chat_id' => $message->getChat()->getId(),
            'text'    => $message->getText(),
        ]);
    }
}
jacklul commented 7 years ago

Probably not using DB...?

poyaz commented 7 years ago

ty @jacklul

I not using Db.

ty very much

noplanman commented 7 years ago

@pooya-az Is it working now? What did you change to make it work?

poyaz commented 7 years ago

I add your code in executeNoDb() because $need_mysql is true value

noplanman commented 7 years ago

Hmm, executeNoDb gets executed when there is no MySQL connection available.

If you don't use the MySQL database, just set $mysql to false.

Glad to see that it's working for you 👍

poyaz commented 7 years ago

@noplanman ty very much for answer my issue.