xBytez / slackbotapi

node.js Slack RTM API module
GNU Lesser General Public License v3.0
136 stars 40 forks source link

[Question] manually run 'rtm.start' on an instance of slackAPI #14

Closed franciskim closed 9 years ago

franciskim commented 9 years ago

This could be considered a more general JavaScript question.

I've made a 'greeter' bot - when someone joins, it'll message them a series of things.

For me to do this, I've needed to re-instantiate the slackAPI when someone joins:

slack.on('team_join', function (data) {
    slack = new slackAPI({
        'token': "abcdedfg",
        'logging': true
    });

I believe this resulted in multiple slackAPI instances being instantiated, and due to the lack of my JavaScript knowledge I do not know a better way to handle this.

The below code needs to be triggered to refresh the user list, as trying to PM a new user that has joined will error:

self.reqAPI('rtm.start', {}, function (data) {
        self.slackData.self = data.self;
        self.slackData.team = data.team;
        self.slackData.channels = data.channels;
        self.slackData.groups = data.groups;
        self.slackData.users = data.users;
        self.slackData.ims = data.ims;
        self.connectSlack(data.url, function(err, data){
            if (!err){
                self.emit(events[data.type], data);
            }
        });
    });

Any help is appreciated!

franciskim commented 9 years ago

For now, I've done the following:

slack.on('team_join', function (data) {
    slack = new slackAPI({
        'token': "foobar",
        'logging': true
    });
    setTimeout(function () {
        slack.sendPM(data.user.name, ":beers: Welcome to DEVANZ @" + data.user.name + "! :sharkdance:");
    }, 10000);
    setTimeout(function () {
        slack = null;
    }, 105000);
franciskim commented 9 years ago

The above hack/workaround has proven not to work. Any ideas on refreshing the channel/chat data when someone new joins?

benhinchley commented 9 years ago

@franciskim I have just been looking into the same type of thing, am about to fork this so I can mess around to find something that will work. I'll let you know what I find

benhinchley commented 9 years ago

I haven't tested this at all, so this could be utterly useless but @franciskim I think something like this should work

slack.on('team_join', function(data) {
    // make a request to the users.list method and save it to slackData.users
    slack.reqAPI('users.list', {}, function(data) {
        // this is probably not accessing that datastore correctly
        slackData.users = data.members;
    });
});
franciskim commented 9 years ago

hey @benhinchley - yeah I'd say that won't be in the right scope. For now, I have done it the manual way, I have my script running via PM2, and when someone joins I just process.exit(); XD

benhinchley commented 9 years ago

@franciskim I just tested something similar and it works, so might be worth a shot? but haha good hack :P

franciskim commented 9 years ago

That's good to hear @benhinchley - do you think the new user details will still be available in the subsequent calls? I think it would just stay in the scope of slack.on('team_join', function(data) { but I could be wrong.

franciskim commented 9 years ago

addressed in https://github.com/xBytez/slackbotapi/pull/19