gpt4 book ai didi

google-apps-script - Google Chat bot - 发送没有事件的私有(private)消息

转载 作者:行者123 更新时间:2023-12-04 14:13:14 25 4
gpt4 key购买 nike

我有一个在 PM 中工作的机器人,我可以与它交谈并让它毫无问题地完成我的任务,但我没有找到如何让它向特定的其他人发送消息。
我希望它将私有(private)消息发送给特定的用户列表,而无需与这些用户进行任何交互。唯一的交互是来自我的命令,要求它向其他人发送消息。
我发现了很多关于bot的文档和帖子回复 使用 webhook 发送消息,但没有关于机器人直接向某人发送 PM。
所以而不是这个:

function onMessage(event) {
return {"text": "MSG = " + message };
}
我正在寻找可以指定的东西 用户 ID 用户名 :
function sendMessage(ID/name) {
return {"text": "MSG = " + message, "ID": ID}; //not accurate example
}
sendMessage("User_ID");
如果您有任何想法或信息,将不胜感激!
更新:
目前还不可能与某人发起 DM 对话,但可以通过检查机器人所在的空间向所有机器人联系人发送消息(因此不需要每个人都向机器人发送消息,只有一个消息是触发它所必需的)。
这是我如何使用它的示例:
//Configure the chatbot service
function get_chatbot_service() {
return OAuth2.createService(BOT_NAME)
.setTokenUrl('https://accounts.google.com/o/oauth2/token') // Set the endpoint URL.
.setPrivateKey(PRIVATE_KEY) // Set the private key.
.setIssuer(CLIENT_EMAIL) // Set the issuer.
.setPropertyStore(PropertiesService.getScriptProperties()) // Set the property store where authorized tokens should be persisted.
.setScope('https://www.googleapis.com/auth/chat.bot'); // Set the scope.
}

//Return all the spaces (DM and rooms) the bot belong to
function get_spaces() {
var service = get_chatbot_service();
var url = 'https://chat.googleapis.com/v1/spaces';
var response = UrlFetchApp.fetch(url, { headers: { Authorization: 'Bearer ' + service.getAccessToken() }});
var rep = JSON.parse(response.getContentText());
return (rep.spaces)
}

//Get the informations and send the message to every contacts the bot have been added to
function send_message() {
var service = get_chatbot_service();
var spaces = get_spaces();
var msg = "Test message";

for (var i = 0; i < spaces.length; i++) {
var space = spaces[i];
if (space.type == "DM") { //Check if we are in DM
var url = 'https://chat.googleapis.com/v1/'+ space.name +'/messages'; //Specify the URL with space name
var options = {
method : 'POST',
contentType: 'application/json',
headers: { Authorization: 'Bearer ' + service.getAccessToken() },
payload : JSON.stringify({ text: msg }) //Add your message
}
UrlFetchApp.fetch(url, options); //Send the message
}
}
}

也许有一天这会帮助某人。

最佳答案

目前没有办法让机器人开始对话。
我在 Google's Public Issue Tracker 上发现了这个问题.你只需要去那里点击标题旁边的星号,你就可以获得有关该问题的更新,并提高该问题的可见度。

关于google-apps-script - Google Chat bot - 发送没有事件的私有(private)消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62674583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com