Wis*_*isp 5 javascript google-apps-script google-forms hangouts-chat
我正在尝试制作一个 Google Hangouts Chat Bot,它可以检测何时填写表单,并使用机器人将最近提交的表单的响应发送到 Hangouts Chat。我已经基于现有代码构建了它(我的 JS / GAS 知识接近于零),主要基于 GitHub TSFormBot 存储库。问题是,它将每个响应作为不同的消息单独发送,而不是包含所有内容的 1 条消息。
例如,一个 4 个问题的表单会导致机器人发送 4 个单独的回复,每个回复中都有一个不同的答案。你能帮我看看我哪里出错了,这样我就可以在一个回复中获得所有 4 个答案的内容吗?
谢谢!
当前代码:
function postToRoom(e) {
var formResponses = FormApp.getActiveForm().getResponses();
var formResponse = formResponses[formResponses.length-1];
var itemResponses = formResponse.getItemResponses();
for (var j = 0; j < itemResponses.length; j++) {
var itemResponse = itemResponses[j];
var options, options, url;
url = PropertiesService.getScriptProperties().getProperty('WEBHOOK_URL');
if (url) {
try {
payload = {
"cards": [
{
"header": {
"title": "There is a new request!",
"imageUrl": "https://images.emojiterra.com/google/android-10/128px/1f916.png",
"imageStyle": "IMAGE",
},
"sections": [
{
"widgets": [
{
"textParagraph": {
"text": '<b>'+ (
itemResponse.getItem().getTitle() + ' ' + itemResponse.getResponse())',
}
}
]
},
{
"widgets": [
{
"buttons": [
{
"textButton": {
"text": "GO TO RESPONSE",
"onClick": {
"openLink": {
"url": e.response.getEditResponseUrl()
}
}
}
},
{
"textButton": {
"text": "GO TO FORM",
"onClick": {
"openLink": {
"url": FormApp.getActiveForm().getEditUrl()
}
}
}
}
]
}
]
}
]
}
]
}
options = {
'method' : 'post',
'contentType': 'application/json; charset=UTF-8',
'payload' : JSON.stringify(payload)
};
UrlFetchApp.fetch(url, options);
} catch(err) {
Logger.log('FormBot: Error processing Bot. ' + err.message);
}
} else {
Logger.log('FormBot: No Webhook URL specified for Bot');
}
}
Run Code Online (Sandbox Code Playgroud)
您遇到的问题是因为在for循环内,您每次使用有效负载时都会发送UrlFetchApp.fetch(url, options);,因此您需要以这种方式执行此操作:
// Previous stuff
for (var j = 0; j < itemResponses.length; j++) {
// Do something
}
// Do more stuff
UrlFetchApp.fetch(url, options);
Run Code Online (Sandbox Code Playgroud)
知道。我在代码中更改的第一件事是创建一个有效负载对象,该对象将拥有一个sections稍后将填充的属性。
var payload = {
"cards": [{
"header": {
"title": "TSFormBot",
"subtitle": "Form Notifications Bot",
"imageUrl": "https://raw.githubusercontent.com/techstreams/TSFormBot/master/notifications.png",
"imageStyle": "IMAGE"
},
"sections": []
}
]
};
Run Code Online (Sandbox Code Playgroud)
有了有效负载对象,我创建了一个用于填充属性的函数sections,在那里您将有for循环,在用所有响应填充对象后,我添加了两个按钮:
/*
* Build Payload
*
* @param {Object} payload
* @param {ItemResponse[]} itemResponses
* @param {FormResponse} formResponse
*/
function populateCard(payload, itemResponses, formResponse){
for (var j = 0; j < itemResponses.length; j++) {
var itemResponse = itemResponses[j];
payload["cards"][0]["sections"].push({
"widgets": [{
"textParagraph": {
"text": itemResponse.getItem().getTitle() + ' ' + itemResponse.getResponse()
}
}
]
});
}
payload["cards"][0]["sections"].push({
"widgets": [
{
"buttons": [
{
"textButton": {
"text": "GO TO RESPONSE",
"onClick": {
"openLink": {
"url": formResponse.getEditResponseUrl()
}
}
}
},
{
"textButton": {
"text": "GO TO FORM",
"onClick": {
"openLink": {
"url": FormApp.getActiveForm().getEditUrl()
}
}
}
}
]
}
]
}
);
return payload;
}
Run Code Online (Sandbox Code Playgroud)
之后,您将能够使用 发送请求UrlFetchApp.fetch(url, options);。你的postToRoom(e)函数看起来像这样:
/*
* Process Form Submission
*
* @param {Object} e - form submit event object
*/
function postToRoom(e) {
var formResponses = FormApp.getActiveForm().getResponses();
var formResponse = formResponses[formResponses.length-1];
var itemResponses = formResponse.getItemResponses();
formResponse.getEditResponseUrl()
var options, options, url;
url = PropertiesService.getScriptProperties().getProperty('WEBHOOK_URL');
if (url) {
try {
var payload = {
"cards": [{
"header": {
"title": "TSFormBot",
"subtitle": "Form Notifications Bot",
"imageUrl": "https://raw.githubusercontent.com/techstreams/TSFormBot/master/notifications.png",
"imageStyle": "IMAGE"
},
"sections": []
}
]
};
// Call this function to populate the card with the responses
var PopulatedPayload = populateCard(payload, itemResponses, formResponse);
options = {
'method' : 'post',
'contentType': 'application/json; charset=UTF-8',
'payload' : JSON.stringify(payload)
};
UrlFetchApp.fetch(url, options);
} catch(err) {
Logger.log('TSFormBot: Error processing Bot. ' + err.message);
}
} else {
Logger.log('TSFormBot: No Webhook URL specified for Bot');
}
}
Run Code Online (Sandbox Code Playgroud)
我使用这些文档来帮助您:
| 归档时间: |
|
| 查看次数: |
839 次 |
| 最近记录: |