Read the statement by Michael Teeuw here.
Attempt to Rebuild MMM-Slack with media handling, includes dont seem to load
-
I’m banging my head to rewrite the MMM-Slack module to unfurl links and media shared on slack onto the screen using the most current version of @slack node sdk. My first test code run from node seems to work lovely:
var NodeHelper = require('node_helper'); var RtmClient = require('@slack/client').RTMClient; // Not sure we need this: // var CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS; // These hold the variables for us to extra the message data // at some point this needs to come from the config var rtm = new RtmClient('xxxxxxKEY_REDACTEDxxxxx'); console.log(RtmClient); rtm.start(); let channel; rtm.on('authenticated', (rtmStartData) => { console.log('connected'); }); rtm.on('message', function(message) { console.log(message.message); }); rtm.on('slack_event', function(event) { console.log(event); });
i get a nice output of the messages from slack in JSON, however when i use similiar code to send notifications to the new module in node_helper.js. rtm is never defined! my debug has led me to it not even being defined.included at the top. Here’s my code for the stripped version:
var NodeHelper = require("node_helper"); var RtmClient = require('@slack/client').RtmClient; // Not sure we need this: var CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS; var userName = ''; var messageText = ''; var messages = []; module.exports = NodeHelper.create({ start: function () { //this.config = {} }, socketNotificationReceived: function (notification, payload) { if (notification === 'START_CONNECTION') { // startslackconnects sends config to front end this.config = payload; //this.sendSocketNotification(notification , payload); this.startSlackConnection(payload); } }, startSlackConnection: function(config) { var self = this; var token = config.config.slackToken; var rtm = new RtmClient('xxxREDACTED KEY'); // this.sendSocketNotification('SIGN IN' , rtm); this shows NULL on the front end! rtm.start(); let channel; rtm.on('authenticated', (rtmStartData) => { this.sendSocketNotifcation('AUTH' , "CONNECTED"); }); rtm.on('message', function(message) { this.sendSocketNotifcation('SLACK_DATA' , message); }); rtm.on('slack_event', function(event) { this.sendSocketNotifcation('SLACK_DATA' , event); }); } });
I did noticed that a directory structure for the updated @slack/client is different than the one from the original module (and it doesnt have a index.js in the top directory. I dont know if that has anything to do with it. I have the old version in the original MMM-Slack directory and that works fine. As I mentioned before when i run the version above from the command line , it works fine - defining rtm (RtmClient) and using it to connect and pull data, but once i try the same code in node_helper.js as the second piece of code… no go. rtm is blank and no data is found.
It sends all data to the front end sucessfully wether its the payload is full or not, I’m just coming up Null or worse with trying to include the slack/client
Any ideas where I can start looking to fix this?
-
@mediathreat just from looking at the code there is something missing (this should result in an error)
rtm.on('message', function(message) { this.sendSocketNotifcation('SLACK_DATA' , message); }); this.sendSocketNotifcation('SLACK_DATA' , event); });
it should be, look at slack event and use arrow functions to keep your scope to be able to call the notification method
rtm.on('message', (message) => { this.sendSocketNotifcation('SLACK_DATA' , message); }); rtm.on('slack_event', (event) => { this.sendSocketNotifcation('SLACK_DATA' , event); });
-
@strawberry-3.141 oops thanks for that, yeah- i cut and paste in two sections and forgot that part.
Is there a way I can check to debug why it wont load the
var RtmClient = require(‘@slack/client’).RTMClient;
-
@mediathreat dou you have it in your local node_modules folder?
-
Actually the fix above got the whole thing working and I was able to strip the code down quite a bit with the new simpler function calls. Thank you! I hope to have a updated module posted to the forum soon from github
@strawberry-3.141 said in Attempt to Rebuild MMM-Slack with media handling, includes dont seem to load:
@mediathreat dou you have it in your local node_modules folder?
-
@mediathreat That sound’s great and i’m looking forward for your module :)
-
a slack module sounds great