Closed imanghafoori1 closed 7 years ago
What's the benefit? You can just use one dedicated post route for all your bots.
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.
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';
}
}
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!';
}
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...
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.
web.php
The hook url goes in config file. config/telegram.php :