iKlsR / laravel-telegram-login-widget

Easily integrate Telegram login and verify the response

Home Page:https://patrickschocke.de/sending-telegram-messages-with-laravel-notifications

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TelegramLoginWidget

Latest Version on Packagist Total Downloads run-tests Quality Score StyleCI codecov

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.

About

Easily integrate Telegram login and verify the response

https://patrickschocke.de/sending-telegram-messages-with-laravel-notifications

License:MIT License


Languages

Language:PHP 100.0%