Creating a new input adapter

You can write your own input adapters by creating a new class that inherits from InputAdapter and overrides the necessary methods established in the base InputAdapter class.

chatterbot.input.InputAdapter(**kwargs)[source]

This is an abstract class that represents the interface that all input adapters should implement.

To create your own input adapter you must override the process_input method to return a Statement object.

Note that you may need to extend the __init__ method of your custom input adapter if you intend to save a kwarg parameter that was passed into the chat bot’s constructor. (An API key might be an example of a parameter you would want to access here.)

from __future__ import unicode_literals
from chatterbot.adapters import Adapter


class InputAdapter(Adapter):
    """
    This is an abstract class that represents the
    interface that all input adapters should implement.
    """

    def process_input(self, *args, **kwargs):
        """
        Returns a statement object based on the input source.
        """
        raise self.AdapterMethodNotImplementedError()

    def process_input_statement(self, *args, **kwargs):
        """
        Return an existing statement object (if one exists).
        """
        input_statement = self.process_input(*args, **kwargs)
        self.logger.info('Recieved input statement: {}'.format(input_statement.text))

        existing_statement = self.chatbot.storage.find(input_statement.text)

        if existing_statement:
            self.logger.info('"{}" is a known statement'.format(input_statement.text))
            input_statement = existing_statement
        else:
            self.logger.info('"{}" is not a known statement'.format(input_statement.text))

        return input_statement