2017 © Pedro Peláez
 

library messenger

Facebook Messenger PHP interface

image

mangoweb/messenger

Facebook Messenger PHP interface

  • Friday, November 11, 2016
  • by viliamkopecky
  • Repository
  • 15 Watchers
  • 9 Stars
  • 296 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 4 Versions
  • 2 % Grown

The README.md

Messenger PHP

Installation

Use composer:, (*1)

$ composer require mangoweb/messenger

Usage

Example screencast, (*2)

The PAGE_ACCESS_TOKEN must be Facebook Page access_token with pages_messaging permission. RECIPIENT_ID is different for each Facebook App, Page and User., (*3)

Sending messages

use Mangoweb\Messenger\Page;
use Mangoweb\Messenger\PageSender;
use Mangoweb\Messenger\Message;
use Mangoweb\Messenger\CardElement;
use Mangoweb\Messenger\Button;
use Mangoweb\Messenger\QuickReply;

$page = new Page(PAGE_ACCESS_TOKEN);
$sender = new PageSender($page);

// Sending simple text message
$textMessage = Message::text('Hello world!');
$sender->send(RECIPIENT_ID, $textMessage);

// Sending an image attachment
$imageMessage = Message::image('https://unsplash.it/400/300');
$sender->send(RECIPIENT_ID, $imageMessage);

// Sending complex message with two cards and some button
$cardButtons = [
    Button::url('Github', 'https://github.com/manGoweb/Messenger')
];

$cards = [
    CardElement::create('Lorem', 'ipsum dolor', 'https://github.com/manGoweb/Messenger', 'https://unsplash.it/400/300?random', $cardButtons),
    CardElement::create('Foo', NULL, NULL, 'https://unsplash.it/400/300?random')
];

$messageWithCards = Message::generic($cards);
$sender->send(RECIPIENT_ID, $messageWithCards);

Thread management

use Mangoweb\Messenger\Page;
use Mangoweb\Messenger\Greeting;
use Mangoweb\Messenger\GetStartedButton;
use Mangoweb\Messenger\PersistentMenu;
use Mangoweb\Messenger\Button;

$page = new Page(PAGE_ACCESS_TOKEN);

// Detect if page is already hooked to your Facebook App
echo ($page->isHookedToApp(APP_ID) ? 'already hooked' : 'not yet hooked') . "\n";

// Load basic info about a Page
$info = $page->loadInfo();
echo $info['name'] . "\n";

// Load info about user as the Page can see them
$profile = $page->loadProfile(RECIPIENT_ID);
echo $profile['first_name'] . ' ' . $profile['last_name'] . "\n";

// Set greeting text
$page->setGreeting(Greeting::text('Hello world'));

// Set "get started" button payload
$page->setGetStartedButton(GetStartedButton::payload('1'));

// Set persistent menu buttons
$page->setPersistentMenu(PersistentMenu::buttons([
    Button::postback('Help', 'help'),
    Button::postback('Sign in', 'sign-in'),
]));

Receiving messages

use Mangoweb\Messenger\Receiver;

$receiver = new Receiver();

// Define event hooks
$receiver->onMessage[] = function($message) {
    echo "Someone sent us a message" . "\n";
    var_dump($message);
};

$receiver->onPostback[]  = function($postback) {
    echo "Someone clicked on a postback button" . "\n";
    var_dump($postback);
};

// Load JSON payload from Messenger request
$httpBody = file_get_contents('php://input');
/*
$httpBody = '{
    "object":"page",
    "entry":[
        {
            "id":"PAGE_ID",
            "time":1458692752478,
            "messaging":[
                {
                    "sender":{
                        "id":"USER_ID"
                    },
                    "recipient":{
                        "id":"PAGE_ID"
                    },
                    "timestamp":1458692752478,
                    "message":{
                        "mid":"mid.1458696618141:b4ef9d19ec21086067",
                        "seq":51,
                        "attachments":[
                            {
                                "type":"image",
                                "payload":{
                                    "url":"IMAGE_URL"
                                }
                            }
                        ]
                    }
                }
            ]
        }
    ]
}';
*/
$payload = json_decode($httpBody, TRUE);

// Process the payload
$receiver->processBody($payload);

Tests

Add tests/php.ini (you may use the templates php-unix.ini or php-win.ini)., (*4)

In tests/local.php you can define constants for testing real API calls (you may use the template local.sample.php), (*5)

$ ./vendor/bin/tester -c tests/php.ini tests

License

MIT. See full license., (*6)

The Versions

11/11 2016

dev-master

9999999-dev https://github.com/mangoweb/messenger

Facebook Messenger PHP interface

  Sources   Download

MIT

The Requires

 

The Development Requires

facebook messenger

11/11 2016

v1.2.0

1.2.0.0 https://github.com/mangoweb/messenger

Facebook Messenger PHP interface

  Sources   Download

MIT

The Requires

 

The Development Requires

facebook messenger

11/10 2016

v1.1.0

1.1.0.0 https://github.com/mangoweb/messenger

Facebook Messenger PHP interface

  Sources   Download

MIT

The Requires

 

The Development Requires

facebook messenger

06/10 2016

v1.0.0

1.0.0.0 https://github.com/mangoweb/messenger

Facebook Messenger PHP interface

  Sources   Download

MIT

The Requires

 

The Development Requires

facebook messenger