GithubHelp home page GithubHelp logo

Comments (3)

iGusev avatar iGusev commented on August 13, 2024

Via \TelegramBot\Api\Client::on you can catch any messages from users and react to them:

/**
 * Use this method to add an event.
 * If second will return true (or if you are passed null instead of closure), first one will be executed.
 *
 * @param \Closure $event
 * @param \Closure|null $checker
 *
 * @return \TelegramBot\Api\Client
 */
public function on(Closure $event, Closure $checker = null)
{
    $this->events->add($event, $checker);

    return $this;
}

For example, to process each message, you can do:

$bot->on(function($message) use ($bot){
        file_put_contents("1.txt", print_r($message,true));
}, function($message) use ($name){
 return true;
});

For processing command $name via $action closure you can use \TelegramBot\Api\Client::command or own code:

$bot->on(function (Message $message) use ($action) {
    preg_match('/^\/([^\s@]+)(@\S+)?\s?(.*)$/', $message->getText(), $matches);

    if (isset($matches[3]) && !empty($matches[3])) {
        $parameters = str_getcsv($matches[3], chr(32));
    } else {
        $parameters = [];
    }

    array_unshift($parameters, $message);

    $action = new ReflectionFunction($action);

    if (count($parameters) >= $action->getNumberOfRequiredParameters()) {
        $action->invokeArgs($parameters);
    }

    return false;
},
    function (Message $message) use ($name) {
        if (!strlen($message->getText())) {
            return false;
        }

        preg_match('/^\/([^\s@]+)(@\S+)?\s?(.*)$/', $message->getText(), $matches);

        return !empty($matches) && $matches[1] == $name;
    }
);

from api.

TheoKlein avatar TheoKlein commented on August 13, 2024

Thanks for your detailed answer!

from api.

MrOplus avatar MrOplus commented on August 13, 2024
> $bot->on(function($message) use ($bot){
>         file_put_contents("1.txt", print_r($message,true));
> }, function($message) use ($name){
>  return true;
> });

the type of the $message is not the Message , it's Update , if you want to process incoming messages your code should be something like this :

$bot->on(function(Update $update) use ($bot){
                $message = $update->getMessage();
                $content = $message->getText();
                file_put_contents('debug.txt',$content);
            }, function($message){
                return true;
            });

from api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.