Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload dockerfile #178

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM java:latest

ADD ./*.jar app.jar

EXPOSE 8080

ENTRYPOINT ["java","-jar","/app.jar"]

MAINTAINER Shuyu Zhang
127 changes: 127 additions & 0 deletions jbot-example/src/main/java/example/jbot/slack/Meteoroid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package example.jbot.slack;

import okhttp3.*;

import java.io.File;
import java.io.IOException;

public class Meteoroid {

private File uploadFile;
private String token;
private String channels;
private String fileType;
private String mediaType;
private String title;
private String initialComment;

private Meteoroid(Builder builder) {
if (builder.token == null) {
new RuntimeException("token is required.");
}
if (builder.uploadFile == null) {
new RuntimeException("uploadFile is required.");
}
uploadFile = builder.uploadFile;
token = builder.token;
channels = builder.channels;
fileType = builder.fileType;
mediaType = builder.mediaType;
title = builder.title;
initialComment = builder.initialComment;
}

public Response post() throws IOException {
RequestBody requestBody = createRequestBody();
Request request = createRequest(requestBody);
OkHttpClient client = new OkHttpClient();
return client.newCall(request).execute();
}

public void post(Callback callback) throws IOException {
RequestBody requestBody = createRequestBody();
Request request = createRequest(requestBody);
OkHttpClient client = new OkHttpClient();
client.newCall(request).enqueue(callback);
}

private RequestBody createRequestBody() {
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
multipartBuilder.setType(MultipartBody.FORM);
multipartBuilder.addFormDataPart("file", uploadFile.getName(),
RequestBody.create(MediaType.parse(mediaType != null ? mediaType : ""), uploadFile));
multipartBuilder.addFormDataPart("token", token);
if (channels != null) {
multipartBuilder.addFormDataPart("channels", channels);
}
if (fileType != null) {
multipartBuilder.addFormDataPart("filetype", fileType);
}
if (title != null) {
multipartBuilder.addFormDataPart("title", title);
}
if (initialComment != null) {
multipartBuilder.addFormDataPart("initial_comment", initialComment);
}
return multipartBuilder.build();
}

private Request createRequest(RequestBody requestBody) {
return new Request.Builder()
.url("https://slack.com/api/files.upload")
.post(requestBody)
.build();
}

public static final class Builder {
private File uploadFile;
private String token;
private String channels;
private String fileType;
private String mediaType;
private String title;
private String initialComment;

public Builder() {
}

public Builder uploadFile(File uploadFile) {
this.uploadFile = uploadFile;
return this;
}

public Builder token(String token) {
this.token = token;
return this;
}

public Builder channels(String channels) {
this.channels = channels;
return this;
}

public Builder fileType(String fileType) {
this.fileType = fileType;
return this;
}

public Builder mediaType(String mediaType) {
this.mediaType = mediaType;
return this;
}

public Builder title(String title) {
this.title = title;
return this;
}

public Builder initialComment(String initialComment) {
this.initialComment = initialComment;
return this;
}

public Meteoroid build() {
return new Meteoroid(this);
}
}
}
93 changes: 58 additions & 35 deletions jbot-example/src/main/java/example/jbot/slack/SlackBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import java.util.regex.Matcher;

/**
* A simple Slack Bot. You can create multiple bots by just
* extending {@link Bot} class like this one. Though it is
* recommended to create only bot per jbot instance.
* A simple Slack Bot. You can create multiple bots by just extending
* {@link Bot} class like this one. Though it is recommended to create only bot
* per jbot instance.
*
* @author ramswaroop
* @version 1.0.0, 05/06/2016
Expand Down Expand Up @@ -45,33 +45,54 @@ public Bot getSlackBot() {
}

/**
* Invoked when the bot receives a direct mention (@botname: message)
* or a direct message. NOTE: These two event types are added by jbot
* to make your task easier, Slack doesn't have any direct way to
* determine these type of events.
* Invoked when the bot receives a direct mention (@botname: message) or a
* direct message. NOTE: These two event types are added by jbot to make your
* task easier, Slack doesn't have any direct way to determine these type of
* events.
*
* @param session
* @param event
*/
@Controller(events = {EventType.DIRECT_MENTION, EventType.DIRECT_MESSAGE})
@Controller(events = { EventType.DIRECT_MENTION, EventType.DIRECT_MESSAGE })
public void onReceiveDM(WebSocketSession session, Event event) {
reply(session, event, "Hi, I am " + slackService.getCurrentUser().getName());
if (event.getText().contains("music")) {
try {
postFileToSlack(session, event);
} catch (Exception e) {
;
}
} else {
reply(session, event, "Hi, I am " + slackService.getCurrentUser().getName());
}
}

/**
* Invoked when bot receives an event of type DIRECT_MENTION or DIRECT_MESSAGE
* with text containing "music"
*
* @param session: The seesion between slack and jbot
* @param event: including the main message prefer to send
*/
public void postFileToSlack(WebSocketSession session, Event event) throws IOException {
File file = new File("demo.mp3"); // add file path here
okhttp3.Response response = new Meteoroid.Builder().token(slackToken).channels(event.getChannelId())
.uploadFile(file).build().post();
reply(session, event, "Enjoy the music!");
response.close();
}

