Skip to main content

Async framework for build messenger application in facebook

Project description

AioBook

AioBook it is async framework for build messenger application in facebook

Installation

Use the package manager pip to install aiobook.

pip install aiobook

Usage

Facebook Handler

Facebook Handler can handle all facebook webhook events

You can use decorators:

from aiobook import FacebookHandler
handler = FacebookHandler("<page_access_token", "<verification_token>", skip_confirm_execution=True)

@handler.handle_message
async def handle_message(event):
    print("receive your message {}".format(event.text))

@handler.handle_postback
async def handle_postback(event):
    print("receive your postback with payload {}".format(event.postback))

Or directly register handlers:

async def handle_message(event):
    print("receive your message {}".format(event.text))

async def handle_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_handle
async def log_message(event):
    logging.info("{} handled.".format(event.name))

@handler.after_handle
async def log_message(event):
    logging.info("{} handled.".format(event.name))

To receive message you need register handler in HTTP Server:

from aiohttp import web
from aiobook import FacebookHandler
handler = 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)])

Messenger

Messenger supports Send API method. Facebook Handler included in Messenger.

from aiobook import Messenger
messenger = Messenger("<page_access_token", "<verification_token>", skip_confirm_execution=True)

@messenger.handler.handle_message
async def handle_message(event):
    await messenger.send(event.sender_id, "Your message: {}".format(event.text))

@messenger.handler.handle_postback
async def handle_postback(event):
    await messenger.send(event.sender_id, "Your press button: {}".format(event.postback))

messenger.send

Allow to send text or templates:

await messenger.send(event.sender_id, message, quick_replies=None,
                   messaging_type=None, metadata=None, notification_type=None,
                   tag=None)

Allowed types for messenger.send

String message and templates

Supported Templates

ButtonTemplate, GenericTemplate, ListTemplate, OpenGraphTemplate, MediaTemplate.

Supported Buttons

CallButton, GamePlayButton, LogInButton, LogOutButton, PostbackButton, UrlButton

Other

QuickReply, Element, MediaElement, OpenGraphElement

from aiobook.core.facebook import QuickReply
from aiobook.core.facebook import Element, MediaElement, OpenGraphElement
from aiobook.core.facebook import ButtonTemplate, GenericTemplate, ListTemplate, OpenGraphTemplate, MediaTemplate
from aiobook.core.facebook import CallButton, GamePlayButton, LogInButton, LogOutButton, PostbackButton, UrlButton

await messenger.send(event.sender_id,
 ButtonTemplate('Hi, press buttons',
  buttons=[PostbackButton('test', 'test_payload'),
           UrlButton(title='test_rl', url='https://www.messenger.com')]))

await messenger.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_profile

Allows you to use a sender_id to retrieve user profile information:

response = await messenger.get_user_profile(event.sender_id, fields=("first_name", "last_name"))

Next fields are supported.

messenger.get_page_info

Allows you to retrieve your page information:

response = await messenger.get_page_info()

messenger.imitate_typing

Decorate 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)  
async def handle_postback(event):
    await messenger.send(event.sender_id, "Your press button: {}".format(event.postback))

Or you can use sender actions independently:

messenger.mark_seen

Sender action to mark last message as read

await messenger.mark_seen(event.sender_id)

messenger.typing_on

Sender action to turn typing indicators on

await messenger.typing_on(event.sender_id)

messenger.typing_off

Sender action to turn typing indicators off

await messenger.typing_off(event.sender_id)

AioBook App

AioBook it is small aiohttp wrapper that helps manage and deploy your messenger app

from aiobook import AioBookApp
from aiobook import Messenger
app = AioBookApp(port=3000)
messenger = Messenger("<page_access_token", "<verification_token>", skip_confirm_execution=True)
app.register_messenger(messenger)
app.start_bot()

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiobook-0.1.2.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

aiobook-0.1.2-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file aiobook-0.1.2.tar.gz.

File metadata

  • Download URL: aiobook-0.1.2.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.2

File hashes

Hashes for aiobook-0.1.2.tar.gz
Algorithm Hash digest
SHA256 90bf2e966debe10221df659796bc5a2dfbb987376ee630fbec65b69f092e2ba2
MD5 761666ded31413bf93ddac4b5f627c03
BLAKE2b-256 728821a74fe5914098deb41a23fd0552d2167a0cea2c0d35fe73ec6c5a336dc8

See more details on using hashes here.

File details

Details for the file aiobook-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: aiobook-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.2

File hashes

Hashes for aiobook-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 916303fb529b10ff97985323f97bfc25f2c36c10e10dc88d5e7a90f1bce32d12
MD5 e1c3366b52ee87d854dbdaa73db1c7e3
BLAKE2b-256 2e8960a3655bbb838b545e1d6a52a77f44523f08f93563f0e1c0d4a3e3c48c43

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page