/chatopia

Primary LanguageJavaMIT LicenseMIT

Download Codacy Badge

Chatopia

A framework to build chatbot compatible with different platforms! The idea is simple, develop chatbots in the same way do implement web applications via Spring Web.

A simple Example:

How to implement a chatbot able to echo the messages

public class MessageEchoExample {

    @OnCommand(value = "echo", help = "This command echo")
    public Response onEchoCommand(Command command, Context context) {
        return Response.asText("echo " + command.name());
    }
    // space star at the end of command name let arguments in
    @OnCommand(value = "echo *", help = "This command echo")
    public Response onEchoStarCommand(Command command, Context context) {
        return Response.asText(command.name().substring(5));
    }
    
}

@OnEvent annotation case be used in order to perform an action based on events in the group. Example below demonstrates how to implement a welcome message for new members.

    @OnEvent("NEW_CHAT_MEMBER")
    public Response onNewMember(Event<UserEventPayload> event, Context context) {
        return Response.asText("Khosh oomadi lanati %s!".formatted(event.getPayload().getFirstName()));
    }

alt text

Milestones

Below are some ideas I'll implement in the comming days, no promise to respect the order though!

  • Design chatbot connector ready to implement for different platforms
  • Telegram Chatbot connector
  • Google chat Chatbot connector
  • Slack chatbot connector
  • Design plugin to make the chatbot extensible
  • Some plugins out of the box
  • List supported events

How to run examples

To test the example you need to run the shell script called set_telegram_webhook.sh in root folder following steps might be needed:

  1. Install jq. jq is a lightweight and flexible command-line JSON processor:

    • on Ubuntu:sudo apt install jq
    • on mac:sudo brew install jq
    • doc and other options: here
  2. Install ngrok:cross-platform application that enables developers to expose a local development server to the Internet with minimal effort:

    • download ngrok*.zip from: here
    • unzip downloaded file: unzip*.zip
    • log in to website
    • set your token:./ngrok authtoken <your_auth_token>
    • move it to bin:sudo mv ngrok /usr/local/bin
    • check if everything works fine by this command:ngrok http 80
  3. Get your currently existing bot API from telegrams botfather or create one.

  4. Once you have your API Key ready, run this script. Replace TELEGRAM_API_KEY with your API Key:

    • set API key in: resources/application.yml
    • ./set_telegram_webhook.sh 8080 /connectors/telegram TELEGRAM_API_KEY
  5. If everything is fine you will see:
    {"ok":true,"result":true,"description":"Webhook is already set"}

  6. Send a simple hi message to your bot to check if its working. have fun.