【发布时间】:2017-10-14 19:55:45
【问题描述】:
我正在尝试根据我在 slackClient.js 中的代码向我的新 Slackbot 发送带有预期行为的问候语来测试我的新 Slackbot:
'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 一个 null 值,但是我可以给它什么样的样板值以便我可以测试呢?
【问题讨论】:
标签: node.js bots slack-api slack