立川企業社
立川企業社

立川創意工作室的創始者,同時也是 PHP 的 Laravel 框架的喜愛者,目前用 Voyager 套件用成後台,有了它至少減少約 40% 的時間做後台界面,喜愛它的就看看吧!

Build Your Facebook Messenger Chatbot Using Lumen

Use Lumen to build a bot that belongs to your Facebook Messanger, all the answers depend on how you do it, it is actually a response bot, as long as there is a question, it will definitely respond, the middle way depends on whether you use wit.ai or Dialogflow can be used, there are many ways to do it, at least it will not be too monotonous

Here I use Lumen, information about it can be found here

  • Versions 5.8 and 5.2 are different, note that only the routing is different
  • The reason for choosing it is that it does not have a front-end interface itself

Information about Lumen 5.8

Because the function of DB is used, its annotations must be removed first

  • Open bootstrap/app.php
  • Find // $app->withFacades();, just remove the previous annotation

Build your route ( Route )

Its path is routes/web.php

  1. The first one is to use get, mainly for the verification of messanger
  2. The second uses post, this is to pass the message to the messanger server
 $router->get('/webhook', 'BotController@verify_token');
$router->post('/webhook', 'BotController@handle_query');

Create a Controller

This has already made the basic process, the whole copy

 <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class BotController extends Controller {

    /**
     * The verification token for Facebook
     *
     * @var string
     */protected $token;

    public function __construct(){$this->token = env('BOT_VERIFY_TOKEN');}

    /**
     * Verify the token from Messenger. This helps verify your bot.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response|\Laravel\Lumen\Http\ResponseFactory
     */public function verify_token(Request $request){$mode = $request->get('hub_mode');$token = $request->get('hub_verify_token');

        if ($mode === "subscribe" && $this->token and $token === $this->token) {return response($request->get('hub_challenge'));}

        return response("Invalid token!", 400);}

    /**
     * Handle the query sent to the bot.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response|\Laravel\Lumen\Http\ResponseFactory
     */public function handle_query(Request $request){$entry = $request->get('entry');

        $sender = array_get($entry, '0.messaging.0.sender.id');// $message = array_get($entry, '0.messaging.0.message.text');
$this->dispatchResponse($sender, 'Hello world. You can customise my response.');

        return response('', 200);}

    /**
     * Post a message to the Facebook messenger API.
     *
     * @param integer $id
     * @param string $response
     * @return bool
     */protected function dispatchResponse($id, $response){$access_token = env('BOT_PAGE_ACCESS_TOKEN');$url = "https://graph.facebook.com/v2.6/me/messages?access_token={$access_token }";

        $data = json_encode(['recipient' => ['id' => $id],'message' => ['text' => $response]]);

        $ch = curl_init($url);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $data);curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); $result = curl_exec($ch);curl_close($ch);

        return $result;}
}

Finally, modify .env

Note these two lines

  • BOT_VERIFY_TOKEN=”This is the string to be used for verification”
  • BOT_PAGE_ACCESS_TOKEN=”This is the access token for the fan group selected by Messenger”
 APP_ENV=local
APP_DEBUG=true
APP_KEY=
APP_TIMEZONE=UTC

BOT_VERIFY_TOKEN="INSERT_TOKEN_HERE"
BOT_PAGE_ACCESS_TOKEN="INSERT_ACCESS_TOKEN_HERE"

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
QUEUE_DRIVER=sync

So you can have a chatbot for Facebook Messenger

CC BY-NC-ND 2.0

Like my work?
Don't forget to support or like, so I know you are with me..

Loading...
Loading...

Comment