hubot - 如何在使用密码向 Hubot 私信后向用户添加角色

标签 hubot rocket.chat

我想为用户添加一个新角色,当他能够为 Hubot 提供正确的密码时。

这是我现在拥有的脚本:

module.exports = (robot) ->
  robot.respond /PasswOrd (.*)/, (res) ->
    paSS = res.match[1]
    if paSS is "1234"
      role = 'h1'
      user = robot.brain.userForName(res.message.user.name)
      #CODE TO ADD ROLE h1 FOR THIS USER ???
      res.reply "Correct." + user.id
    else
      res.reply "Incorrect password."

它使用 @bot PasswOrd 1234 正确启动

但我不知道如何添加角色。
谢谢。

最佳答案

没有其他办法,我能够使用 REST API “解决”这个问题。这是个坏主意吗?

我已经简化了我的实际代码来回答原始问题。

user = robot.brain.userForName(res.message.user.name)
robot.http("https://xxx.xxx.xxx/api/v1/users.info?userId=" + user['id'])
    .headers("X-Auth-Token":"xxxxxxxxxxxxxxx", "X-User-Id":"xxxxxxx")
    .get() (err, response, body) ->
        Info = JSON.parse(body)
        Info.user.roles.push("newRole")
        robot.http("https://xxx.xxx.xxx/api/v1/users.update")
            .headers("X-Auth-Token":"xxxxxxxxxxxxxxx", "X-User-Id":"xxxxxxx", "Content-type":"application/json")
            .post('{"userId": "' + user['id'] + '", "data": { "roles": ' + JSON.stringify(Info.user.roles) + ' }}')

关于hubot - 如何在使用密码向 Hubot 私信后向用户添加角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48582477/

相关文章:

node.js - 无法在 Heroku 上部署 Hubot

rocket.chat - (Rocket.Chat) 身份验证 token 永不过期

docker - Rocketchat 安装向导挂起

Rocket.Chat - 如何获取 roomId

testing - 如何在测试脚本中检查 Hubot 日志输出

regex - Hubot 不响应正则表达式

node.js - 通过 hubot-slack 启用 unfurl_links 选项

hubot - 从另一个调用一个命令

linux - Vagrant.configure 的语法错误 ("2") do |config|, while trying to run a script file

docker-compose - 管理员用户在火箭聊天中失败