DEV Community

Andrii Maliuta
Andrii Maliuta

Posted on

WebEx bot with webex-java-sdk and Spring. Part 1

To use the Webex chat you need to register for a Webex Account at https://developer.webex.com/.

Create WebEx app that will server as your bot

Go to https://developer.webex.com/my-apps and create the app that will be acting as your bot:

Image description

Create Spring Java App

It is not mandatory to use Spring - any way of creating Web App with Java framework can do. Also, a serverless apps or lambdas can be used to host the code.

We will use Spring Boot app:

  1. Groovy
  2. Gradle
  3. Spring Web
  4. Gson (parse JSON)
  5. Jsoup (parse HTML messages)

Gradle dependencies code:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'  
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'javax.json:javax.json-api:1.1.4'
    implementation 'org.glassfish:javax.json:1.1'
    implementation 'com.google.code.gson:gson:2.8.9'
    implementation('org.jsoup:jsoup:1.14.3')
    implementation 'org.codehaus.groovy:groovy'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Enter fullscreen mode Exit fullscreen mode

Install Spark SDK

Not to create models and services to interact with Webex and use HTTP calls, we can use the WebEx Java SDK (https://github.com/webex/webex-java-sdk (Spark Java SDK)).
It can be installed via Gradle - use gradle install to locate the code locally in maven repos.

App code

We will create RoomService that will interact with WebEx Room that exist in WebEx of our account.

RoomService.groovy

import com.anma.wbb.webexbotbsg.Room

interface RoomService {

    List<Room> getRooms(teamId, type, sortBy, max);
    public Room getRoom(String id);

}
Enter fullscreen mode Exit fullscreen mode

RoomServiceImpl class that implements RoomService interface

import com.anma.wbb.webexbotbsg.Room
import com.anma.wbb.webexbotbsg.Spark
import org.springframework.stereotype.Service

@Service
class RoomServiceImpl implements RoomService {

    String testBot = System.getenv("WEBEX_TEST_BOT");
    String myToken = System.getenv("WEBEX_MY_TOKEN");

    Spark spark = Spark.builder()
            .baseUrl(URI.create("https://api.ciscospark.com/v1"))
            .accessToken(bsgBot)
            .build();

    @Override
    public Room getRoom(String id) {
        Room room = spark.rooms().path("/${id}").get()
        return room
    }

    @Override
    List<Room> getRooms(teamId, type, sortBy, max) {

        List<Room> rooms = new ArrayList<>()
        spark.rooms()
                .iterate()
                .forEachRemaining(room -> {
//                    System.out.println(room.getTitle() + ", created " + room.getCreated() + ": " + room.getId());
                    rooms.add(room)
                })

        return rooms
    }
}
Enter fullscreen mode Exit fullscreen mode

So, now we have the service that gets rooms and its implementation. We can also add the services that will interact with messages that are present in rooms.

Oldest comments (0)