How easy it is to create a Bot using Gupshup Bot Platform

Omkar Mestry
6 min readJun 28, 2021

--

Gupshup Platform:- https://www.gupshup.io/

Today we will learn how to create a basic bot using the Gupshup bot platform. One of the biggest advantages of using the Gupshup bot platform is that you can create a single bot and then publish it to all channels supported by Gupshup and use a single API for using the bot over multiple channels. Gupshup bot platform provides the easiest way to create a bot, plus it also provides a cloud IDE for coding your bot or you can also code your bot locally. So let’s get started.

First, you need to create an account on the Gupshup Platform.

After creating your account you will be able to see an API KEY when you hover over your profile icon, just copy it we would require this API KEY later.

You will be able to see an API KEY instead of the grey box present below the text “API Key”

We will create the bot using the local coding approach, I like this approach because it allows you to debug the code easily but if you want to use the cloud IDE provided by Gupshup Platform you can follow this Guide, for this the prerequisite is node js to be installed, visit Node.js website to download and follow the instructions provided to install it. Once Node.js is installed follow the following steps to create a local setup to create a bot.

sudo npm install -g yo
sudo npm install -g generator-gupshup-bot

Great!!! we are now ready to create our bot, go to the directory where you would like to save code for the bot and then run the following command to start the creation of the bot.

yo gupshup-bot

Once you run this command, you will be asked some questions for the creation of the bot, you will be also asked to provide API Key, refer to the following image to answer the questions.

  • Note:- Name of the bot should not be less than 6 characters and no special characters are allowed.
Reference image to understand the process of creation of a bot

Once completed with the process, you would have successfully created a bot on the Gupshup Bot Platform. To verify whether the bot is created or not you can visit Bot Platform Dashboard.

Reference image of Dashboardard

Now we have created a bot but what should we do with it, let check whether this bot is working or not, to start the bot, run the following command in the directory where you have created the bot.

npm start

Once you run this command the bot will start running on the local system, following is an example.

Example of running a bot

To test this bot we will be using a proxy bot that will allow us to interact with our bot, visit the Gupshup Proxy Bot, and click on the bot icon on the bottom right corner, to open the chat window of the proxy bot.

Once the chat window opens type in the command highlighted in the above image which basically allows us to proxy to our bot using a proxy bot. Once you run this command now you will be interacting with your bot, refer to the following image.

Example for proxying bot

Yeahh!!! this is the beauty of the Gupshup Bot Platform as it allows you to test your bot before even deploying it and publishing it to any channel. Now we have successfully created a bot and we are able to run it. You can also test your bot on a specific channel before publishing it to that channel using a proxy bot created by Gupshup for example, if you want to test your bot on the Telegram channel, search gupshupproxybot in Telegram which is the proxy bot created by Gupshup in Telegram for testing.

Now, what should we do next, as it is a bot we should be able to code the bot to respond to the user based on the user input right?

Let’s do so changes to our bot, let’s make our bot respond as ‘Hello’ when the user types in ‘Hi’ or ‘Hello’ and for any else input we will respond to the user as ‘I don’t know what to respond to this’.

To make these changes you need to open the index.js file in the root of the directory where you have to create the bot and update the code in the MessageHandler function to the following code:-

function MessageHandler(context, event) {if(event.message === 'Hi' || event.message === 'Hello')return context.sendResponse("Hello");elsereturn context.sendResponse("I don't know what to respond to this");}

Then restart the bot so that the new change can be reflected, so now we can test these changes using the same proxy bot, open the proxy bot and run the proxy command which we used earlier from the logs which we get when we start the bot to proxy to our bot.

Then you can test the bot by typing ‘Hi’ or ‘Hello’ and the bot will respond ‘Hello’ and if we type anything else like for example ‘Bye’ bot will respond with ‘I don’t know what to respond to this’.

Example of how the bot will interact.

So now we have created a bot as we need, let's publish it to a channel, one of the easiest among all the channels to publish is Telegram. But before publishing let us test our bot using the Gupshup proxy bot in Telegram.

To do this search gupshupproxybot and follow the same process followed using previously.

Example of how to test bot using Telegram proxy bot

Now let us publish our bot. To publish it on Telegram you just need to follow the following process:-

  • First, we need to deploy our bot, to deploy the bot run the following command from the directory where you have created your bot. This allows us to finalize the bot and then deploy it when we ready to put the bot on production.
npm run deploy
  • Once the bot is deployed, you need to register on Telegram if not registered.
  • Then in Telegram, you need to search for BotFather, this is basically a bot that allows you to create a bot in Telegram which can be used to publish the bot you have created on Gupshup in the Telegram channel.
You will get a token instead of the grey area similar to the above image
  • Once you create a new bot in Telegram using BotFather you will get a token as seen in the above image, copy the token.
  • Click on the Channels in the Publish Tab of the bot in the Gupshup Bot Platform Dashboard, you will see multiple channels list, from this list click on the publish button of Telegram which will open up a page that will show the steps to publish bot on Telegram, open the last section and then past it in token copied earlier and enter the username of the bot provided while creating the Telegram bot and then click submit.

Vola!!! your bot is now published on Telegram, you can now try your bot in Telegram by searching the username of the bot, the following is an example of the bot we just now created.

Example bot published on Telegram

I hope you might have enjoyed reading this post. Keep exploring the Gupshup Bot Platform and if you have any query feel free to reach out to me at omkar.mestry@gupshup.io or you can also share your query on devsupport@gupshup.io

Gupshup:- https://www.gupshup.io/

In the next upcoming post, I will be showing how we can persist data for users at the Room level and at the Bot level so stay tuned!!!

Enjoy and Happy Coding !!!

--

--

No responses yet