pschocke / laravel-telegram-login-widget
Easily integrate Telegrams login widget into your Laravel application to send Telegram messages
Installs: 7 910
Dependents: 0
Suggesters: 0
Security: 0
Stars: 17
Watchers: 2
Forks: 4
Open Issues: 0
Requires
Requires (Dev)
- mockery/mockery: ^1.1
- orchestra/testbench: ~7
- phpunit/phpunit: ^9
- sempro/phpunit-pretty-print: ^1.0
README
Laravel Telegram Login Widget. Easily integrate Telegrams login widget to send Telegram messages.
You can view a full video of the installation process an usage here, where we build an app that sends Telegram notifications from start to finish.
Installation
Via Composer
composer require pschocke/laravel-telegram-login-widget
Then publish the configuration file
php artisan vendor:publish --tag=telegramloginwidget.config
Usage
First you have to create a bot at Telegram. After that set up your login widget in your frontend.
Create an env variable TELEGRAM_BOT_TOKEN
with your bots token
Create a route to handle the callback/redirect after the the successful connection between the user account and your telegram bot. Telegram uses a hash to allow you to verify the response is from Telegram. Here comes this package in play:
use pschocke\TelegramLoginWidget\Facades\TelegramLoginWidget; class TelegramCallbackController extends Controller { public function __invoke(Request $request) { if($telegramUser = TelegramLoginWidget::validate($request)) { // user is valid } // telegram response is not valid. Account connection failed } }
if you want more control over the response, you can use the validateWithError()
method to catch more fine tuned errors:
use pschocke\TelegramLoginWidget\Facades\TelegramLoginWidget; class TelegramCallbackController extends Controller { public function __invoke(Request $request) { $telegramUser = []; try { $telegramUser = TelegramLoginWidget::validateWithError($request); } catch(pschocke\TelegramLoginWidget\Exceptions\HashValidationException $e) { // the response is not from telegram } catch(pschocke\TelegramLoginWidget\Exceptions\ResponseOutdatedException $e) { // the response is outdated. } } }
At this stage, $telegramUser
contains a collection of all attributes Telegram provides: id, first_name, last_name, username, photo_url and auth_date.
echo $telegramUser->first_name; // Max echo $telegramUser->last_name; // Mustermann
Testing
$ composer test
Contributing
Please see contributing.md for details and a todolist.
Security
If you discover any security related issues, please email author email instead of using the issue tracker.
Credits
License
MIT. Please see the license file for more information.