我使用了一个node包: slack-client在slack上与api进行交互。现在,无论是否使用slack client,我如何从我的机器人向我想要指定的用户发送直接消息?以下是目前为止使用普通套接字连接的情况:
var WebSocket = require('ws')
,ws2 = new WebSocket(myURL); //from rtm start
ws2.on('open', function() {
ws2.send({
"id": 333,
"type": "message",
"channel": "@user1", //User I want to send to
"text": "HEY!!!!"
});
});
ws2.on('message', function(message) {
console.log('received: %s', message);
}); 我希望这条消息会直接从机器人传给我,但什么也没有。我得到了一个类型为hello的回复?上面的发送细节我在另一篇关于这个的帖子上得到的,但它对我不起作用。消息Id是我创建的。
发布于 2015-07-16 20:32:26
好的,当通过web api调用rtm.start时,你会得到一个DM的列表,这个列表将对不同的用户开放,否则你可以很容易地用im.open打开一个im。我使用了问题中提到的node包slack-client,所以您可以这样做:
//name of user your bot wants to send a msg to.
var userTest = slack.getUserByName('user1');
slack.openDM(userTest.id, function(res)
{
TestMsg(res.channel.id, 'some other msg');//test function I'm using
});接下来是TestMsg函数:
function TestMsg(userChannelId, msg)
{
request.post({url: 'https://slack.com/api/chat.postMessage',
form: { token: "xxxx-yourbot-token",channel: userChannelId,text: msg ,username: "yourBotNamehere", as_user: false}
}, function(error, response, body){
console.log(response.body);
});
}我还不能使用websockets send方法让它工作,但我想现在可以使用postMessage的api,因为您可以使用postMessage发布格式丰富的消息。希望这能帮助到某人
https://stackoverflow.com/questions/31394724
复制相似问题