GMail To GChat Integration

Author: Shivani Umale

We will be using apps script and webhook for integration between gmail and Google chat room. Apps script will fetch the newly received emails from gmail and the webhook of Google chat room will post the data in Google chat room using webhook.

Step 1: Configure Google chat room.

Here we will create a webhook for Google chat room

1. Open Google chat. 

2. Next to “Rooms,” click Create or find a room.

3. Enter a room name. 

4. From the drop down menu of the new create room, select Manage webhooks.

5. Enter a name for your webhook and click Save.

6. You will get a Google chat room webhook url .

7. The above webhook url will be used in our apps script.

8. Copy the webhook url.

Step 2: Set Up Your Gmail

1. Sign in to your Google Account.

2. From the left side panel select  More -> All Mail.

3. At the top of your email messages list, click the main Select button to choose all the messages that are currently displayed.

4. Then click on Select all ** conversations in All Mail.(You will have different count)

5. Select the Label option.

6. Select Create new .

7. Enter the New Label name as notified and hit the Create button.

8. Now all mails will have a label notified.

9. To open the script editor, go to script.google.com . Make sure you are signed to the correct Google Account.

10. Click on the New project button. Rename the project name. (In the example below the project name is renamed to Gmail to google chat)

11. Remove the default code and copy paste the below code under Code.gs

 function getNewMails(){

   var url=”PASTE YOUR Google CHAT ROOM Webhook URL

   var threads = GmailApp.search(“-in:notified”,0,100);

   var label =GmailApp.createLabel(“notified”)

   for (var t in threads) {

  threads[t].addLabel(label);

         }

   for(var i=0; i<threads.length; i++)

     {

       if(threads[i].isInInbox())

       {

          var messages = threads[i].getMessages();

          for(var j=0; j<messages.length; j++)

          {

             var vars=messages[j].getSubject();

             var sender1=messages[j].getFrom();

             var dat=messages[j].getDate();

             var id=threads[i].getId();

              var link=”https://mail.Google.com/mail/u/0/#inbox/”+id

              var required=”Sender: “+sender1+ 

                             ” \n Subject “+vars+ “\n Sent date “+dat+”\n”+link

               var payload = {

                      “text”: required}

                var options = {

                  “method”: “post”,

                  “contentType”: “application/json”,

                  “payload”: JSON.stringify(payload)};

                UrlFetchApp.fetch(url,options)}

            }}}

12. Click on Save Project to save the apps script. 

13. Now run the apps script.

14. When you run your apps script for the first time, it will ask for permissions as shown below. Click on Review permissions.

15. Next, it will ask you to sign in. Select your Gmail Account. 

16. Select the Allow button for the app script to access your google account.

17. You will get the below output.

18. Now let’s add a trigger for the function getNewMails(). Click on the trigger icon.

19. Select the Add Trigger button from the bottom right corner.

20. Configure the trigger according to your requirement and click Save.

21. The trigger will be added in project for function getNewMails()

22. Now this script will check for new emails.

23. Go to Google Chats.

24. When a new email is received, Google chat room will get notifications as shown below.

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.