owen1317 / Messenger-PHP

This is a set of PHP classes to help build Facebook Messenger bots in PHP.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Messenger-PHP

This is a set of classes to facilitate the creation of Facebook Messenger bots in PHP.

The main BotApp class implements:

  • Webhook validation routine.
  • Debugging info saved to a log file (configurable, uses bot.log as default).
  • JSON message parsing and dispatching to methods implemented in child class.
  • Session handling via custom session ids. This uses the standard PHP session functions.
  • Methods for sending text, quick reply, template and attachments.
  • Convenience methods for sending image, video, audio, file (calls attachment method internally).

There are also some PHP classes for building the messages to be sent. Instead of crafting JSON strings, you can just create the objects.

This is still a work in progress, but is already functional.

Getting started

To get started, you need to create a Page in Facebook, as well as an App. Follow these steps:

  1. Create a new Page in Facebook (or you can use one you already have)
  2. Add a new App in https://developers.facebook.com/
  3. In "Products", you need to add "Messenger"
  4. In Messenger, you will generate a Page Access Token. Select the page, and copy the token that was created.
  5. Setup webhooks: you need to enter the callback URL (it's the full URL to your PHP script) as well as a verify token (this is a random string). From the checkboxes below, make sure at least messages and messages_postbacks are checked.
  6. In your PHP script, put both Page Access and Verify tokens in the $config array used when initializing the class.
  7. Verify and Save, and if all is well, you should get a green tick. If not, check your web server log, or the generated debug log.
  8. Subscribe your webhook to your Page events by selecting it below and clicking Subscribe

Session handling

Session handling is important when you need to take different actions depending on your bot state. The standard PHP session functions are used internally. However, as Facebook doesn't send or handles cookies, we need a different approach. Before calling session_start(), a custom session_id is crafted, based on the user's Facebook ID, to make sure it's unique. The class implements 2 methods for setting and getting a value. Use $this->setSession($key, $val) to set a session variable, and $this->getSession($key) to get the value associated with a key.

To-Do list

Some much needed stuff that needs to be done:

  • Better class documentation, with examples.
  • Better validation and error handling.
  • Airline and receipt templates are not implemented yet.

About

This is a set of PHP classes to help build Facebook Messenger bots in PHP.


Languages

Language:PHP 100.0%