📜 ⬆️ ⬇️

BotAuth - login and register using bots



BotAuth is a package that allows you to log in using a Vkontakte, FaceBook, Telegram bot.

The main task of BotAuth is to make it easier for visitors to access Web sites / PWA through social networks.
')
While soc. networks do not implement the feedback of native mobile applications with the web site, developers have to send the user to the browser, where they need to enter their login and password again.

Using bots, you can get feedback (callback) from the native application, thereby not to force to enter the username and password social. network in the browser.

Demo - https://laravel.zetrider.ru/botauth
GitHub - https://github.com/zetrider/BotAuth

View links:

https://vk.me/ https://t.me/ https://m.me/ 

open a mobile application to start a dialogue with the bot. The visitor does not have to re-enter the username and password in the browser.

You can connect bots:


Installation
  1. composer require zetrider / botauth
  2. Enable package in config / app.php
    Provider
     ZetRider\BotAuth\BotAuthServiceProvider::class, 

    Facade
     'BotAuth' => ZetRider\BotAuth\Facades\BotAuth::class, 

  3. Copy config. file
     php artisan vendor:publish --tag=botauth-config 

    if necessary
     php artisan vendor:publish --tag=botauth-views 

     php artisan vendor:publish --tag=botauth-migrations 

  4. Specify for the necessary soc. networking link in config / botauth.php in the link parameter.
  5. Fill the ENV file with bots 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 

  6. Run migrations
     php artisan migrate 

  7. In Middleware VerifyCsrfToken add address exclusion for callback, default is botauth / callback / * '
     protected $except = [ 'botauth/callback/*' // Except callback Csrf middleware ]; 

  8. For your User model, add the trait:
     use ZetRider\BotAuth\Traits\BotAuthUserTrait; 

    which will add a relationship with user logins from social. nets


Connect bots:

In contact with
  1. Open your community settings or create a new vk.com/groups?w=groups_create
  2. In the community settings, open the section “Settings” - “Working with API”
  3. Create an access key, select the item “Allow the application access to community messages”, write down the key, you need to specify it in .env BOTAUTH_VKONTAKTE_API_TOKEN
  4. On the same page, select Callback API, select API Version 5.95 , enter the address of your site in the Address field, the default example
     https://DOMAIN/botauth/callback/vkontakte 
  5. Below indicate the line that the server should return to .env BOTAUTH_VKONTAKTE_API_CONFIRM
  6. In the field “Secret key” invent any secret key, specify in .env BOTAUTH_VKONTAKTE_API_SECRET
  7. After filling in all the keys in .env, click "Verify"
  8. On the same page, open the “Event Types” tab, select “Inbox Messages”
  9. Open the settings of the community, the "Messages" item, turn on the "community messages"
  10. Open the community settings, the "Messages" item - "Settings for the bot", enable the "Bots features"

The bot is ready to go.

An example of a direct link to the dialogue with the bot
 https://vk.me/zetcode 


Telegram
  1. Create your bot via @BotFather
  2. Remember the key, specify in .env BOTAUTH_TELEGRAM_API_TOKEN
  3. Add a web hook through
     https://api.telegram.org/botYOUR_TOKEN/setWebhook?url=https://DOMAIN/botauth/callback/telegram 

    replace YOUR_TOKEN with your token, DOMAIN with your domain
  4. 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 the dialogue with the bot
 https://t.me/BotAuthBot 


Facebook
  1. You should have a page created, if it doesn’t exist, add www.facebook.com/pages/creation/?ref_type=universal_creation_hub
  2. Add a new developers.facebook.com/apps application
  3. In the settings of the application, select “Basic”, copy the “Secret of the application” in .env BOTAUTH_FACEBOOK_API_SECRET
  4. In the settings of the application, you need to add the product "Messenger"
  5. In the settings of the product “Messenger” create an access token, specify it in .env BOTAUTH_FACEBOOK_API_TOKEN
  6. In the settings of the product "Messenger" create a web hook, in the callback URL specify
     https://DOMAIN/botauth/callback/facebook 
    replace DOMAIN with your domain
  7. In the field “Confirm marker” enter any text, save it in .env BOTAUTH_FACEBOOK_API_CONFIRM
  8. In the "Subscription Fields" options, select "messages"
  9. Click "Confirm"
  10. After confirming the server in the settings of the web hooks, select the page, click "Subscribe", select the page
  11. In the “Messenger App Test” window, next to “pages_messaging”, click “Add to Application”

The bot is ready to work, but is available only to administrators.

After confirmation of the application, it will be available to all visitors. Submit the application for moderation.

An example of a direct link to the dialogue with the bot
 https://m.me/zetridercode 


Important:

  1. The site should work on https
  2. Facebook bot returns a PSID that does not match the public user ID.
  3. By default, the bot controller works with the model \ App \ User. If you have another case, simply create your controller and model based on examples from the repository. Model , Controller

How to add your provider:

Create your class that inherits the abstract class.

 ZetRider\BotAuth\AbstractProvider 

Example example / ExampleProvider.php

Add a service provider, for example AppServiceProvider in the boot method

 // Register example proider BotAuth::extend('example', function() { return new \Path\To\Your\Class\ExampleProvider(); }); 

The provider will handle requests in the callback at

 https://.../botauth/callback/example 

Developments

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')); }); 

Source: https://habr.com/ru/post/455104/


All Articles