
BotAuth - login and registration using bots
- Tutorial

BotAuth - a package that allows you to log in using the bot Vkontakte, FaceBook, Telegram.
The main objective of BotAuth is to simplify visitors to Web sites / PWA login through social networks.
While social. Networks do not provide feedback from native mobile applications to a website; developers have to send a user to a browser, where they need to enter their login and password again.
Using bots, you can get feedback (callback) from the native application, thereby not forcing to enter the username and password of the social. network in the browser.
Demo - https://laravel.zetrider.ru/botauth
GitHub - https://github.com/zetrider/BotAuth
Links of the form:
https://vk.me/ https://t.me/ https://m.me/
open a mobile application to start a dialogue with the bot. The visitor will not have to re-enter the username and password in the browser.
You can connect bots:
- In contact with
- Telegram
- Your own provider (example below)
Installation
- composer require zetrider / botauth
- Connect the package in config / app.php
ProviderZetRider\BotAuth\BotAuthServiceProvider::class,
Facade'BotAuth' => ZetRider\BotAuth\Facades\BotAuth::class,
- Copy the config. file
php artisan vendor:publish --tag=botauth-config
if necessaryphp artisan vendor:publish --tag=botauth-views
php artisan vendor:publish --tag=botauth-migrations
- Indicate for the necessary social. networks link in config / botauth.php in the link parameter.
- Fill ENV file with bot keys
BOTAUTH_VKONTAKTE_API_SECRET BOTAUTH_VKONTAKTE_API_TOKEN BOTAUTH_VKONTAKTE_API_CONFIRM BOTAUTH_TELEGRAM_API_TOKEN BOTAUTH_TELEGRAM_PROXY BOTAUTH_FACEBOOK_API_SECRET BOTAUTH_FACEBOOK_API_TOKEN BOTAUTH_FACEBOOK_API_CONFIRM
- Run migrations
php artisan migrate
- In Middleware VerifyCsrfToken add an address exception for callback, default is botauth / callback / * '
protected $except = [ 'botauth/callback/*' // Except callback Csrf middleware ];
- For your User model, add a trait:
use ZetRider\BotAuth\Traits\BotAuthUserTrait;
which will add a relationship with user logins from the social. networks
Connecting bots:
In contact with
The bot is ready to go.
An example of a direct link to a dialogue with a bot
- Open your community settings or create a new vk.com/groups?w=groups_create
- In the community settings, open the "Settings" section - "Work with the API"
- Create a passkey, select "Allow the application to access community messages", write down the key, it must be specified in .env BOTAUTH_VKONTAKTE_API_TOKEN
- On the same page, select the Callback API, select “API Version” 5.95 , specify the callback address of your site in the “Address” field, default example
https://DOMAIN/botauth/callback/vkontakte
- Below specify the line that the server should return to .env BOTAUTH_VKONTAKTE_API_CONFIRM
- In the "Secret key" field, enter any secret key, specify in .env BOTAUTH_VKONTAKTE_API_SECRET
- After filling out all the keys in .env, click the "Confirm" button
- On the same page, open the “Event Types” tab, select “Inbox”
- Open the community settings, the item "Messages", enable the "community messages"
- Open the community settings, the item "Messages" - "Settings for the bot", turn on "Bot features"
The bot is ready to go.
An example of a direct link to a dialogue with a bot
https://vk.me/zetcode
Telegram
The bot is ready to go.
An example of a direct link to a dialogue with a bot
- Create your bot via @BotFather
- Remember the key, specify in .env BOTAUTH_TELEGRAM_API_TOKEN
- Add a web hook via
https://api.telegram.org/botYOUR_TOKEN/setWebhook?url=https://DOMAIN/botauth/callback/telegram
replace YOUR_TOKEN with your token, DOMAIN with your domain - If necessary, specify the proxy in .env BOTAUTH_TELEGRAM_PROXY , for example socks5h: //127.0.0.1: 1080
The bot is ready to go.
An example of a direct link to a dialogue with a bot
https://t.me/BotAuthBot
Facebook
The bot is already ready for work, but is available only to administrators.
After confirming the application, it will become available to all visitors. Submit the application for moderation.
An example of a direct link to a dialogue with a bot
- You must have created a page, if it is not, add www.facebook.com/pages/creation/?ref_type=universal_creation_hub
- Add the new developers.facebook.com/apps app
- In the application settings, select “Basic”, copy the “Application Secret” to .env BOTAUTH_FACEBOOK_API_SECRET
- In the application settings you need to add the product "Messenger"
- In the “Messenger” product settings, create an access token, specify it in .env BOTAUTH_FACEBOOK_API_TOKEN
- In the settings of the Messenger product, create a web hook, specify in the callback URL
replace DOMAIN with your domainhttps://DOMAIN/botauth/callback/facebook
- In the "Confirm marker" field, specify any text, save it in .env BOTAUTH_FACEBOOK_API_CONFIRM
- In the “Subscription Fields” options, select “messages”
- Click Confirm
- After confirming the server in the web hook settings, select the page, click "Subscribe"
- In the “Check Messenger Application” window, next to “pages_messaging”, click “Add to Request”
The bot is already ready for work, but is available only to administrators.
After confirming the application, it will become available to all visitors. Submit the application for moderation.
An example of a direct link to a dialogue with a bot
https://m.me/zetridercode
Important:
- The site should work on https
- The Facebook bot returns a PSID that does not match the public user ID.
- By default, the bot controller works with the \ App \ User model. If you have a different case, just create your controller and model based on examples from the repository. Model , Controller
How to add your provider:
Create your own class that inherits an abstract class
ZetRider\BotAuth\AbstractProvider
Example example / ExampleProvider.php
Add a provider to the service, for example, AppServiceProvider in the boot method
// Register example proider
BotAuth::extend('example', function() {
return new \Path\To\Your\Class\ExampleProvider();
});
The provider will process requests in a callback at
https://.../botauth/callback/example
Events
Event upon successful processing of a new message from the bot
// Catch bot callback
\Event::listen(\ZetRider\BotAuth\Events\MessageNewEvent::class, function($event)
{
$provider = $event->provider; // ZetRider\BotAuth\AbstractProvider
$slug = $provider->getProviderSlug();
$data = $provider->getCallbackResponse();
$user = $provider->getUser();
$text = $provider->getText();
// You can send a message
// $provider->sendMessage(__('Back to web site'));
});