Package: koslab.messengerbot
ContentsIntroductionExample: Writing An Echo BotBot ConfigurationBot HooksSend APIPostback PayloadSessionMessenger Bot with AMQPConversation APIContributorsChangelog1.0b5 (2016-08-09)1.0b4 (2016-08-08)1.0b3 (2016-08-08)1.0b2 (2016-07-13)1.0b1 (2016-07-13)Introductionkoslab.messengerbotmakes writingFacebook Messenger Boteasier by providing a framework that handles and abstract
the Bots API. It is originally developed usingMorepathas the web request processor and the default hub implementation is on morepath, but this library should
work with any Python web frameworksExample: Writing An Echo BotLets installkoslab.messengerbotpipinstallkoslab.messengerbotNow lets write our EchoBot inechobot.pyfromkoslab.messengerbot.botimportBaseMessengerBot# bot implementationclassEchoBot(BaseMessengerBot):GREETING_TEXT='Hello!. EchoBot, at your service!'STARTUP_MESSAGE={'text':'Hi!, lets get started!'}defmessage_hook(self,event):text=event['message'].get('text','')self.send(recipient=event['sender'],message={'text':text})And now lets write a hub config file,config.yml.webhook:webhookuse_message_queue:falsemessage_queue:amqp://guest:guest@localhost:5672//hub_verify_token:<MY-VERIFY-TOKEN>bots:-page_id:<PAGE-ID>title:EchoBotclass:echobot:EchoBotaccess_token:<PAGE-ACCESS-TOKEN>Start the botmessengerbot_hubconfig.ymlFinally proceed to follow theMessenger Platform Getting Startedguide to get your bot configured and registered in Facebook.Bot ConfigurationPOSTBACK_HANDLERSDictionary mapping of payload to name of object method that will handle the
payload. Default value is:POSTBACK_HANDLERS={}Example:POSTBACK_HANDLERS={'mypostback':'mypostback_hook'}defmypostback_hook(self,event):...GREETING_TEXTGreeting textfor new threads. Default value is:GREETING_TEXT='Hello World!'STARTUP_MESSAGEMessage object to be sent whenGet Startedmenu is clicked. Default value is:STARTUP_MESSAGE={'text':'Hello World!'}PERSISTENT_MENUPersistent menucall_for_actionbuttons configuration. Default value is:PERSISTENT_MENU=[{'type':'postback','title':'Get Started','payload':'messengerbot.get_started'}]Bot HooksFollowing are the list of hooks that can be implemented on the botmessage_hookHandlesMessage ReceivedandMessage Echoevent.postback_hookHandlesPostback Receivedevent. This hook have a default implementation which triggers methods based
on payload value. To define the mapping, configurePOSTBACK_HANDLERSclass variable.authentication_hookHandlesAuthenticationevent.account_linking_hookHandlesAccount Linkingevent.message_delivered_hookHandlesMessage Deliveredevent.message_read_hookHandlesMessage ReadeventSend APIBaseMessengerBotclass provide asendmethod to send responses to
Facebook Messenger Bot service. Parameters are:recipientRecipient object. Eg:{ 'id': '12345678'}messageMessage object. Refer toFacebook Send API referencefor supported messagessender_actionSender actions. Supported values:mark_seen,typing_on,typing_offNote:Ifmessageis defined,sender_actionvalue will be ignored.A convenience methodreplycan also be used to send a response. Parameters
are:eventEvent objectmessageAccepts string, callable or message object. Strings are automatically
converted into message object. Callable will be called with the event
object as its parameter.Postback PayloadPostback values may be a JSON object or a string. In the case of
Postback in JSON object format, aneventkey is required for routing postbacks
to the right handler bypostback_hook. For string postback values, the
whole string is treated as the event key.SessionSession Management is provided through a thin wrapper aroundBeaker Cache. Current conversation
session variable may be acquired throughget_sessionmethod onBaseMessengerBotclass. Session object isdict-like and may be treated
as such.defmessage_hook(self,event):session=self.get_session(event)Messenger Bot with AMQPAMQP queuing is supported by the hub process. To use this, inconfig.ymlsimply setuse_message_queuetotrueand configure the transport uri
to the message queue onmessage_queuesetting. The queue is implemented usingKombu, so you may also useother transportsthat are supported by Kombuuse_message_queue:truemessage_queue:amqp://guest:guest@localhost:5672//Conversation APINOTE:This is a draft spec. Not yet implemented. Inputs are welcomed.Specconversation:myconversationsteps:-message:What is your name?type:textstore:name-message:Please share your phototype:image-attachmentstore:photo-message:Please share your locationtype:location-attachmentstore:location-message:-type:generic-templateelements:-title:Summarysubtitle:Summaryimage_url:${data['photo']['url']}buttons:-type:postbacktitle:Savepayload:myconversation.saveContributorsNote:  place names and roles of the people who contribute to this packagein this file, one to a line, like so:Mohd Izhar Firdaus Bin Ismail, Original AuthorChangelog1.0b5 (2016-08-09)Rearrange priority of event handlers so that quick reply postbacks are
caught [Izhar Firdaus]1.0b4 (2016-08-08)Ignore CLI when starting up hub
[Izhar Firdaus]1.0b3 (2016-08-08)Ensure that all child processes are killed when parent is terminated.
[Izhar Firdaus]1.0b2 (2016-07-13)Bug with page_id mapping. Ensure it is read as string now instead of integer
[Izhar Firdaus]1.0b1 (2016-07-13)Initial fully functional bot framework with hub implementation
[Izhar Firdaus]