Package: blip-sdk
blip-sdk-pythonSimple BLiP SDK for PythonThis is a work in progressRead more about BLiPhereInstallingYou should install theblip-sdkpackage to access the BLiP server:pip install blip-sdk
pip install lime-transport-websocketInstantiate the BlipSdk ClientYou will need anidentifierand anaccess keyto connect a chatbot toBLiP. To get them:Go toPainel BLiPand login;ClickCreate chatbot;Choose theCreate from scratchmodel option;Go toSettingsand click inConnection Information;Get your bot'sidentifierandaccess key.In order to instantiate the client use theClientBuilderclass informing theidentifierandaccess keyYou can start the client asynchronously or synchronouslyAsynchronously (the recommended way)importasynciofromlime_transport_websocketimportWebSocketTransportfromblip_sdkimportClientBuilderasyncdefmain_async()->None:# Create a client instance passing the identifier and access key of your chatbotclient=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 portawaitclient.connect_async()print('Application started. Press Ctrl + c to stop.')loop=asyncio.get_event_loop()loop.run_until_complete(main_async())loop.run_forever()Or the sync version (we only recommend for scripts)fromtimeimportsleepfromlime_transport_websocketimportWebSocketTransportfromblip_sdkimportClientBuilderdefmain()->None:# Create a client instance passing the identifier and access key of your chatbotclient=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 portclient.connect()print('Application started. Press Ctrl + c to stop.')main()whileTrue:sleep(1)Eachclientinstance represents a server connection and can be reused. To close a connection:awaitclient.close_async()# or syncclient.close()ReceivingAll messages sent to the chatbot are redirected to registeredreceiversof 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,lambdam:print(m)))The next sample shows how to add a notification receiver with a filter for thereceivedevent type:client.add_notification_receiver(Receiver(lambdan:n.event==NotificationEvent.RECEIVED,lambdan:print(n)))It's also possible to use a custom function as a filter:Example of a message receiver filtering by the originator:deffilter_originator(message:Message)->bool:returnmessage.from_n=='553199990000@0mn.io'client.add_message_receiver(Receiver(filter_originator,lambdam:print(m)))Each registration of a receiver returns ahandlerthat can be used to cancel the registration:remove_receiver=client.add_message_receiver(Receiver(True,lambdam:print(m)))remove_receiver()SendingIt'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:awaitclient.connect_async()# Once connected it's possible to send messagesmessage=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)ContributingFor information on how to contribute to this package, please refer to ourContribution guidelines.