2016-09-01 22 views
1

シンプルなスクリプトですが、正しく動作しません。ユーザーの入力によって特定された特定のチャンネルにメッセージを送信しようとしています。HubotでSlackの特定のチャンネルにメッセージを送信できません - SlackRTMError:no channel id

コード:

module.exports = function(robot) { 

    robot.respond(/in (.*) say (.*)/i, function(msg) { 

     var channel = msg.match[1]; 
     var sentence = msg.match[2]; 

     robot.send(channel, sentence); 
    }); 
} 

私はそれを実行すると、hubotは、次のエラーがスローされます。

2016-09-01T18:46:36.836661+00:00 app[web.1]: Unhandled rejection SlackRTMError: no channel id

2016-09-01T18:46:36.836677+00:00 app[web.1]: at RTMClient.handleMessageAck [as _handleMessageAck] (/app/node_modules/hubot-slack/node_modules/@slack/client/lib/clients/rtm/client.js:497:40) 2016-09-01T18:46:36.836679+00:00 app[web.1]: at RTMClient._handleWsMessageViaEventHandler (/app/node_modules/hubot-slack/node_modules/@slack/client/lib/clients/rtm/client.js:460:12) 2016-09-01T18:46:36.836680+00:00 app[web.1]: at RTMClient.handleWsMessage (/app/node_modules/hubot-slack/node_modules/@slack/client/lib/clients/rtm/client.js:420:10) 2016-09-01T18:46:36.836683+00:00 app[web.1]: at WebSocket.emit (events.js:98:17) 2016-09-01T18:46:36.836684+00:00 app[web.1]: at Receiver.ontext (/app/node_modules/hubot-slack/node_modules/@slack/client/node_modules/ws/lib/WebSocket.js:841:10) 2016-09-01T18:46:36.836685+00:00 app[web.1]: at /app/node_modules/hubot-slack/node_modules/@slack/client/node_modules/ws/lib/Receiver.js:536:18 2016-09-01T18:46:36.836686+00:00 app[web.1]: at /app/node_modules/hubot-slack/node_modules/@slack/client/node_modules/ws/lib/Receiver.js:368:7 2016-09-01T18:46:36.836687+00:00 app[web.1]: at /app/node_modules/hubot-slack/node_modules/@slack/client/node_modules/ws/lib/PerMessageDeflate.js:249:5 2016-09-01T18:46:36.836687+00:00 app[web.1]: at afterWrite (_stream_writable.js:278:3) 2016-09-01T18:46:36.836688+00:00 app[web.1]: at onwrite (_stream_writable.js:270:7) 2016-09-01T18:46:36.836689+00:00 app[web.1]: at WritableState.onwrite (_stream_writable.js:97:5) 2016-09-01T18:46:36.836689+00:00 app[web.1]: at afterTransform (_stream_transform.js:99:5) 2016-09-01T18:46:36.836690+00:00 app[web.1]: at TransformState.afterTransform (_stream_transform.js:74:12) 2016-09-01T18:46:36.836691+00:00 app[web.1]: at Zlib.callback (zlib.js:456:5) 2016-09-01T18:46:36.836691+00:00 app[web.1]: 2016-09-01T18:46:36.836681+00:00 app[web.1]: at WebSocket.wrapper (/app/node_modules/hubot-slack/node_modules/@slack/client/node_modules/lodash/lodash.js:4762:19)

任意のアイデアを、それが機能しない理由?私は#generalとgeneralを使って機能をテストしましたが、機能しませんでした。

+1

「チャンネル」は未定義です。あなたが期待したものであることを確認するためにログインしようとしましたか? – dlsso

答えて

1

ありがとうございます!私はmsg.envelope.channelを使ってIDを見つけました。私はそれがどこに格納されているか分からなかったので、私はそれに対処できませんでした。

for(x in msg.envelope) { 
    console.log(x + " : " + msg.envelope[x]); 
} 

これは、msgオブジェクトの変数をチェックする最良の方法でした。プラス私はmsg.messageRoom(roomID、msg)の代わりにrobot.sendを使用して送信しなければなりませんでした。

ありがとうございました

1

Slack APIでは、チャネルは名前ではなく、何らかのIDの形式です。 C024BE91Lのように、最初の文字が "C"のときにチャンネルIDであることをIDで示すことができます。

あなたが必要とするのは、その名前でチャンネルIDを取得することです。これを行うには、すべてのチャネルを取得するためにchannels.listメソッドを使用して、Array#findをその名前で適切なチャネルを見つけるために:

bot.api.channels.list((error, response) => { 
    const targetChannel = response.data.find(channel => channel.name === message.match[1]); 

    robot.send(targetChannel.id, sentence); 
}); 

はちょうどこのAPIをあなたのボットがメッセージを受信するたびに呼び出さないためにそれを最適化し、それうまくいくはずです。

PS類似の問題についての議論も参照できます。in this issue on Github

+0

このスニペットでは、 'bot'は' robot'オブジェクトと同じですか? –

+0

「ロボット」の出所がわからないのと同じように、私は「ロボット」と「ロボット」が同じかどうか確信が持てません。私は公式の文書で広く使われている用語を使っています。しかし、彼らは同じでなければなりません。 –

関連する問題