2017-05-15 2 views
0

Ich versuche, meinen neuen Slackbot zu testen, indem ich ihm eine Begrüßung mit dem erwarteten Verhalten, dass er mit 'dies ist eine Testnachricht' gemäß meinem Code hier in slackClient antwortet. js:TypeError: Eigenschaft 'sendMessage' von null kann nicht gelesen werden

'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; 

Dies ist der Fehler erhalte ich:

rtm.sendMessage('this is a test message', message.channel, function messageSent() { 
    ^

TypeError: Cannot read property 'sendMessage' of null 

Wenn ich das richtig verstanden hat, es mir zu sagen, ich nicht rtm einen Wert von null geben kann, aber dann, welche Art von vorformulierten Wert kann ich gib es nur so, dass ich das testen kann?

Antwort

0

Ich fand meinen Fehler, ich hatte rtm als const und dann eine lassen. Also habe ich const aus rtm = new RtmClient (bot_token) entfernt; und ging weiter, um nlp wie folgt hinzuzufügen:

'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; 
Verwandte Themen