2017-05-15 4 views
0

slackbot에서 slackClient의 코드에 따라 '이것이 테스트 메시지입니다'라고 응답하는 예상 동작으로 인사말을 보내서 새 Slackbot을 테스트하려고합니다. JS :이 오류가TypeError : null의 'sendMessage'속성을 읽을 수 없습니다.

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

내가 얻을 :

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

TypeError: Cannot read property 'sendMessage' of null 

I을 올바르게 이해한다면, 그 다음 내가 RTM에게 null 값을 줄 수 말해,하지만 것 상용구 값의 어떤 종류의 내가 할 수있는 이걸 시험해 볼 수있게 해줘 요?

답변

0

나는 내 오류를 발견했다. 나는 rtm을 const로 사용했다. 그래서 rtm = new RtmClient (bot_token)에서 const를 삭제했습니다. 다음과 같이 nlp를 추가했습니다 :

'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; 
관련 문제