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

Possible to add Slack integration? #137

Open
todbot opened this issue Feb 18, 2020 · 26 comments
Open

Possible to add Slack integration? #137

todbot opened this issue Feb 18, 2020 · 26 comments

Comments

@todbot
Copy link
Owner

todbot commented Feb 18, 2020

Copy of issue todbot/blink1#275

Looks like it might be possible to add Slack integration, since IFTTT Slack channel doesn't support Slack event triggers. (But why? This may prevent us from adding it too)

Slack API: https://api.slack.com
Supported events: https://api.slack.com/events

Perhaps Blink1Control configuration is:

User inputs Web API auth token generated here: https://api.slack.com/web
User picks which events they want and what color patterns to trigger.

@Skarlso
Copy link

Skarlso commented Apr 12, 2020

Hi @todbot

If you don't mind, I started to actually hack this together. :)

@todbot
Copy link
Owner Author

todbot commented Apr 12, 2020

Hi @Skarlso, That's great news! Please let me know if you have any questions. I started looking into it before covid but the Slack API seemed pretty deep and it wasn't clear to me if it was even possible to do what I assumed everyone wanted (event on mention)

@Skarlso
Copy link

Skarlso commented Apr 12, 2020

Hey! I'll let you know when I know more. My initial research also led me to that. It looks like only bots are getting a mention event and users don't. But I'll figure something out. Maybe with the live chat thing... But I'll ask about structure for sure later on when I have my notes straight. 😊

@Skarlso
Copy link

Skarlso commented Apr 13, 2020

@todbot hey. So I thought of this. There is no direct, simple way to a mention per the API. HOWEVER! There is a way to just simply respond to messages. I'm going to use the WebAPI event, message received. That has the text. The UI will take a comma separated list of things to look for in a message, just like you can set arbitrary words to get notification on in Slack, you can have a list like, @userID,Gergely,Skarlso whatever. If those are mentioned in the TEXT portion of an event, I run the selected pattern.

Now, this could be further refined later to something like, run a different pattern on a different text mention, like, if somebody mentioned me by my userID ( which will be just the display text in Slack but passed along is the userID ) it's more important than someone writing down my name.

This can be achieved with multiple triggers right now ofc, but later on could be combined into a single view.

What say you? :)

@todbot
Copy link
Owner Author

todbot commented Apr 13, 2020

Hey that sounds like it could work. And could be much more useful than simple @mentions. (like "blink whenever 'gravitational wave' is mentioned" or whatever)
If you write a small Node program that demonstrates this, I think it could be pretty easy to integrate it into Blink1Control2.
Thanks for doing the research!

@Skarlso
Copy link

Skarlso commented Apr 13, 2020

Cool! I actually started adding a slack service and a form. But I can do the latter if you wish. :)

@todbot
Copy link
Owner Author

todbot commented Apr 13, 2020

Oh sure, if you feel comfortable enough moving around in the codebase, please try adding stuff! I just didn't want to inflict it upon you. :)

@Skarlso
Copy link

Skarlso commented Apr 13, 2020

Thanks, much appreciated. :) Humm, I don't know yet. :D I think I have the gist of it, but if I get stuck, I'll be sure to notify you. :)

@Skarlso
Copy link

Skarlso commented Apr 13, 2020

Sh*t. This still requires an App to be installed and running in some workspace. :( I misread this stuff. I thought a user could have a user token. But the user token is just there so the bot can act on the users behalf. A workspace or a community slack like gopher will never allow to install a bot from a user. :/

A user basically can't do anything. :(

Sure, you can request any scope you want—but final say always resides with the user installing your app. Like a picky eater, a user can choose to refuse any and all installs that seem to request permissions beyond what an app truly needs.

This basically kills this whole process. If you can't get an app into the workspace, this won't work.

Now. I will still add this as an RTM. ( the other option is running a server which Slack can call to ) Messages still can be received. But the bot basically needs to be in the channel. And this won't work for private conversations of course. :/

@Skarlso
Copy link

Skarlso commented Apr 14, 2020

Hi, so I'm trying to debug this stuff. :) I'm trying to run it in Dev so I can see console output and things. but it comes up as a blank screen. Any ideas why?

@todbot
Copy link
Owner Author

todbot commented Apr 14, 2020

I think I don't call this out in the README well, but are you running both npm run watch and npm run startdev at the same time?

@Skarlso
Copy link

Skarlso commented Apr 14, 2020 via email

@Skarlso
Copy link

Skarlso commented Apr 16, 2020

Ah worked! Thanks. I think I have something now. Going to test it. :)

@applefreak
Copy link

applefreak commented Nov 19, 2020

I got this to work somewhat by watching the Slack logs. Sorry @todbot I stole your code from your node-blink1-server project.

Edit: this works on macOS btw, you can probably change out the log path to your Slack's log if you're using other OS.

Just replace YOUR_HOME_FOLDER in the SLACK_LOG path;

const Tail = require('tail').Tail;
const Blink1 = require('node-blink1');

