Used to listen to events in Webex Teams on a localhost port. The only command it can understand is hello, replies "hello there". This simple Java-based bot serves as a perfect testing companion for webhook forwarding applications and demonstrates how to build a basic Webex Teams bot using Java and the Cisco Spark Java SDK.
Perfect for:
- Testing webhook forwarding with applications like hookbuster
- Local development without requiring public webhook endpoints
- Learning Java bot development with Webex Teams
- Prototyping bot interactions in a controlled environment
- Firewall-friendly testing for applications behind corporate firewalls
- Webex Teams access token: this will be used to communicate with Webex Teams. Ideally you would use the access token of the same bot here.
- Open port on localhost: this is where the app will listen for incoming messages
- Java 1.8: Required runtime environment
- Maven 3.6.1: Build and dependency management
Quick Setup:
# Clone the repository
git clone https://github.com/WebexSamples/javabot.git
cd javabot
# Install dependencies and compile
mvn install
# Run the application
mvn exec:java -Dexec.mainClass=MainWhen you run the application, you'll see the interactive CLI:
+---------------------------------------------+
| |
| _ _ _ |
| | | __ ___ ____ _| |__ ___ | |_ |
| _ | |/ _` \ \ / / _` | '_ \ / _ \| __| |
| | |_| | (_| |\ V / (_| | |_) | (_) | |_ |
| \___/ \__,_| \_/ \__,_|_.__/ \___/ \__| |
| |
| |
+---------------------------------------------+
Please enter an access token
AaBbCcDdEeFfGgHhIiJjAaBbCcDdEeFfGgHhIiJjAaBbCcDdEeFfGgHhIiJj_ABCD_1a2b3c4d-1234-abcd-9876-abcdefghijkl
INFO Verifying account
INFO Account verified as Droid
Please enter port you want to listen on:
5000
INFO Service listening on localhost:5000
The application will prompt you for:
- Access Token: Your Webex Teams bot access token
- Port Number: Local port to listen for incoming HTTP requests (e.g., 5000)
Once running, the bot responds to:
- hello: Responds with "hello there π"
Tip: You can use the hookbuster to test how this app works. Start a websocket listener for messages:created and the hookbuster will forward incoming message events to localhost, where javabot can listen to them. No need for webhooks!
javabot/
βββ src/main/java/
β βββ Main.java # Application entry point and CLI
β βββ bot/
β β βββ Bot.java # Webex Teams API wrapper
β β βββ BotService.java # HTTP server and message processing
β βββ util/
β βββ Printer.java # Console output utilities
βββ pom.xml # Maven configuration
βββ javaBot.iml # IntelliJ IDEA project file
βββ README.md # This file
- Main.java: Entry point that handles CLI interactions and initializes the bot
- Bot.java: Wrapper around the Cisco Spark SDK with convenience methods
- BotService.java: HTTP server that listens for incoming requests and processes messages
- Printer.java: Utility class for colored console output
Webex Teams β hookbuster β HTTP POST β javabot β Bot Response β Webex Teams
- HTTP Request: Receives JSON payload from webhook/hookbuster
- Message Parsing: Extracts message content and metadata
- Command Detection: Looks for "hello" in message text
- Response Generation: Sends reply back to Webex Teams
- Bot Filtering: Ignores messages from other bots
public class Bot {
private Spark spark;
public Bot(String accessToken) {
spark = Spark.builder()
.baseUrl(URI.create("https://api.ciscospark.com/v1"))
.accessToken(accessToken)
.build();
}
void sayHello(String roomId) {
Message message = new Message();
message.setRoomId(roomId);
message.setMarkdown("hello there π");
spark.messages().post(message);
}
}public class BotService {
public void run() {
ServerSocket serverSocket = new ServerSocket(port);
while (true) {
Socket socket = serverSocket.accept();
JSONObject requestBody = getRequestBodyFromInput(socket);
evaluateRequest(requestBody);
}
}
private void evaluateRequest(JSONObject requestBody) {
String resource = (String) requestBody.get("resource");
if (resource.equals("messages")) {
// Process message and respond to "hello"
if (messageList.contains("hello")) {
bot.sayHello(roomId);
}
}
}
}<dependencies>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>com.ciscospark</groupId>
<artifactId>ciscospark-client</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
</dependencies>- javax.json: JSON processing API
- ciscospark-client: Official Cisco Spark Java SDK
- org.json: JSON parsing and manipulation library
The recommended way to test javabot is with hookbuster:
- Start JavaBot: Run javabot and set it to listen on port 5000
- Start Hookbuster: Configure hookbuster to forward messages to localhost:5000
- Test Interaction: Send "hello" to your bot in Webex Teams
- Verify Response: Bot should respond with "hello there π"
You can also test manually by sending HTTP POST requests:
curl -X POST http://localhost:5000 \
-H "Content-Type: application/json" \
-d '{
"resource": "messages",
"data": {
"id": "message-id",
"roomId": "room-id",
"personEmail": "user@example.com"
}
}'JavaBot expects JSON payloads in this format:
{
"resource": "messages",
"event": "created",
"data": {
"id": "message-id",
"roomId": "room-id",
"personId": "person-id",
"personEmail": "user@example.com",
"text": "hello bot",
"created": "2023-01-01T12:00:00.000Z"
}
}JavaBot automatically filters out messages from other bots:
if (!message.getPersonEmail().contains("@webex.bot")) {
// Process message from human user
}The application validates access tokens before starting:
private static boolean verifyAccount(String accessToken) {
try {
Person me = bot.getMyDetails();
return true;
} catch (Exception e) {
return false;
}
}JavaBot provides colored console output for better visibility:
- INFO: Cyan text for general information
- ERROR: Red text for error messages
- Logo: ASCII art banner on startup
INFO Verifying account
INFO Account verified as DROID
INFO Service listening on localhost:5000
INFO Request received
INFO {"resource":"messages","data":{"id":"123","roomId":"456"}}
# Compile the project
mvn compile
# Run tests (if any)
mvn test
# Package as JAR
mvn package
# Run with custom main class
mvn exec:java -Dexec.mainClass=MainThe project includes IntelliJ IDEA configuration (javaBot.iml). You can:
- Import Project: Open the project in IntelliJ IDEA
- Maven Integration: IDE will automatically handle dependencies
- Run Configuration: Set up run configuration for Main.java
To add new commands:
- Modify BotService.java: Add new command detection logic
- Update Bot.java: Add new response methods
- Test: Verify new commands work with hookbuster
Example command addition:
// In evaluateRequest method
if (messageList.contains("goodbye")) {
bot.sayGoodbye(roomId);
}
// In Bot.java
void sayGoodbye(String roomId) {
Message message = new Message();
message.setRoomId(roomId);
message.setMarkdown("goodbye! π");
spark.messages().post(message);
}Port Already in Use
ERROR Address already in use
ERROR Service not running
Solution: Choose a different port or stop the process using the port
Invalid Access Token
ERROR Access token invalid
Solution: Verify your token has the correct permissions and hasn't expired
Network Issues
ERROR Connection refused
Solution: Check network connectivity and firewall settings
Add debug logging by modifying the Printer class:
public static void printDebug(String message) {
System.out.println(YELLOW_BRIGHT + "DEBUG " + message + RESET);
}- Memory: Minimal footprint, mainly for HTTP server and SDK
- CPU: Low usage, event-driven processing
- Network: Outbound HTTPS to Webex APIs only
- Single-threaded: Handles one request at a time
- Lightweight: Suitable for development and testing
- Blocking I/O: Uses traditional Java I/O model
For production use, consider:
- Threading: Add multi-threading for concurrent request handling
- Error Recovery: Implement retry logic for API failures
- Logging: Add comprehensive logging framework
- Configuration: Externalize configuration (properties files)
- Security: Implement proper authentication and authorization
FROM openjdk:8-jre-slim
WORKDIR /app
COPY target/javaBot-1.0-SNAPSHOT.jar app.jar
COPY target/lib/ lib/
EXPOSE 5000
CMD ["java", "-cp", "app.jar:lib/*", "Main"]- Fork the repository
- Create a feature branch:
git checkout -b feature/new-feature - Make your changes
- Test with hookbuster integration
- Submit a pull request
- Follow Java naming conventions
- Add JavaDoc comments for public methods
- Test all new functionality with hookbuster
- Maintain backward compatibility
This project is licensed under the terms specified in the LICENSE file.
- Hookbuster: WebSocket-to-HTTP event forwarder
- Webex Node Bot Framework: Node.js bot framework
- Cisco Spark Java SDK: Official Java SDK
- Webex Developer Portal
- Cisco Spark Java SDK Documentation
- Webex Teams API Reference
- Maven Documentation
- Create an issue in this repository
- Review Webex Developer Documentation
- Join the Webex Developer Community
Repository: https://github.com/WebexSamples/javabot