irazasyed / telegram-bot-sdk

🤖 Telegram Bot API PHP SDK. Lets you build Telegram Bots easily! Supports Laravel out of the box.
https://telegram-bot-sdk.com
BSD 3-Clause "New" or "Revised" License
3.02k stars 669 forks source link

Default command, triggerCommand() from a controller. #121

Closed thomasgroch closed 8 years ago

thomasgroch commented 8 years ago

I am developing a bot that responds to the command /music {youtube_link}. So far my command works perfectly. I would like the system invoked by default "/music" when it found a link on the user menssage.

I realized in the documentation that I implement this in the controller after: $update = Telegram::commandsHandler(true);

Commands Handler method will always return an Update object (Whether it processes the command or not), So you can use it to process further or for anything.

So, on my Controller I try:

    public function webhook($token = null)

      $update = Telegram::commandsHandler(true);

      $chat_id = $update->getMessage()->getChat()->getId();
      $text_message = $update->getMessage()->getText();

      if( $this->_isYoutubeLink($text_message) && FALSE === strpos($text_message, 'music') ) { 
            Telegram::sendMessage([
                  'chat_id' => $chat_id,
                  'text' => 'By default I will allways return the audio from link you send me. :)'
            ]);

            Telegram::triggerCommand('music', $text_message);

      }

I got no error message is recorded in the storage files / logs / laravel.log and /var/log/nginx/error.log And unfortunately the command is not triggered.

Note: The message "By default I will... " is sent normally.

Since this is a very common scenario, I decided to open this topic to know the solution that you have given.

irazasyed commented 8 years ago

triggerCommand() this method cannot be used outside of the command class as it's a protected method.

thomasgroch commented 8 years ago

@irazasyed, I put this function only to illustrate the problem.

Someone had any different approach?

irazasyed commented 8 years ago

I see. Well, I think i didn't quite understand what was the problem here then?

Are you asking how to debug?

The SDK throws various Exceptions. So you can use try/catch method to catch any exceptions and handle the error accordingly.

irazasyed commented 8 years ago

Check out the Exceptions directory for all the exception it throws.

thomasgroch commented 8 years ago

The problem is I can not trigger the command when the user did not enter a command. All I'm trying to do is take advantage of the command code "/music {youtube}-link" to handle messages that are youtube links so it be a default behavior.

irazasyed commented 8 years ago

In that case, I'd recommend you to setup a class or a trait that you can use it inside your command class as well as inside your controller.

Something like this:

public function webhook($token = null) {
    $update = Telegram::commandsHandler(true);

      $chat_id = $update->getMessage()->getChat()->getId();
      $text_message = $update->getMessage()->getText();

      if( $this->_isYoutubeLink($text_message) && FALSE === strpos($text_message, 'music') ) {
            Telegram::sendMessage([
                  'chat_id' => $chat_id,
                  'text' => 'By default I will allways return the audio from link you send me. :)'
            ]);

            $music = new App\Services\Music($text_message);
            $response = $music->getMusic();
      }
}

Obviously, the above code is roughly done based on your code. I have no idea what your bot does. But you get the idea.

thomasgroch commented 8 years ago

This way I won't be able to call $this->replyWithAudio for instance. I was thinking that with the same say I could do something like:

public function webhook($token = null) {
    $update = Telegram::commandsHandler(true);
    $text_message = $update->getMessage()->getText();

    $commandClass = "app\TelegramCommands\MusicCommand";
    if( class_exists($commandClass) ){
         $obj = new $commandClass($update);
         $obj->handle( $text_message );
    }
}

But it's also not possible.

irazasyed commented 8 years ago

Yeah! You won't be able to do that but all the replyWith<Method> methods are simply wrappers that use the current message's chat id. You can just manually pass that which you get from the update object anyway.

But I'll see if we can open that trigger method in next release.

irazasyed commented 8 years ago

Closed in last commit. Will be released in V3.