rampatra / jbot

Make Slack and Facebook Bots in Java.
GNU General Public License v3.0
1.2k stars 352 forks source link

Upload file to slack #166

Open Elfilali-Taoufiq opened 4 years ago

Elfilali-Taoufiq commented 4 years ago

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

Lionelsy commented 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();
    }

in SlackBot.java

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

result
Ronin3502 commented 4 years ago

yeah, the above code is quite helpful!

Elfilali-Taoufiq commented 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();
    }

in SlackBot.java

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

result
Elfilali-Taoufiq commented 4 years ago

so helpful Mr @Lionelsy , thank you so much

Lionelsy commented 4 years ago

so helpful Mr @Lionelsy , thank you so much

My pleasure : )