Build Your Facebook Messenger Chatbot Using Lumen
IPFS
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
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
- The first one is to use get, mainly for the verification of messanger
- 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
Like my work? Don't forget to support and clap, let me know that you are with me on the road of creation. Keep this enthusiasm together!
- Author
- More