Package: bothub
This package provide components to works withBotHub.Studio, which is a chatbot hosting service.Withbothub-cli, you can deploy a new chatbot with just four lines of commands.InstallationTo install bothub:$ pip install bothubThe bothub package works on python2 and 3 both.Getting StartedYou can build an echo chatbot simply by subclassingBaseBotclass and overridinghandle_messagemethod.# -*- coding: utf-8 -*-from__future__import(absolute_import,division,print_function,unicode_literals)frombothub_client.botimportBaseBotfrombothub_client.decoratorsimportchannelclassBot(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()defdefault_handler(self,event,context):When a bot receives a message from an user, it triggershandle_messagemethod witheventandcontextobject.Aneventis 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.MessagingTo send a message, use aself.send_messagemethod with a message you want to send.self.send_message('hello')In most cases, you may omituser_idandchannelarguments. 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’ usingMessageobject.frombothub_client.messagesimportMessagemessage=Message(event).add_quick_reply('Go ahead')\.add_quick_reply('Never mind')\.set_text('May I reserve the seat?')self.send_message(message)Messageclass 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)StorageTo store/retreive a property data, we provides following methods:Project levelself.set_project_data(data): set data to a projectself.get_project_data(key=None): get data from a projectUser levelself.set_user_data(data, user_id=None, channel=None): set user dataself.get_user_data(user_id=None, channel=None, key=None): get user datadatashould be a dict. An existing properties not included indatawill be ignored, not be deleted.Ifuser_idandchannelisNone, it regarded as a message sender.WhenkeyisNone, get whole dictionary will be returned. Otherwise, subtree of given key will be returned.NLU IntegerationYou can usenlumethod 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.defhandle_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.defhandle_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, uselangkeyword argument onask()function.askmethod returns aNluResponseobject which contains attributes like:raw_response: A raw response which NLU service returns.action: ANluActionclass object to identify intent and required parameters.next_message: Next message text to respond NLU service recommend.ANluActionobject contains attributes like:intent: Intent nameparameters: parameter dictcompleted: A boolean indicates whether action completedFor incompleted action, you need to reply to user withnext_messageattribute of a NluResponse instance to complete action.LicenseThis package is licensed under AGPLv3 for non-commercial personal use. If you want to use this package for commercial use, please contact tobothub@bothub.studio.