Package: aiobook
AioBookAioBook it is async framework for build messenger application in facebookInstallationUse the package managerpipto install aiobook.pipinstallaiobookUsageFacebook HandlerFacebook Handler can handle allfacebook webhook eventsYou can use decorators:fromaiobookimportFacebookHandlerhandler=FacebookHandler("<page_access_token","<verification_token>",skip_confirm_execution=True)@handler.handle_messageasyncdefhandle_message(event):print("receive your message{}".format(event.text))@handler.handle_postbackasyncdefhandle_postback(event):print("receive your postback with payload{}".format(event.postback))Or directly register handlers:asyncdefhandle_message(event):print("receive your message{}".format(event.text))asyncdefhandle_postback(event):print("receive your postback with payload{}".format(event.postback))handler.set_webhook_handler("message",handle_message)handler.set_webhook_handler("postback",handle_postback)To get list allowed and defined handlers:handler.get_allowed_handlers()handler.get_defined_handlers()Also you can set handler before_handle, and after_handle. It will be called before(or after) handle_event:@handler.before_handleasyncdeflog_message(event):logging.info("{}handled.".format(event.name))@handler.after_handleasyncdeflog_message(event):logging.info("{}handled.".format(event.name))To receive message you need register handler in HTTP Server:fromaiohttpimportwebfromaiobookimportFacebookHandlerhandler=FacebookHandler("<page_access_token","<verification_token>",skip_confirm_execution=True)app=web.Application()app.add_routes([web.get("<url_pattern>",handler.handle_get)])app.add_routes([web.post("<url_pattern>",handler.handle_post)])MessengerMessenger supportsSend API method.
Facebook Handler included in Messenger.fromaiobookimportMessengermessenger=Messenger("<page_access_token","<verification_token>",skip_confirm_execution=True)@messenger.handler.handle_messageasyncdefhandle_message(event):awaitmessenger.send(event.sender_id,"Your message:{}".format(event.text))@messenger.handler.handle_postbackasyncdefhandle_postback(event):awaitmessenger.send(event.sender_id,"Your press button:{}".format(event.postback))messenger.sendAllow to send text or templates:awaitmessenger.send(event.sender_id,message,quick_replies=None,messaging_type=None,metadata=None,notification_type=None,tag=None)Allowed types for messenger.sendString message and templatesSupported TemplatesButtonTemplate, GenericTemplate, ListTemplate, OpenGraphTemplate, MediaTemplate.Supported ButtonsCallButton, GamePlayButton, LogInButton, LogOutButton, PostbackButton, UrlButtonOtherQuickReply, Element, MediaElement, OpenGraphElementfromaiobook.core.facebookimportQuickReplyfromaiobook.core.facebookimportElement,MediaElement,OpenGraphElementfromaiobook.core.facebookimportButtonTemplate,GenericTemplate,ListTemplate,OpenGraphTemplate,MediaTemplatefromaiobook.core.facebookimportCallButton,GamePlayButton,LogInButton,LogOutButton,PostbackButton,UrlButtonawaitmessenger.send(event.sender_id,ButtonTemplate('Hi, press buttons',buttons=[PostbackButton('test','test_payload'),UrlButton(title='test_rl',url='https://www.messenger.com')]))awaitmessenger.send(event.sender_id,GenericTemplate([Element('test',buttons=[PostbackButton('test','test_payload'),UrlButton(title='test_rl',url='https://www.messenger.com')]),Element('test2',image_url,'test2',buttons=[PostbackButton('test','test_payload'),UrlButton(title='test_rl',url='https://www.messenger.com')])]))messenger.get_user_profileAllows you to use a sender_id to retrieve user profile information:response=awaitmessenger.get_user_profile(event.sender_id,fields=("first_name","last_name"))Next fields aresupported.messenger.get_page_infoAllows you to retrieve your page information:response=awaitmessenger.get_page_info()messenger.imitate_typingDecorate func to imitate typing with defined timeout before answer. Included mark_seen, typing_on
and typing_off sender_actions.@messenger.handler.handle_postback@messenger.imitate_typing(1)asyncdefhandle_postback(event):awaitmessenger.send(event.sender_id,"Your press button:{}".format(event.postback))Or you can use sender actions independently:messenger.mark_seenSender action to mark last message as readawaitmessenger.mark_seen(event.sender_id)messenger.typing_onSender action to turn typing indicators onawaitmessenger.typing_on(event.sender_id)messenger.typing_offSender action to turn typing indicators offawaitmessenger.typing_off(event.sender_id)AioBook AppAioBook it is small aiohttp wrapper that helps manage and deploy your messenger appfromaiobookimportAioBookAppfromaiobookimportMessengerapp=AioBookApp(port=3000)messenger=Messenger("<page_access_token","<verification_token>",skip_confirm_execution=True)app.register_messenger(messenger)app.start_bot()LicenseMIT