Skip to main content

BLiP SDK Python

Project description

blip-sdk-python

Simple BLiP SDK for Python

This is a work in progress

wemake-python-styleguide Build PyPI Publish PyPI - Downloads PyPI PyPI - Python Version


Read more about BLiP here

Installing

You should install the blip-sdk package to access the BLiP server:

pip install blip-sdk
pip install lime-transport-websocket

Instantiate the BlipSdk Client

You will need an identifier and an access key to connect a chatbot to BLiP. To get them:

  • Go to Painel BLiP and login;
  • Click Create chatbot;
  • Choose the Create from scratch model option;
  • Go to Settings and click in Connection Information;
  • Get your bot's identifier and access key.

In order to instantiate the client use the ClientBuilder class informing the identifier and access key:

def main_async():
    # Create a client instance passing the identifier and access key of your chatbot
    client = ClientBuilder() \
        .with_identifier(identifier) \
        .with_access_key(access_key) \
        .with_transport_factory(lambda: WebSocketTransport()) \
        .build()

    # Connect with the server asynchronously
    # Connection will occurr via websocket on the 8081 port
    await client.connect_async()

Each client instance represents a server connection and can be reused. To close a connection:

client.close_async()

You can also connect synchronously with the server

def main():
    # Create a client instance passing the identifier and access key of your chatbot
    client = ClientBuilder() \
        .with_identifier(identifier) \
        .with_access_key(access_key) \
        .with_transport_factory(lambda: WebSocketTransport()) \
        .build()

    # Connect with the server asynchronously
    # Connection will occurr via websocket on the 8081 port
    client.connect()

Receiving

All messages sent to the chatbot are redirected to registered receivers of messages and notifications. You can define filters to specify which envelopes will be handled by each receiver. The following example shows how to add a simple message receiver:

client.add_message_receiver(Receiver(True, lambda e: e
))

The next sample shows how to add a notification receiver with a filter for the received event type:

client.add_notification_receiver(Receiver(True, lamda e: e
))

It's also possible to use a custom function as a filter:

Example of a message receiver filtering by the originator:

def filter_originator(message: Message):
    if message.from == '553199990000@0mn.io':
        return message
client.add_message_receiver(Receiver(True, filter_originator))

Each registration of a receiver returns a handler that can be used to cancel the registration:

remove_receiver = client.add_message_receiver(Receiver(True, lambda env: env))

remove_receiver()

Sending

It's possible to send notifications and messages only after the session has been stablished.

The following sample shows how to send a message after the connection has been stablished:

await client.connect_async()

# Once connected it's possible to send messages
message = client.send_message(Message('text/plain', 'message', to=user_id))

The following sample shows how to send a notification after the connection has been stablished:

client.connect_async()
notification = Notification(
        NotificationEvent.Received,
        Reason(ReasonCode.ApplicationError, 'failed'),
        to=user_Id
    )
client.send_notification(notiication)

Contributing

For information on how to contribute to this package, please refer to our Contribution guidelines.

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

blip_sdk-1.0.2.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

blip_sdk-1.0.2-py3-none-any.whl (34.5 kB view details)

Uploaded Python 3

File details

Details for the file blip_sdk-1.0.2.tar.gz.

File metadata

  • Download URL: blip_sdk-1.0.2.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for blip_sdk-1.0.2.tar.gz
Algorithm Hash digest
SHA256 5dd6d260968105d0420edfb097b37acf01881cf226630b28a132a0035167e7e9
MD5 57b1f2140b3fe80733dd296670ad6b18
BLAKE2b-256 d23ca1288ed6a53638e3b6d3a058ce2086ae02b26c6273fc2c477d876d82a63e

See more details on using hashes here.

File details

Details for the file blip_sdk-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: blip_sdk-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 34.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for blip_sdk-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 aef06f08f58dbad3bc50468ff76a76ee388b7d5cccb387b81b5c39739ed9e20c
MD5 58c7a75ebbde1a58b7ff0b3728554925
BLAKE2b-256 46f03251dbb5392a085e5a258ecf8c19c807b672f8a87ba7087e2ae03895d860

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