/**
* Invoked when bot receives an event of type message with text satisfying
* the pattern {@code ([a-z ]{2})(\d+)([a-z ]{2})}. For example,
* messages like "ab12xy" or "ab2bc" etc will invoke this method.
* Invoked when bot receives an event of type message with text satisfying the
* pattern {@code ([a-z ]{2})(\d+)([a-z ]{2})}. For example, messages like
* "ab12xy" or "ab2bc" etc will invoke this method.
*
* @param session
* @param event
*/
@Controller(events = EventType.MESSAGE, pattern = "^([a-z ]{2})(\\d+)([a-z ]{2})$")
public void onReceiveMessage(WebSocketSession session, Event event, Matcher matcher) {
reply(session, event, "First group: " + matcher.group(0) + "\n" +
"Second group: " + matcher.group(1) + "\n" +
"Third group: " + matcher.group(2) + "\n" +
"Fourth group: " + matcher.group(3));
reply(session, event, "First group: " + matcher.group(0) + "\n" + "Second group: " + matcher.group(1) + "\n"
+ "Third group: " + matcher.group(2) + "\n" + "Fourth group: " + matcher.group(3));
}

/**
Expand All @@ -86,11 +107,11 @@ public void onPinAdded(WebSocketSession session, Event event) {
}

/**
* Invoked when bot receives an event of type file shared.
* NOTE: You can't reply to this event as slack doesn't send
* a channel id for this event type. You can learn more about
* <a href="https://api.slack.com/events/file_shared">file_shared</a>
* event from Slack's Api documentation.
* Invoked when bot receives an event of type file shared. NOTE: You can't reply
* to this event as slack doesn't send a channel id for this event type. You can
* learn more about
* <a href="https://api.slack.com/events/file_shared">file_shared</a> event from
* Slack's Api documentation.
*
* @param session
* @param event
Expand All @@ -100,37 +121,38 @@ public void onFileShared(WebSocketSession session, Event event) {
logger.info("File shared: {}", event);
}


/**
* Conversation feature of JBot. This method is the starting point of the conversation (as it
* calls {@link Bot#startConversation(Event, String)} within it. You can chain methods which will be invoked
* one after the other leading to a conversation. You can chain methods with {@link Controller#next()} by
* Conversation feature of JBot. This method is the starting point of the
* conversation (as it calls {@link Bot#startConversation(Event, String)} within
* it. You can chain methods which will be invoked one after the other leading
* to a conversation. You can chain methods with {@link Controller#next()} by
* specifying the method name to chain with.
*
* @param session
* @param event
*/
@Controller(pattern = "(setup meeting)", next = "confirmTiming")
public void setupMeeting(WebSocketSession session, Event event) {
startConversation(event, "confirmTiming"); // start conversation
startConversation(event, "confirmTiming"); // start conversation
reply(session, event, "Cool! At what time (ex. 15:30) do you want me to set up the meeting?");
}

/**
* This method will be invoked after {@link SlackBot#setupMeeting(WebSocketSession, Event)}.
* This method will be invoked after
* {@link SlackBot#setupMeeting(WebSocketSession, Event)}.
*
* @param session
* @param event
*/
@Controller(next = "askTimeForMeeting")
public void confirmTiming(WebSocketSession session, Event event) {
reply(session, event, "Your meeting is set at " + event.getText() +
". Would you like to repeat it tomorrow?");
nextConversation(event); // jump to next question in conversation
reply(session, event, "Your meeting is set at " + event.getText() + ". Would you like to repeat it tomorrow?");
nextConversation(event); // jump to next question in conversation
}

/**
* This method will be invoked after {@link SlackBot#confirmTiming(WebSocketSession, Event)}.
* This method will be invoked after
* {@link SlackBot#confirmTiming(WebSocketSession, Event)}.
*
* @param session
* @param event
Expand All @@ -139,15 +161,16 @@ public void confirmTiming(WebSocketSession session, Event event) {
public void askTimeForMeeting(WebSocketSession session, Event event) {
if (event.getText().contains("yes")) {
reply(session, event, "Okay. Would you like me to set a reminder for you?");
nextConversation(event); // jump to next question in conversation
nextConversation(event); // jump to next question in conversation
} else {
reply(session, event, "No problem. You can always schedule one with 'setup meeting' command.");
stopConversation(event); // stop conversation only if user says no
stopConversation(event); // stop conversation only if user says no
}
}

/**
* This method will be invoked after {@link SlackBot#askTimeForMeeting(WebSocketSession, Event)}.
* This method will be invoked after
* {@link SlackBot#askTimeForMeeting(WebSocketSession, Event)}.
*
* @param session
* @param event
Expand All @@ -159,6 +182,6 @@ public void askWhetherToRepeat(WebSocketSession session, Event event) {
} else {
reply(session, event, "Okay, don't forget to attend the meeting tomorrow :)");
}
stopConversation(event); // stop conversation
stopConversation(event); // stop conversation
}
}