vohinc / laravel-botanalytics by casperlaitw

A Botanalytics Wrapper for Laravel
23
1
2
Package Data
Maintainer Username: casperlaitw
Maintainer Contact: casper.lai@sleepingdesign.com (Casper Lai)
Package Create Date: 2017-01-24
Package Last Update: 2017-01-26
Language: PHP
License: MIT
Last Refreshed: 2024-04-25 15:05:28
Package Statistics
Total Downloads: 23
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 1
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

Laravel Botanalytics

Build Status StyleCI

A Botanalytics Wrapper for Laravel

Botanalytics is a bot analytics service, improves Human-to-Bot interaction.

Install

Composer

To get the latest version

composer require vohinc/laravel-botanalytics

Add Provider

Include the provider within config/app.php

'providers' => [
    ...
    Vohinc\LaravelBotanalytics\BotanalyticsServiceProvider::class,
    ...
]

Publish Configuration

php artisan vendor:publish --provider=Vohinc\LaravelBotanalytics\BotanalyticsServiceProvider --tag=config

Config

Set Botanalytics Token

Add your botanalytics token to .env

BOTANALYTICS_TOKEN=botanalytics-token

Usage

Incoming Message

<?php
namespace App;

use Vohinc\LaravelBotanalytics\BotanalyticsFacade;
use Closure;

class BotAnalyticsMiddleware
{
    /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @return mixed
         */
        public function handle($request, Closure $next)
        {
            $body = $request->all();
            if (array_get($body, 'object') === 'page') {
                BotanalyticsFacade::facebook()->request([
                    'recipient' => null,
                    'message' => $body,
                ]);
            }
    
            return $next($request);
        }
}

Out-going Message

<?php
namespace App;

use Vohinc\LaravelBotanalytics\BotanalyticsFacade;
use Illuminate\Http\Request;

class WebhookController extends Controller
{
    public function request(Request $request)
    {
        $message = [
            'recipient' => [
                'id' => 'Sender ID',    
            ],
            'message' => [
                'text' => 'hello, world!',    
            ],
        ];
        
        // response $message to Facebook
        
        // Send to botanalytics
        BotanalyticsFacade::facebook()->request([
            'recipient' => array_get($message, 'recipient.id'),
            'message' => array_get($message, 'message'),
        ]);
    }
}

License

This package is licensed under the MIT license.