Skip to content

Instantly share code, notes, and snippets.

@St3ph-fr
Last active November 12, 2022 02:51
Show Gist options
  • Save St3ph-fr/58d757cd7cc3c23c5bf116117e9a2aac to your computer and use it in GitHub Desktop.
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 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
}
}
}
}
]
}
]
}
]
}
]
}
}
@agrrajag
Copy link

@St3ph-fr et all
One thing that has been nice from my end is having an additional button (copying lines 58-67) that goes to the Google Sheet responses so we can see the full form response log. I am not sure how to get the sheet information through API, but have been able to just add the direct URL of the sheet.

@Virgule123
Copy link

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment