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.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.
-
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!
-
-
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
-
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!
-
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
-
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 ... ...