我们正在实施Google环聊聊天机器人,它将向域中的用户发送主动通知。为此,Google chat Bot API需要使用空格ID才能向用户发送主动通知。
参考文档:https://developers.google.com/hangouts/chat/reference/rest/v1/spaces/list
代码:
jwtClient.authorize(function (err) {
if (err) {
console.log(err);
return;
}
else {
chat.spaces.list({
auth: jwtClient
}, function (err, resp) {
if (err)
console.log(err);
else {
chat.spaces.list({
auth: jwtClient
}, function (err, resp) {
if (err)
console.log(err);
else {
var spaceList = resp.data.spaces;
spaceList.forEach(element => {
var spaceUrl = `https://chat.googleapis.com/v1/${element.name}/messages?key=${apiKey}`;
request({
url: spaceUrl,
method: "POST",
headers: {
'Content-Type': 'application/json'
},
json: customMessage
},
function (error, response, body) {
callback(error, body)
}
);
})
};
});
}
});
}
});
}
}
但是此API仅返回将Bot添加到其掩护范围内的那些用户的空间列表。
他们是否正在努力为Google域中的每个用户获取/为其创建空间?
答案 0 :(得分:2)
不幸的是,如果用户未与机器人互动,则无法提取Space ID。允许这样做将使该漫游器能够在未经许可的情况下向任何用户发送垃圾邮件。
我建议将空间ID存储到数据库中。因此,一旦用户开始与漫游器进行对话,您以后就可以在需要时向他们发送消息。在房间中添加机器人或与其进行交互是机器人向用户发送消息所需的“同意”。