Write Your First G-Chat Bot

Have you ever been in a situation where you need to send repeated reminders or messages in your groups, such as reminding someone to fill their report by the end of day, or to submit their work report by end of the day or maybe anything else.

Over the result of that you might want to automate your daily messages or reminder in your groups, doing these manually can be hard and tedious.

You might also have already seen some kind of bot which gives you reminder daily or weekly or at any specific time, but you still don’t know how to create your own one, if yes this is for you, in this article we’ll learning about how we can create our own google bot to do these things and how we can add trigger so that they trigger at a specific given time or range.

Creating Your Own Bot

Step 1:

In the first step we’ll need a chat room either a new one or existing one, here I’m creating a new one. You can create a new one by clicking on create room.

create a new room

Step 2:

Once we have a chat room the next thing that we require is a webhook url using which we’ll be sending messages to this group, and in-order to get that click on the group name and below you can see the Manage webhooks option.

Click on manage webhook option

Click on the option you’ll be then seeing your already existing webooks if you have any and also an option to add a new one.

After clicking on add another you’ll have to give a webhook name and avatar icon if you want. Once you provide these informations you’ll be provided a webhook url in, and you can see it in incoming webhooks.

Simply copy the webhook url to use it further, keep it with you, you’ll need it to create the bot.

Step 3:

We’ll need to go to App Script console to write the code, to ease this process let’s just create a new sheet and once you open that sheet in the option on top you can see the option for extension in that you’ll see an option for App Script click on that to open App Script console.

google sheet
App Script Console

Step 4:

Now you’ve reached to a point where you can see the App Script console, they use something called Google Script (.gs) to write the code which is almost JavaScript.

Let’s put some stuff here to make the bot.

Final code

It did have an empty function “myFunction” which it executes by default when we save and run it.

Replace webhook url with your actual webhook url.

In order to send it to the google chat room we need to make a post call to the webhook url and to do that we have created two functions “postBotMessage” and “postOptions

As you can see “postOption” takes payload and generate the option for the request and on the other hand “postBotMessage” uses that function to generate post options and to make the actual call to webhook.

Once we write this amount of code we’ll need to save and run the code in order to see the effect of it.

On the top you see an icon to save and run, first save and than run, you must see the magic.

message from bot

It did sent a message to the group, what you specified in the call.

We’re done ✅ .

Till here we’ve created generated a webhook and using that webhook we’ll also created a bot which sends a message to the group. How cool is that?

Step 5:

Now let’s make it even cooler by adding a trigger to run it automatically at a given time.

see the executions option

On the left side you can see option for Executions click on that and you’ll see all your executions.

Triggers

As we’re new and we don’t have any such specified it’s empty. But we can create a new one by clicking Add Trigger.

Once you click on Add Trigger you’ll see options that you can add for creating your trigger there are many. Below you can see I’ve created a daily trigger which runs between 9–10 AM, and thats it, we can save it

Tigger Options
List of triggers

Now it will run your selected functions daily once between 9–10AM.
Yes, surely you can customise it further to run only on weekdays or weekends or a given day at a given time, that makes it even cooler and rest is your creativity.

I personally love creating these bots for random purposes, might be for your daily IPL match reminders? Stock Market reminder or update reminder you can add any because at the end it’s code and it will do whatever you’ll ask it to do, its just the matter of how you’ll ask.

I’ll leave the remaining part up for you, please do go ahead and create your own bots 🤖 , any feedbacks or loopholes if you found in the article please feel free to write in notes or comments I’ll be more than happy to take that, Thank you for reading :)

Application Developer at ThoughtWorks

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store