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.05k stars 673 forks source link

New feature proposal #449

Closed imanghafoori1 closed 7 years ago

imanghafoori1 commented 7 years ago

web.php

Route::telegramWebHook('someControler@someMethod');
// or
Route::telegramWebHook(function(){

   // some php code here...
});

The hook url goes in config file. config/telegram.php :

'mybot' => [
            'username' => 'TelegramBot',
            'web_hook_url' => 'bot<some-token>/webhook',  
            'token'    => env('TELEGRAM_BOT_TOKEN', 'YOUR-BOT-TOKEN'),
            'commands' => [
                //Acme\Project\Commands\MyTelegramBot\BotCommand::class
            ],
        ],
irazasyed commented 7 years ago

What's the benefit? You can just use one dedicated post route for all your bots.

imanghafoori1 commented 7 years ago

Some people use the same laravel app serve both reqular website and a bot. The bot is an assistant for the web app. so the web.php gets crowded.

irazasyed commented 7 years ago

You can use routes/api.php for this then. I do the same thing and here's how it works:

Route::post('/telegram/{bot}/{token}/webhook', 'BotController@webhook')->name('bot-webhook');

// Ex:
// https://domain.com/api/telegram/mybot/12345/webhook
class BotController extends Controller
{
    /**
     * Webhook Handler
     *
     * @param $bot
     *
     * @return string
     */
    public function webhook($bot)
    {
        Telegram::bot($bot)->commandsHandler(true);

        return 'Ok';
    }
}
irazasyed commented 7 years ago

And here's my setWebhook method.

/**
 * Set Webhook
 *
 * @param $bot
 *
 * @return string
 */
public function setWebhook($bot)
{
    $token = Telegram::getBotConfig($bot)['token'];
    $webhook = route('bot-webhook', compact('bot', 'token'));

    Telegram::bot($bot)->setWebhook([ 'url' => $webhook]);

    return 'Done!';
}
imanghafoori1 commented 7 years ago

All bots using the same controller method... I think the should have their own controller because each one wants to do it's own thing...

Route::telegramBot('my_bot1', 'botController_1@handle');

Route::telegramBot('my_bot2', 'botController_2@handle');

each bot is now free to do it's own thing in it's dedicated controller...

irazasyed commented 7 years ago

You can already do that using the post routes. Just create a new one for each bot and its own controller. Totally up to you and flexible. I don't think we need another route method for this as it'll likely confuse people and restricts with the webhook path and other options. Thanks for the proposal, though.