Inspiration

There are many chat bot development frameworks out there, but non could give me the level of abstraction i needed and at the same time, they did not support some features that were specific to my needs, so I decided to write my own library.

What it does

  • It wraps around the Messenger APIs to make chat bot development a breeze.
  • It provides a developer with expressive ways of listening for different events sent to their webhook.
  • It provides a developer with different apis for building different chatbot features using messeger native elements.
  • It removes the friction of a developer having to deal with the messenger APIs directly.

How I built it

It is driven by a very simple but powerful event dispatching library Événement, Guzzle a PHP HTTP client that makes it easy to send HTTP requests and also utilizes the messenger API.

Challenges I ran into

No challenges

Accomplishments that I'm proud of

I feel proud for having built this library with many features to offer

What I learned

I learned how to use appropriate design patterns where necessary to achieve the best performance

What's next for Bionic

Am adding support for more messenger features and other messaging platforms and also port it to other languages i.e. python, node.js

Basic example

<?php
use Andre\Bionic\Bionic;
use Andre\Bionic\Plugins\Messenger\Messages\Message\Nlp;
use Andre\Bionic\Plugins\Messenger\Messages\Message\QuickReply;
use Andre\Bionic\Plugins\Messenger\Messages\Message\Text;
use Andre\Bionic\Plugins\Messenger\MessengerPlugin as Plugin;
use Andre\Bionic\Plugins\Messenger\Messages\EndPoint\Sender;
use Andre\Bionic\Plugins\Messenger\Messages\EndPoint\Recipient;
use Andre\Bionic\Plugins\Messenger\Messages\Message\Attachments\Image;

/* validate verify token needed for setting up web hook */
if (isset($_GET['hub_verify_token'])) {
    if ($_GET['hub_verify_token'] === 'your verify token') {
        echo $_GET['hub_challenge'];
        return;
    } else {
        echo 'Invalid Verify Token';
        return;
    }
}

$data = json_decode(file_get_contents('php://input'), true);

$config = [
    'page_access_token' => 'your page access token',
    // 'graph_api_version' => 'v2.10' optional and defaults to v2.10
];

$bionic = Bionic::initialize()
    ->setPlugin(Plugin::create($config));

// register your event listeners before calling the 'receive' method on the bionic instance
// $bionic->listen($event_name, $event_listener);
$bionic->listen('message.attachments.image', function (Plugin $plugin, Sender $sender, Recipient $recipient, Image $image, $channel){

    // $plugin - current plugin being used i.e. MessengerPlugin
    // $sender - sender of the message i.e. Messenger user
    // $recipient - recipient of the message i.e. Your facebook page
    // $image - Image attachment that was sent
    // $channel - event delivery channel, messaging or standby

    // this sends the attachment as a message back to the sender
    $plugin->sendAttachment($image, $sender);

})->listen('message.text', function (Plugin $plugin, Sender $sender, Recipient $recipient, Text $text, QuickReply $quickReply = null, Nlp $nlp = null, $channel) {

      $text->getText();
      if ($quickReply)
          $quickReply->getPayload();

      if ($nlp)
          $nlp->getEntities();

      $plugin->sendText($text, [], $sender);

})->listen('exception', function (Exception $exception) {

});

$bionic->receive($data);
return http_response_code(200);

Built With

  • evenement
  • guzzle
  • messenger-api
  • php
Share this project:

Updates