2017-05-15 20 views
0

私は新しいSlackbotを、ここにslackClientの私のコードに従って「これはテストメッセージです」と返答する期待される振る舞いで挨拶を送ってテストしようとしています。 JS:TypeError:ヌルのプロパティ '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 

私はこれを正しく理解していれば、それは私がRTMにヌルの値を与えることができない私に言っていたが、その後定型値のどのような私ができます私はこれをテストすることができますようにそれを与える?

答えて

0

私のエラーが見つかりました。私はconstとしてrtmを持っていました。そこで、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;