私は、フォームが記入されたことを検出するとGoogleのハングアウトチャットボットをしようとし、ボットを使用してハングアウトチャットへの最新のフォーム送信のレスポンスを送信しています。私は主にGitHubのに基づいて、(私のJS / GASの知識はゼロに近い)、既存のコードをオフにこれを構築しているTSFormBotのレポ。問題は、それがすべてのコンテンツと1つのメッセージの代わりに、異なるメッセージとしてinvidiually各応答を送信している、です。
exmapleのために、4の質問形式は、それぞれに異なった答えの一つで、4つの個々の応答を送信するボットが発生します。私は1つの応答ですべての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');
}
}
問題は、あなたの内側に、からだと持っているfor
ループ、あなたはあなたのペイロードが使用するたびに送信されUrlFetchApp.fetch(url, options);
、したがって、あなたはこの方法でそれを実行する必要があります。
// Previous stuff
for (var j = 0; j < itemResponses.length; j++) {
// Do something
}
// Do more stuff
UrlFetchApp.fetch(url, options);
知っています。私はあなたのコードに変更された最初の事は、それが作成することでしたペイロード持ってしまうオブジェクト、sections
後に移入されます属性を。
var payload = {
"cards": [{
"header": {
"title": "TSFormBot",
"subtitle": "Form Notifications Bot",
"imageUrl": "https://raw.githubusercontent.com/techstreams/TSFormBot/master/notifications.png",
"imageStyle": "IMAGE"
},
"sections": []
}
]
};
持つペイロードのオブジェクトを、私は移入するための機能を作成しsections
、あなたがそこにあります、属性をfor
ループし、すべての応答を持つオブジェクトを充填した後、私は2つのボタンを追加しました:
/*
* 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;
}
すべてのことの後、あなたが使用して要求を送信することができます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');
}
}
カードレスポンス
ドキュメント
私はあなたを助けるためにこれらのドキュメントを使用しました: