Open Elfilali-Taoufiq opened 4 years ago
Maybe you can try this piece of code
/**
* 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})
public void onReceiveDM(WebSocketSession session, Event event) {
boolean flag = true;
if(event.getText().contains("paper")){
flag = false;
try{postFileToSlack(session, event);}
catch(Exception e){;}
}
if(flag){
reply(session, event, "Hi, I am " + slackService.getCurrentUser().getName());
}
}
public void postFileToSlack(WebSocketSession session, Event event) throws IOException {
File file = new File("demo.pdf");
okhttp3.Response response = new Meteoroid.Builder()
.token(slackToken)
.channels(event.getChannelId())
.uploadFile(file)
.build()
.post();
reply(session, event, "file received!");
response.close();
}
Here you need to install the okhttp3 and Meteoroid, (For Convenience), i just download the Meteoroid.java to the same directory of SlackBot.java
You can add more check or operations to send different files.
Here is the effect in my bot
yeah, the above code is quite helpful!
Maybe you can try this piece of code
/** * 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}) public void onReceiveDM(WebSocketSession session, Event event) { boolean flag = true; if(event.getText().contains("paper")){ flag = false; try{postFileToSlack(session, event);} catch(Exception e){;} } if(flag){ reply(session, event, "Hi, I am " + slackService.getCurrentUser().getName()); } } public void postFileToSlack(WebSocketSession session, Event event) throws IOException { File file = new File("demo.pdf"); okhttp3.Response response = new Meteoroid.Builder() .token(slackToken) .channels(event.getChannelId()) .uploadFile(file) .build() .post(); reply(session, event, "file received!"); response.close(); }
Here you need to install the okhttp3 and Meteoroid, (For Convenience), i just download the Meteoroid.java to the same directory of SlackBot.java
You can add more check or operations to send different files.
Here is the effect in my bot
so helpful Mr @Lionelsy , thank you so much
so helpful Mr @Lionelsy , thank you so much
My pleasure : )
I saw the issue about uploading file to slack
public void postFileToSlack(Event event, File file) throws IOException { okhttp3.Response response = new Meteoroid.Builder() .token("YOUR-TOKEN") .channels(event.getChannelId()) .uploadFile(file) .build() .post(); response.close(); }
but i can't get it, where can i add the function in JBot code, help me please