const SLACK_LOG = `${YOUR_HOME_FOLDER}/Library/Containers/com.tinyspeck.slackmacgap/Data/Library/Application Support/Slack/logs/webapp-console.log`;

const devices = Blink1.devices(); // returns array of serial numbers
let blink1 = null;

if (devices.length) {
    blink1 = new Blink1();
}

function blink1TryConnect () {
    if (blink1) {
        return false;
    }

    devices = Blink1.devices();

    if (devices.length) {
        blink1 = new Blink1();
    }

    return true;
};

// Call blink1.fadeToRGB while dealing with disconnect / reconnect of blink1
function blink1Fade (millis, r, g, b, ledn){
    blink1TryConnect();

    if (!blink1) {
        return "no blink1";
    }

    try {
        blink1.fadeToRGB(millis, r, g, b, ledn);
    } catch (err) {
        blink1 = null;
        return ""+err;
    }

    return "success";
};

function on () {
    blink1Fade(500, 255, 0, 0);
}

function off () {
    blink1Fade(500, 0, 0, 0);
}

let focused = false;

let log = new Tail(SLACK_LOG);
log.on('line', function (line) {
    if (line.match(/\[FOCUS-EVENT\] Client window focused/g)) {
        console.log('window focused');
        focused = true;
        off();
        return;
    }

    if (line.match(/\[FOCUS-EVENT\] Client window blurred/g)) {
        console.log('window blurred');
        focused = false;
        return;
    }

    if (line.match(/NEW_NOTIFICATION/g)) {
        console.log('new noti');
        if (focused === false) {
            on();
        } else {
            off();
        }
    }
});

@Skarlso
Copy link

Skarlso commented Nov 20, 2020

How would you get a hold of the slack log? How would this work on a slack which you don't control?

@applefreak
Copy link

Well I dug around Slack's source code to find their log path. I think on Windows it's in user's AppData folder. The log in question is the log of the Slack client app that's running on your computer. Mine is a copy downloaded from the Mac AppStore.

As to your second question, I'm not quite sure what you mean by "Slack you don't control". If you're in a Slack workspace, and you have the Slack client app running, you will get notifications right? The app emits some logs when you get notifications and I'm just watching that log file.

Hope this helps :)

@Skarlso
Copy link

Skarlso commented Nov 20, 2020

Ah you mean the mac client can log. Well, I don't use the app. I'm using the in browser version. :-)

@applefreak
Copy link

applefreak commented Nov 20, 2020

@Skarlso I reread the thread, it seems like you want to control the Blink(1) through some Slack command. Well my code doesn't work for that use case. Mine simply acts as an notification indicator to your own Slack client.

I think it's still possible because there are 3rd party Slack clients that have almost full abilities of a normal user. Check this out: https://github.com/wee-slack/wee-slack.

I don't know the details as to how exactly that works but the code is there.

@Skarlso
Copy link

Skarlso commented Nov 20, 2020

Yep that's exactly what I'm trying to do here #139.

My only problem is that the server isn't starting up for some reason. I'll take a second look at it today.

@Skarlso
Copy link

Skarlso commented Nov 20, 2020

Hah I actually found my error... I might be able to pull this baby off. ;)

@Skarlso
Copy link

Skarlso commented Nov 20, 2020

It WORKS!!!!!!!!!!!!!!!!!! 🥳 🥳

@Skarlso
Copy link

Skarlso commented Nov 20, 2020

Slack integration is now fully functional. I'll add some notes on how to create a classic slack app which has access to the Real Time Messaging system and what tokens to use and how. Also, need to add a slack bot user for that. After picking through a lot of documentation, it's actually quite easy.

I tested it, and got a blinker reaction on a Keyword in a message. So it's possible to set up multiple rules for different keywords. An alert with Red blinking, or just my name mention with yellow blinking... etc.

@Skarlso
Copy link

Skarlso commented Nov 20, 2020

I added entries to the Notes about some clarification on how to use this integration. @todbot Is it all good now? :)

@todbot
Copy link
Owner Author

todbot commented Nov 20, 2020

Hi @Skarlso! This is incredible, thank you! I'm looking at it now and will start doing some test builds of it over the weekend.
@applefreak thank you for the pointer to wee-slack, and your very cool (but hard to do for everyone) hack that snooped the logs.

@Skarlso
Copy link

Skarlso commented Nov 20, 2020

Thanks @todbot. :) As a side question... Are you going to pin to a version in eslint? Can I add 8 as language version and convert things to let for example?

Or, for now, let's stick with the current context so refactoring can be done more easily later on as a whole? What do you think? :)

@Skarlso
Copy link

Skarlso commented Nov 21, 2020

@todbot Something that is not working atm is multiple slack Event sources. That's because there is only one connection to the bot. :/ if you create a new event source to the same bot, that will overtake the other one.

In order to have multiple ones, you would need multiple bots. I don't think I can do anything about this I'm afraid. :/

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

No branches or pull requests

3 participants