2017 © Pedro Peláez
 

library messenger

Component to allow the comunication between messenger bot and your application.

image

quangtam/messenger

Component to allow the comunication between messenger bot and your application.

  • Tuesday, January 24, 2017
  • by quangtam
  • Repository
  • 1 Watchers
  • 0 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Scrutinizer Code Quality Code Coverage Build Status Total Downloads Latest Stable Version License, (*1)

Facebook Messenger

This repo contain some tools to work with facebook messenger bot and laravel/lumen. This repo work on beta of facebook messenger. Some big change are coming for sure but I guess that can help you., (*2)

Table of contents

  1. Installation
  2. Implement contract
  3. Change service provider
  4. Example of MessengerContract implementation
  5. Create application
  6. Host your application
  7. Facade
    1. sendTextMessage
    2. sendImageMessage
    3. getCurrentUserProfile
    4. sendCard
    5. persistMenu
  8. Example

Installation for Laravel

composer required distilleries/messenger, (*3)

Add Service provider to bootstrap/app.php:, (*4)

``` php, (*5)

$app->register(Distilleries\Messenger\MessengerLumenServiceProvider::class);, (*6)



##Installation for Lumen `composer required distilleries/messenger` Add Service provider to `config/app.php`: ``` php 'providers' => [ \Distilleries\Messenger\MessengerServiceProvider::class, ]

And Facade (also in config/app.php) replace the laravel facade Mail, (*7)

``` php 'aliases' => [ 'Messenger' => 'Distilleries\Messenger\Facades\Messenger' ], (*8)



## Implement contract To easily implement the fonctionality for your application I created a `Distilleries\Messenger\Contracts\MessengerReceiverContract`. | Event | Method | Description | | ----- | ------ | ------------| | messaging_optins | receivedAuthentication | Subscribes to Authentication Callback via the Send-to-Messenger Plugin | | message | receivedMessage | Subscribes to Message Received Callback | | message_deliveries | receivedDeliveryConfirmation | Subscribes to Message Delivered Callback | | messaging_postbacks | receivedPostback | Subscribes to Postback Received Callback | | all other | defaultHookUndefinedAction | Call when the other methods was no called | ## Change service provider To change the class use go to `app/Providers/MessengerServiceProvider.php` and change the class inside the share function. ```php $this->app->singleton('Distilleries\Messenger\Contracts\MessengerReceiverContract', function ($app) { return new MyMessengerClass(); });

Example of MessengerContract implementation


class MyMessengerClass implements MessengerContract { public function receivedAuthentication($event) { $senderID = $event->sender->id; Messenger::sendTextMessage($senderID, "Authentication successful"); } public function receivedMessage($event) { $senderID = $event->sender->id; Messenger::sendTextMessage($senderID, 'Test'); Messenger::sendImageMessage($senderID, env('APP_URL') . '/assets/images/logo.png');, Messenger::sendCard($senderID, [ 'template_type' => 'generic', 'elements' => [ [ "title" => "Messenger Boilerplate", "image_url" => env('APP_URL') . '/assets/images/logo.png', "subtitle" => "example subtitle", 'buttons' => [ [ 'type' => "web_url", 'url' => "https://github.com/Distilleries/lumen-messenger-boilerplate", 'title' => "Come download it!" ] ] ] ] ]); } public function receivedDeliveryConfirmation($event) { $senderID = $event->sender->id; Messenger::sendTextMessage($senderID, 'Test'); } public function receivedPostback($event) { $senderID = $event->sender->id; Messenger::sendTextMessage($senderID, 'Test'); } }

Create application

Follow the messenger documentation to create the app https://developers.facebook.com/docs/messenger-platform/quickstart., (*9)

  • For the webhook uri use /webhook
  • For the VALIDATION_TOKEN, generate a random key

After the application created and the page created and associated copy the .env.example to .env, (*10)

    VALIDATION_TOKEN=
    PAGE_ACCESS_TOKEN=

Host your application

You have to host your application to become use it. Facebook can't send you a web hook in local. So make sure you have an hosting ready before start you development., (*11)

Your bot is in sandobox by default. Only the people with the permission in your application can talk with it., (*12)

Facade

sendTextMessage

Officiale documention, (*13)

php Messenger::sendTextMessage($senderID, "Authentication successful");, (*14)

sendImageMessage

Officiale documention, (*15)

```php Messenger::sendImageMessage($senderID, env('APP_URL') . '/assets/images/logo.png');, (*16)

```, (*17)

getCurrentUserProfile

Officiale documention, (*18)

```php Messenger::getCurrentUserProfile($senderID);, (*19)

```, (*20)

sendCard

Officiale documention, (*21)

```php Messenger::sendCard($senderID, [ 'template_type' => 'generic', 'elements' => [ [ "title" => "Messenger Boilerplate", "image_url" => env('APP_URL') . '/assets/images/logo.png', "subtitle" => "example subtitle", 'buttons' => [ [ 'type' => "web_url", 'url' => "https://github.com/Distilleries/lumen-messenger-boilerplate", 'title' => "Come download it!" ] ] ], (*22)

          ]
      ]);

```, (*23)

Example

On this messenger class you can say hi and the bot give you an answer like this :, (*24)

Hi First name Last name, (*25)

Send a picture with a picto on the bottom right`, (*26)

I customize your profile picture. Do you like it?, (*27)

The Versions

24/01 2017

dev-master

9999999-dev

Component to allow the comunication between messenger bot and your application.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Quang Tam

24/01 2017

1.0.2

1.0.2.0

Component to allow the comunication between messenger bot and your application.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Quang Tam

24/01 2017

1.0.1

1.0.1.0

Component to allow the comunication between messenger bot and your application.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Quang Tam

24/01 2017

1.0

1.0.0.0

Component to allow the comunication between messenger bot and your application.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Quang Tam