Skip to main content

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 an echo chatbot simply by subclassing BaseBot class and overriding handle_message method.

 # -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function, unicode_literals)

from bothub_client.bot import BaseBot
from bothub_client.decorators import channel

class Bot(BaseBot):
    """Represent a Bot logic which interacts with a user.

    BaseBot superclass have methods belows:

    * Send message
      * self.send_message(message, chat_id=None, channel=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)
    * Channel Handler
      from bothub_client.decorators import channel
      @channel('<channel_name>')
      def channel_handler(self, event, context):
        # Handle a specific channel message
    * Command Handler
      from bothub_client.decorators import command
      @command('<command_name>')
      def command_handler(self, event, context, args):
          # Handle a command('/<command_name>')
    * Intent Handler
      from bothub_client.decorators import intent
      @intent('<intent_id>')
      def intent_result_handler(self, event, context, answers):
          # Handle a intent result
          # answers is a dict and contains intent's input data
            {
              "<intent slot id>" : <entered slot value>
              ...
            }
    """
    @channel()
    def default_handler(self, event, context):

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>}

  • chat_id: Chatroom ID where message is sent. It can be a 1:1 chatroom or group chatroom.

  • location: Location information if possible {"longitude": <float>, "latitude": <float>}

  • postback: A postback data.

  • new_joined: A boolean which indicates this bot was invited to some chatroom or not.

  • raw_data: A raw data itself messenger platforms sent.

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')

In most cases, you may omit user_id and channel arguments. Then it replies to whom sent a message to your bot. Put values to those arguments when you want to specify a receiver.

You can 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, payload=None, image_url=None)

  • add_location_request(text)

  • add_keyboard_button(text)

Storage

To store/retreive a property data, we provides following methods:

  • Project level

    • self.set_project_data(data): set data to a project

    • self.get_project_data(key=None): 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, key=None): get user data

data should be a dict. An existing properties not included in data will be ignored, not be deleted.

  • If user_id and channel is None, it regarded as a message sender.

  • When key is None, get whole dictionary will be returned. Otherwise, subtree of given key will be returned.

NLU Integeration

You can use nlu method to perform NLU after setup NLU integration at BotHub.Studio.

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)

If you want to use a language other than english, use lang keyword argument on ask() function.

ask method returns a NluResponse object 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.

A NluAction object contains attributes like:

  • intent: Intent name

  • parameters: parameter dict

  • completed: A boolean indicates whether action completed

For incompleted action, you need to reply to user with next_message attribute of a NluResponse instance to complete action.

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.

Metadata

Release files for bothub 0.1.30

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bothub 0.1.30
File Size Uploaded Provenance Actions
bothub-0.1.30.tar.gz 17.3 kB Without provenance Details for bothub-0.1.30.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bothub 0.1.30
File Interpreter ABI Platform Actions
bothub-0.1.30-py2.py3-none-any.whl Python 2, Python 3 none any Details for bothub-0.1.30-py2.py3-none-any.whl

Total release size: 40.2 kB

Release files / bothub-0.1.30.tar.gz

Download URL bothub-0.1.30.tar.gz
Size 17.3 kB
Tags Source
SHA-256 checksum
How to use checksums
583421b74fbb8b7332faa8ca6923574b824c8f60122ad98ee052ac370cab069a
BLAKE2b-256 checksum
How to use checksums
2cfea933ea17b12f5e7b8d073b624e5c64289df278b760aa7240a1af22e9b486
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

Release files / bothub-0.1.30-py2.py3-none-any.whl

Download URL bothub-0.1.30-py2.py3-none-any.whl
Size 22.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
554d66d7f28f80f5ad6a7208709bb900568109b609f8764e3f9d74fa22676517
BLAKE2b-256 checksum
How to use checksums
bdd70b6fe23f443e93b6c092a552ec08da0b10017d6160b09fec8c905d2e2b83
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

Security for bothub 0.1.30

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.30 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page