懒散的api rtm直接消息

时间:2015-07-13 22:42:25

标签: websocket slack-api

我正在使用一个节点包:slack-client在松弛时与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);
}); 

我希望这条消息可以直接从机器人那里直接传达给我,但没有。我收到你好类型的回复吗?上面的发送详细信息我得到了另一篇关于此的帖子,但它对我不起作用。消息Id是我创建的消息。

1 个答案:

答案 0 :(得分:0)

好的,所以当通过web api调用rtm.start时,你会得到一个可以为各种用户开放的DM列表,否则你可以轻松地用im.open打开一个im。我正在使用我的问题中提到的节点包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发送方法让它工作但是我想postMessage的api现在可以用,因为你可以用postMessage发布格式丰富的消息。希望这有助于某人

相关问题