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 file to slack #166

Open
Elfilali-Taoufiq opened this issue Jan 22, 2020 · 5 comments
Open

Upload file to slack #166

Elfilali-Taoufiq opened this issue Jan 22, 2020 · 5 comments

Comments

@Elfilali-Taoufiq
Copy link

Elfilali-Taoufiq commented Jan 22, 2020

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
Copy link

Lionelsy commented May 7, 2020

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
Copy link

yeah, the above code is quite helpful!

@Elfilali-Taoufiq
Copy link
Author

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
Copy link
Author

so helpful Mr @Lionelsy , thank you so much

@Lionelsy
Copy link

Lionelsy commented May 8, 2020

so helpful Mr @Lionelsy , thank you so much

My pleasure : )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants