I am trying to test my new Slackbot by sending it a greeting with the expected behavior that it replies with 'this is a test message' in accordance with my code here in slackClient.js:
'use strict'
const RtmClient = require('@slack/client').RtmClient;
const CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS;
const RTM_EVENTS = require('@slack/client').RTM_EVENTS;
let rtm = null;
function handleOnAuthenticated(rtmStartData) {
console.log(`Logged in as ${rtmStartData.self.name} of team ${rtmStartData.team.name}, but not yet connected to a channel`);
}
function handleOnMessage(message) {
console.log(message);
// This will send the message 'this is a test message' to the channel identified by id 'C0CHZA86Q'
rtm.sendMessage('this is a test message', message.channel, function messageSent() {
// optionally, you can supply a callback to execute once the message has been sent
});
}
function addAuthenticatedHandler(rtm, handler) {
rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, handler);
}
module.exports.init = function slackClient(bot_token, logLevel){
const rtm = new RtmClient(bot_token);
addAuthenticatedHandler(rtm, handleOnAuthenticated);
rtm.on(RTM_EVENTS.MESSAGE, handleOnMessage)
return rtm;
}
module.exports.addAuthenticatedHandler = addAuthenticatedHandler;
This is the error I get:
rtm.sendMessage('this is a test message', message.channel, function messageSent() {
^
TypeError: Cannot read property 'sendMessage' of null
If I understand this correctly, it's telling me I cannot give rtm a value of null, but then what kind of boilerplate value can I give it just so that I can test this out?
I found my error, I had rtm as a const and then a let. So I removed the const from rtm = new RtmClient(bot_token); and moved on to add nlp like so:
'use strict'
const RtmClient = require('@slack/client').RtmClient;
const CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS;
const RTM_EVENTS = require('@slack/client').RTM_EVENTS;
let rtm = null;
let nlp = null;
function handleOnAuthenticated(rtmStartData) {
console.log(`Logged in as ${rtmStartData.self.name} of team ${rtmStartData.team.name}, but not yet connected to a channel`);
}
function handleOnMessage(message) {
// This will send the message 'this is a test message' to the channel identified by id 'C0CHZA86Q'
rtm.sendMessage('this is a test message', message.channel, function messageSent() {
// optionally, you can supply a callback to execute once the message has been sent
});
}
function addAuthenticatedHandler(rtm, handler) {
rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, handler);
}
module.exports.init = function slackClient(bot_token, logLevel, nlpClient){
rtm = new RtmClient(bot_token);
nlp = nlpClient;
addAuthenticatedHandler(rtm, handleOnAuthenticated);
rtm.on(RTM_EVENTS.MESSAGE, handleOnMessage)
return rtm;
}
module.exports.addAuthenticatedHandler = addAuthenticatedHandler;