Code Monkey home page Code Monkey logo

lita-example's Introduction

lita-example

You will find a single handler plugin under the plugins directory but there are eight different hanlder classes implemented to explain various features. Before we jump on to the handlers, lets first go through the lita config.

Config

config.robot.name = "Lita"
config.robot.mention_name = "lita"
config.robot.alias = "/"

config.robot.log_level = :info

config.robot.adapter = :shell

config.redis.host = "127.0.0.1"
config.redis.port = 6379

config.http.host = "127.0.0.1"
config.http.port = 8080

Our bot name will be 'Lita' and mention name will be 'lita'. We will also keep an alias for easy communication and it will be "/".

Since this is just a demo, we will not be connecting to any chat service for now and will communicate in shell.

For our demo bot to work, we will need a redis server running on our dev machine on port 6379. If you already have redis installed, all you need to do is run redis-server in your terminal window and redis server will start.

Handlers

Routes

  • First one is the demo handler which is the default handler class generated by Lita during handler initialization. It has three different routes:

    • Chat: This is for chatting with Lita.

        You : lita ping
        Lita: PONG
        You : ping #no response for this one
        You : /ping
        Lita: PONG
      
    • Event: You will get a message as soon as lita connects to any chat service.

        Lita is available for chat.
      
    • HTTP: Lita has an in built http server

        REQUEST: curl "http://localhost:8080/hello/1" 
        RESPONSE: wishes for user with id: '1'         
      
  • Now we will see three more handlers where each can understand a specific route.

    • demo_chat: This handler can only understand chats and nothing else.

        You: hi
        Lita: Hello!
      
    • demo_http: This handler can understand only http routes.

        #REQUEST: curl "http://localhost:8080/hello/dummy,text/world"
        #RESPONSE: dummy,text
      
    • demo_event: Only events like connecting to chat service can be understood by this handler. If you want to do some action based on events, this is the right place. For example, as soon as Lita is connected to a chat service, it will send out a message Welcome User!

Templates

  • Lita also provides a way to maintain some predefined templates to be rendered during the chat flow as done in demo_template handler. For example, you will just say "wish" and bot will render a template containing "Hello" and user name.

      You: /wish
      Lita: Hello, Carl!
    
  • You can also pass some helper methods which will be executed during template rendering. In below example, on saying 'reverse', bot will reverse the first and last name of the user during template rendering:

      You: /reverse
      Lita: Pug, Carl
    

Timer

  • What if we need to perform some action after some time or at regular intervals? Lita has a way for this as well. If you look into demo_timer handler, there are two methods "after_timer" and "every_timer". After timer performs a task after a specified interval but once, whereas every timer is like a cron. Once triggered, it will perform the task at regular intervals and it will continue to do so until Lita is specificaly told to stop the timer.

      You: after_timer
      Lita: Hello, 1 seconds later!
    
      You: every_timer
      Lita: This is your 1 second reminder!
      Lita: This is your 1 second reminder!
      Lita: This is your 1 second reminder!
    

Redis

  • Lita uses redis as a persistent data store. It exposes the full power of Redis rather than using it to serialize JSON. One such use is described in demo_redis handler.

      You: /save yesuagg YesuAggarwal
      Lita: saved!
      
      You: /fetch yesuagg
      Lita: YesuAggarwal
    

Reading Config

  • Adapters and handlers share a common system for specifying configuration attributes. Configuration attributes are automatically namespaced with the plugin's type and "namespace" value. For example, configuration attributes defined by a handler named demo_config would be found at config.handlers.demo_config. In this project, we will hit github's user info endpoint to fetch the user info by username. The API route will be kept in config like this:

     config.handlers.demo_config.github_user_api = "https://api.github.com/users/"
    

    Now you can fetch the user info this way:

     You: gitinfo yesuagg
     Lita:	login: yesuagg
     		name: Yesu Aggarwal
     		...
     		...
    

License

MIT

lita-example's People

Contributors

yesuagg avatar dependabot[bot] avatar

Stargazers

Andrei avatar George Villasboas avatar

Watchers

 avatar

Forkers

dhananjayas

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.