Get Started with Java

Introduction

Bots can be built using Java in two ways:

Both options are described below.

The Java client is built in an event handler architecture. If you are building a bot that listens to conversations, you implement an interface of a listener with the functions to handle all events that come through the data feed.

Using a Generated Java Bot

The Symphony bot generator generates a simple test bot with the parts you need to quickly get started.

  1. Follow the instructions to install and run the Symphony bot generator.
  2. Follow the prompts to get a Java bot.
  3. Open the project in your IDE of choice by choosing the generated pom.xml file. This opens the project as a Maven project and loads the dependencies for you.
  1. Review config.json and make sure the configuration parameters are correct.
  2. Run the BotExample main class:

Main Class Example

The following shows an example of using the package:

import authentication.SymBotAuth;
import clients.SymBotClient;
import configuration.SymConfig;
import configuration.SymConfigLoader;
import listeners.IMListener;
import listeners.RoomListener;
import model.*;
import services.DatafeedEventsService;
import javax.ws.rs.core.NoContentException;

import java.net.URL;
import java.util.List;

public class BotExample {

    public static void main(String [] args) {
        BotExample app = new BotExample();
    }
    public BotExample() {
        URL url = getClass().getResource("config.json");
        SymConfigLoader configLoader = new SymConfigLoader();
        SymConfig config = configLoader.loadFromFile(url.getPath());
        SymBotAuth botAuth = new SymBotAuth(config);
        botAuth.authenticate();
        SymBotClient botClient = SymBotClient.initBot(config, botAuth);
        DatafeedEventsService datafeedEventsService = botClient.getDatafeedEventsService();
        RoomListener roomListenerTest = new RoomListenerTestImpl(botClient);
        datafeedEventsService.addRoomListener(roomListenerTest);
        IMListener imListener = new IMListenerImpl(botClient);
        datafeedEventsService.addIMListener(imListener);

    }
}

RoomListener Example

public class RoomListenerTestImpl implements RoomListener {

    private SymBotClient botClient;

    public RoomListenerTestImpl(SymBotClient botClient) {
        this.botClient = botClient;
    }

    private final Logger logger = LoggerFactory.getLogger(RoomListenerTestImpl.class);

    public void onRoomMessage(InboundMessage inboundMessage) {
        OutboundMessage messageOut = new OutboundMessage();
        messageOut.setMessage("Hi "+inboundMessage.getUser().getFirstName()+"!");
        try {
            this.botClient.getMessagesClient().sendMessage(inboundMessage.getStream().getStreamId(), messageOut);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void onRoomCreated(RoomCreated roomCreated) {

    }

    public void onRoomDeactivated(RoomDeactivated roomDeactivated) {

    }

    public void onRoomMemberDemotedFromOwner(RoomMemberDemotedFromOwner roomMemberDemotedFromOwner) {

    }

    public void onRoomMemberPromotedToOwner(RoomMemberPromotedToOwner roomMemberPromotedToOwner) {

    }

    public void onRoomReactivated(Stream stream) {

    }

    public void onRoomUpdated(RoomUpdated roomUpdated) {

    }

    public void onUserJoinedRoom(UserJoinedRoom userJoinedRoom) {
        OutboundMessage messageOut = new OutboundMessage();
        messageOut.setMessage("Welcome "+userJoinedRoom.getAffectedUser().getFirstName()+"!");
        try {
            this.botClient.getMessagesClient().sendMessage(userJoinedRoom.getStream().getStreamId(), messageOut);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void onUserLeftRoom(UserLeftRoom userLeftRoom) {

    }
}

Using the Java Client

  1. Add symphony-api-client-java dependency to the pom.xml file of your Maven project:
<dependency>
            <groupId>com.symphony.platformsolutions</groupId>
            <artifactId>symphony-api-client-java</artifactId>
            <version>1.0.2</version>
</dependency>
  1. Add the configuration file as a resource of your project
  2. Create a main class that includes the initialization of the client and load the config by pointing to the path to your config.json.
  3. Implement the RoomListener, IMListener, and/or ConnectionListener and add them to the DatafeedEventsService, which reads events on the data feed and triggers your implemented functions.

Get Started with Java