Code Monkey home page Code Monkey logo

wpfbbotkit's Introduction

WPFBBotKit

WPFBBotKit provides an easy way for developers to start creating bots with the Facebook Messenger Platform. WPFBBotKit is not a bot itself, but allows you to easily verify and receive webhook requests from the Messenger Platform and send responses from your bot via a simplified API.

Getting Started

  1. Download or clone this repo and install via the WordPress plugin installer.
  2. Access WPFBBotKit Settings via the "WPFBBotKit" menu option under "Settings" in the wp-admin.
  3. Follow the Messenger Platform setup guide to set up your bot using the Webhook URL and Verification String from the WPFBBotKit Settings page.

Usage

Receive messages via the wpfbbk_message_received hook which will provide an instance of WPFBBK_Messaging as its only argument.

add_action( 'wpfbbk_message_received', function( $M ) {
	# bot code here
}, 10, 1 );

When a message has been received, you'll have access to the message's text and/or postback content as well as a few helper functions to help you respond to the message and find out information about the sender.

add_action( 'wpfbbk_message_received', function( $M ) {

	// Retrieve user's info from Facebook API
	$info = $M->get_user_info();
	
	// set fallback since user info is not guaranteed
	$name = $info->first_name ? $info->first_name : 'Human';
	
	if ( ! $M->postback ){
	
		// if no postback  is set, respond to text message
		if ( 'hello' === $M->text || 'hi' === $M->text ) {
			$M->reply_with_text( "Hi $name!", true );
			$M->reply_with_image_url( 'https://media.giphy.com/media/13TXV4kfn7r2iA/giphy.gif' );
		} else {
		
			// reply_with_buttons is one of a few reply helpers provided by `WPFBBK_Messaging`
			$M->reply_with_buttons(
				"Sorry, $name. I don't understand what you said. Do you want me to send you a gif?",
				array(
					array(
						'type'  => 'postback',
						'title' => '๐Ÿ‘  Sure.',
						'payload' => 'REPLY_WITH_GIF',
					),
					array(
						'type'  => 'postback',
						'title' => '๐Ÿ‘Ž  No, go away.',
						'payload' => 'NO_REPLY',
					),
				)
			);
		}
	} else {
	
		// postback has been received
		if( 'REPLY_WITH_GIF' === $M->postback ) {
			$M->reply_with_image_url( 'https://media.giphy.com/media/13TXV4kfn7r2iA/giphy.gif' );
		}
	}
}, 10, 1 );

Additionally, a wpfbbk_request_received action is fired whenever the webhook url is hit and provides an instance of the WP_REST_Request object for the request.

// debug requests sent to webhook
add_action( 'wpfbbk_request_received', function( $req ) {
   error_log( print_r( $req->get_raw_data(), 1 ) );
}, 10, 1 );

Finally, a wpfbbk_before_send_request filter is applied before sending any request via WPFBBK::api_send() which provides an opportunity to bypass the built-in sending functionality and offload sending API requests to a queue. Documentation for this hook can be found inline in WPFBBK::api_send().

// send requests to queue instead of using WPFBBK::api_send()
apply_filters( 'wpfbbk_before_send_request', function( $request_handled, $method, $url, $data ) {
   if ( ! $request_handled ) {
       $request_handled = SomeOtherClass::queueRequest( $method, $url, $data );
   }
   return $request_handled;
}, 10, 4 );

Requirements

Requires WordPress 4.7 or better since WPFBBotKit utilizes the WordPress Rest API.

License

This project is licensed under the GPL V2 License - see the LICENSE file for details

Credits

WPFBBotKit was created by Jeff Gould from Delicious Brains

wpfbbotkit's People

Contributors

jrgould avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.