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
}
}
}
}
]
}
]
}
]
}
]
}
}
@St3ph-fr
Copy link
Author

Thank you I added the multiline interesting to have it by defaut.

Stéphane

@jounivarjonen
Copy link

Could this change to work with Google spreadsheet also? Tried to look at how to change it but didn't get it yet. :)

@St3ph-fr
Copy link
Author

St3ph-fr commented Feb 1, 2022

Hi

If you want to dothat when someone edit a sheet for exemple.
Logic is almost the same but you have to use a onEdit() trigger.

Some similarity but a bit different.

Stéphane

@Virgule123
Copy link

Hi Stéphane,

I juste want the respons of a Form to a chat message of o chat room.
I have a track, but i'm a litle confused.

Can You help me on this subject?
`function myFunction() {

var WebWhooklink = "mon webhooks de google hangout"

var message = { text: "Hello Chat"};

var payload = JSON.stringify(message);
var options = {
        method: 'POST',
        contentType: 'application/json',
        payload: payload
};

var response =  UrlFetchApp.fetch(WebWhooklink, options ).getContentText();`

but the problème with this code, is just sending a fucking "Hello chat" to the chat, not the response....
i have try to add modification but no issues...

Can you Help me? Please.

Thanks in advance

@agrrajag
Copy link

@Virgule123
That looks pretty significantly different from the code above, @St3ph-fr 's code is pretty rock solid in regards to getting it from the form into the chat. I've been using it on a few internal projects and loving how it works. I'm relatively new to this... but from the API docs, it looked like you had to use the card function to get the different values from the form.

@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