Websocket slack api rtm直接消息

Websocket slack api rtm直接消息,websocket,slack-api,Websocket,Slack Api,我正在使用一个节点包:slackclient与slack的api交互。现在,无论是否使用slack client,我如何从我的机器人向我想要指定的用户发送直接消息?以下是到目前为止普通套接字连接的主要功能: var WebSocket = require('ws') ,ws2 = new WebSocket(myURL); //from rtm start ws2.on('open', function() { ws2.send({ "id": 333, "type":

我正在使用一个节点包:slackclient与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是我创建的。

好的,因此当通过web api调用rtm.start时,您将获得一个DM列表,该列表将为各种用户打开,否则您可以轻松地使用im.open打开im。我正在使用问题中提到的节点包slack客户端,因此您可以执行以下操作:

     //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发布格式丰富的消息。希望这对其他人有所帮助

好的,因此当通过web api调用rtm.start时,您将获得一个DM列表,该列表将对各种用户开放,否则您可以轻松地使用im.open打开im。我正在使用问题中提到的节点包slack客户端,因此您可以执行以下操作:

     //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发布格式丰富的消息。希望这对某人有所帮助

我收到了slack support的一封电子邮件,该电子邮件应能提供解决方案。如果可以的话,我将在今晚晚些时候添加一个答案。我收到slack支持部门的一封电子邮件,该邮件将提供解决方案。如果有效,我将在今晚晚些时候添加答案