Build serverless chatbot on BotHub.Studio
Project description
This package provide components to works with BotHub.Studio, which is a chatbot hosting service.
With bothub-cli, you can deploy a new chatbot with just four lines of commands.
Installation
To install bothub:
$ pip install bothub
The bothub package works on python2 and 3 both.
Getting Started
You can build a echo chatbot simply by subclassing BaseBot class and overriding handle_message method.
# -*- coding: utf-8 -*-
from bothub_client.bot import BaseBot
class Bot(BaseBot):
"""Represent a Bot logic which interacts with a user.
BaseBot superclass have methods belows:
* Send message
* self.send_message(message, user_id=None, channel=None, extra=None)
* Data Storage
* self.set_project_data(data)
* self.get_project_data()
* self.set_user_data(data, user_id=None, channel=None)
* self.get_user_data(user_id=None, channel=None)
* self.nlu(vendor) -> NluClient
When you omit user_id and channel argument, it regarded as a user
who triggered a bot.
"""
def handle_message(self, event, context):
self.send_message(event['content'])
When a bot receives a message from an user, it triggers handle_message method with event and context object.
An event is a dict which contains following items:
content: A message text received.
channel: Which channel (messenger platform) sent a message.
sender: Who sent a message. {"id": <user-id>, "name": "<username>}
raw_data: A raw data itself messenger platforms offers.
You can respond to this message with various tools we provides.
Messaging
To send a message, use a self.send_message method with a message you want to send.
self.send_message('hello')
Most of case you can omit user_id and channel arguments or put values to those arguments to send a message to specific user rathan than whom sent a message to your bot.
You also send a message with rich controls like quick replies or buttons using Message object.
from bothub_client.messages import Message
message = Message(event).add_quick_reply('Go ahead')\
.add_quick_reply('Never mind')\
.set_text('May I reserve the seat?')
self.send_message(message)
Message class provides these methods:
set_text(text)
add_url_button(text, url):
add_postback_button(text, payload)
add_quick_reply(text, image_url=None)
add_location_request(text)
add_keyboard_button(text)
Storage
To store/retreive some data, we provides following methods:
Project level
self.set_project_data(data): set data to a project
self.get_project_data(): get data from a project
User level
self.set_user_data(data, user_id=None, channel=None): set user data
self.get_user_data(user_id=None, channel=None): get user data
data should be a dict. A keys which server has but not included in data key will be ignored not be deleted.
NLU Integeration
If you registered NLU integration at BotHub.Studio, you can use nlu method to make a request.
There are two styles to request to NLU service. (eg. to use API.ai)
First, use event object to construct message and session_id.
def handle_message(self, event, context):
response = self.nlu('apiai').ask(event=event)
self.send_message(response.next_message)
Or, put explicit message and session_id by yourself.
def handle_message(self, event, context):
response = self.nlu('apiai').ask(message='hello', session_id='customer1')
self.send_message(response.next_message)
ask method returns a NluResponse class which contains attributes like:
- raw_response
A raw response which NLU service returns.
- action
A NluAction class object to identify intent and required parameters.
- next_message
Next message text to respond NLU service recommend.
License
This package is licensed under AGPLv3 for non-commercial personal use. If you want to use this package for commercial use, please contact to bothub@bothub.studio.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file bothub-0.1.12.tar.gz
.
File metadata
- Download URL: bothub-0.1.12.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f66393b577c4c756ae6e16246ff90afa5b35a85f40085c35131ca8499733bd00 |
|
MD5 | c428de6ff715fc0626821cb2cdca4f6c |
|
BLAKE2b-256 | d3aecf124b63124ff832402c8d56152f99c8ca479e3200404c8e3159e377b6c1 |
File details
Details for the file bothub-0.1.12-py2.py3-none-any.whl
.
File metadata
- Download URL: bothub-0.1.12-py2.py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ba6f34f260759f7162fe1a03d9c9bd3a6550d7b518bd9d6a87e1650cdc8c761 |
|
MD5 | 6eb41f815d06ee69586fef752d37045a |
|
BLAKE2b-256 | 964f8ff1d64d8781d138a48af434b18f9b8edc4a2027245da90e600e0f9f5214 |