koslab / koslab.messengerbot

Python Facebook Messenger Bot API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Introduction

koslab.messengerbot makes writing Facebook Messenger Bot easier by providing a framework that handles and abstract the Bots API. It is originally developed using Morepath as the web request processor and the default hub implementation is on morepath, but this library should work with any Python web frameworks

Example: Writing An Echo Bot

Lets install koslab.messengerbot

Now lets write our EchoBot in echobot.py

And now lets write a hub config file, config.yml.

Start the bot

Finally proceed to follow the Messenger Platform Getting Started guide to get your bot configured and registered in Facebook.

Bot Configuration

POSTBACK_HANDLERS

Dictionary mapping of payload to name of object method that will handle the payload. Default value is:

Example:

GREETING_TEXT

Greeting text for new threads. Default value is:

STARTUP_MESSAGE

Message object to be sent when Get Started menu is clicked. Default value is:

PERSISTENT_MENU

Persistent menu call_for_action buttons configuration. Default value is:

Bot Hooks

Following are the list of hooks that can be implemented on the bot

message_hook

Handles Message Received and Message Echo event.

postback_hook

Handles Postback Received event. This hook have a default implementation which triggers methods based on payload value. To define the mapping, configure POSTBACK_HANDLERS class variable.

authentication_hook

Handles Authentication event.

account_linking_hook

Handles Account Linking event.

message_delivered_hook

Handles Message Delivered event.

message_read_hook

Handles Message Read event

Send API

BaseMessengerBot class provide a send method to send responses to Facebook Messenger Bot service. Parameters are:

recipient

Recipient object. Eg: { 'id': '12345678'}

message

Message object. Refer to Facebook Send API reference for supported messages

sender_action

Sender actions. Supported values: mark_seen, typing_on, typing_off

Note: If message is defined, sender_action value will be ignored.

A convenience method reply can also be used to send a response. Parameters are:

event

Event object

message

Accepts string, callable or message object. Strings are automatically converted into message object. Callable will be called with the event object as its parameter.

Postback Payload

Postback values may be a JSON object or a string. In the case of Postback in JSON object format, an event key is required for routing postbacks to the right handler by postback_hook. For string postback values, the whole string is treated as the event key.

Session

Session Management is provided through a thin wrapper around Beaker Cache. Current conversation session variable may be acquired through get_session method on BaseMessengerBot class. Session object is dict-like and may be treated as such.

Messenger Bot with AMQP

AMQP queuing is supported by the hub process. To use this, in config.yml simply set use_message_queue to true and configure the transport uri to the message queue on message_queue setting. The queue is implemented using Kombu, so you may also use other transports that are supported by Kombu

Conversation API

NOTE: This is a draft spec. Not yet implemented. Inputs are welcomed.

Spec

About

Python Facebook Messenger Bot API


Languages

Language:Python 100.0%