defstudio / telegraph

Telegraph is a Laravel package for fluently interacting with Telegram Bots
MIT License
697 stars 118 forks source link

Separate webhookhandler for specific bot. #401

Closed pou closed 9 months ago

pou commented 1 year ago

I'm going to add an option to make it possible to set a specific webhookandler for each bot.

Do you have any wishes?

Vahagn-99 commented 1 year ago

i solved this problem like this.

  1. Create The seperate Provider and revrite the original webhook route handler like htis class CustomTelegraphWebhookProvider extends ServiceProvider { /**

    • Register services. */ public function register(): void { // }

    /**

    • Bootstrap services. */ public function boot(): void { Route::post('/telegraph/{token}/webhook', [WebhookController::class, 'handle'])->name('telegraph.webhook'); } }

then i create the that controller like this (my case) class WebhookController extends Controller { public function handle(Request $request, TelegraphWebhookDispatcher $dispatcher, string $token): Response { $req = $request->all(); $chatId = $req['message']['chat']['id'];

    /** @var TelegraphBot $bot */
    $bot = TelegraphBot::fromToken($token);
    /** @var TelegraphChat $chat */
    $chat = $bot->chats()->where('chat_id', $chatId)->first();
    $widget = $bot->widget ?? $chat->widget;

    /** @var WebhookHandler $handler */
    $handler = $dispatcher->dispatch($widget);

    $handler->handle($request, $bot);

    return \response()->noContent();
}

} how to implement the logic depend on your scenario but the goal is create some resolver that or kernel class with should be responsible for dispatching concrate handler