【问题标题】:slack api rtm direct messageslack api rtm 直接消息
【发布时间】:2015-10-02 09:18:20
【问题描述】:

我正在使用节点包: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);
}); 

我希望该消息会直接从机器人发送给我,但什么也没有。我得到一个类型的回复你好虽然?上面的发送详细信息我在另一篇关于此的帖子中找到,但它对我不起作用。消息 ID 是我创建的。

【问题讨论】:

  • 我收到了一封来自 slack 支持的电子邮件,它应该提供解决方案。如果可行,将在今晚晚些时候添加答案

标签: websocket slack-api


【解决方案1】:

好的,所以当通过 web api 调用 rtm.start 时,您将获得一个对各种用户开放的 DM 列表,否则您可以轻松地使用 im.open 打开一个即时消息。我正在使用我的问题中提到的节点包 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 发布格式丰富的消息。希望这可以帮助某人

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 2017-02-27
    • 1970-01-01
    • 2017-08-13
    相关资源
    最近更新 更多