How to add role to user after private message with Hubot with password

I would like to add a new role for the user when he can give the correct Hubot password.

Here is the script I have:

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 correctly with @bot PasswOrd 1234

But I do not know how to add a role.
Thanks.

+5
source share
1 answer

Seeing no other way, I was able to "solve" this using the REST API. It is a bad idea?

I simplified my actual code to answer the original question.

 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) + ' }}') 
0
source

Source: https://habr.com/ru/post/1275126/


All Articles