Last active
November 12, 2022 02:51
-
-
Save St3ph-fr/58d757cd7cc3c23c5bf116117e9a2aac to your computer and use it in GitHub Desktop.
Push Google Forms Submission to Hangouts Chat with Apps Script and Webhook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This script will allow you to push form submission in a Hangouts Chat space by using Webhook | |
* | |
* How to setup ? | |
* 1 - Create a Hangouts Chat space | |
* 2 - Add people | |
* 3 - Create Webhook : https://developers.google.com/hangouts/chat/how-tos/webhooks | |
* 4 - Create a form and go to Apps Script editor | |
* 5 - Copy paste code below | |
* 6 - Replace Webhook url int the code for URL_WEBHOOK | |
* 7 - Create a trigger for the function pushToHangoutsChat | |
* | |
*/ | |
function pushToHangoutsChat(e) { | |
var URL_WEBHOOK = "URL you get in the Hangouts chat space"; | |
var form = e.source; | |
var emailRespondent = e.response.getRespondentEmail(); | |
var title =form.getTitle(); | |
var formUrl = form.getSummaryUrl().replace("viewanalytics","edit#responses") | |
var items = e.response.getItemResponses(); | |
var widgets = []; | |
for(var i = 0; i< items.length; i++) { | |
var item = items[i]; | |
widgets.push({"keyValue": { | |
"topLabel": item.getItem().getTitle(), | |
"content": item.getResponse(), | |
"contentMultiline": true} | |
}); | |
} | |
var card = buildCard(title,emailRespondent,widgets,formUrl); | |
var options = { | |
method : "post", | |
contentType : "application/json; charset=UTF-8", | |
payload : JSON.stringify(card) | |
}; | |
UrlFetchApp.fetch(URL_WEBHOOK, options); | |
} | |
function buildCard(title,respondent,widgets,formUrl){ | |
return { | |
"cards": [ | |
{ | |
"header": { | |
"title": title, | |
"subtitle": "par "+respondent, | |
"imageUrl": "https://goo.gl/kv2ENA", | |
"imageStyle": "IMAGE" | |
}, | |
"sections": [ | |
{ | |
"widgets": widgets | |
}, | |
{ | |
"widgets": [ | |
{ | |
"buttons": [ | |
{ | |
"textButton": { | |
"text": "Voir Formulaire", | |
"onClick": { | |
"openLink": { | |
"url": formUrl | |
} | |
} | |
} | |
} | |
] | |
} | |
] | |
} | |
] | |
} | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@agrrajag
Thanks for the recomendations,
You have use the same code above and it's ok?
When I put the code into my script, change the webhooks for mine, that make me a "Erreur TypeError: Cannot read property 'getRespondentEmail' of undefined" :-/
it's not working for me, do you know why?
Thanks in advance