Chatbots represent a new trend in how people access information, make decisions, and communicate. -Christie Pitts - Co-Founder of Backstage Studio
In the following lessons, you'll learn how to create a Telegram chatbot that utilizes IBM's Watson Assistant and Node-RED, that you've already set up during earlier lessons.
Telegram is a cross-platform, freeware instant messaging app. It supports bots, third-party accounts that are run by programs. There's even a bot for making new bots, BotFather, which we'll use for creating our own bot.
Watson Assistant is a platform created by IBM. It's a conversation AI platform that's often used in chatbots utilized by businesses for customer support. We'll use it to create a simple chatbot inside Telegram, that can respond to simple greetings and farewells.
Smart homes and other connected products won’t just be aimed at home life. They’ll also have a major impact on business. And just like any company that blissfully ignored the Internet at the turn of the century, the ones that dismiss the Internet of Things risk getting left behind. -Jared Newman, writer for PCWorld, TechHive and Fast Company
To get started creating the bot, first, we'll need a Telegram account.
Creating an Account
Go to telegram.org and create an account using your phone number. After receiving your verification code and verifying the account, you can start with creating a new bot.
Search for the BotFather on the top-left search bar. Select BotFather and press Start on the chat window. This shows you the basic commands.
Create a New Bot
Create a new bot using the /newbot command, then choose a name for your bot. After that, choose an username for your bot that end in"bot". Now a new bot has been created and BotFather gives you a token for your bot.
Great! Now you have your first Telegram bot. It doesn't do much as of yet, but we'll get there soon.
Cloud computing is the third wave of the digital revolution. – Lowell McAdam, CEO of Verizon . -Lowell McAdam, CEO of Verizon
Now we'll need the Watson Assistant for our bot to function, so let's continue straight away!
Start by going to your IBM Cloud Dashboard and open the Catalogue from the top-right corner.
Search for the "Watson Assistant" in the search bar. Make sure that the region setting is correctly set and rename the service. Then press Create. Now the Watson Assistant is visible in your Resource list.
Launch Watson Assistant by pressing the Launch -button. When launching it for the first time, it may ask for your preferences. You can fill them in or skip them.
On the left side menu, select Skills and press Create skill. Then select the skill type; we'll use Dialog skill. Press Next.
Now let's name the skill. Call it something like ChatBot and add a description. Then click Create dialog skill.
Intents - Greetings
Let's create Intent by pressing the Create intent -button. Name the intent Greetings. Write down some examples of greetings that a user may use, such as "hello", "hi", or "nice to meet you".
Intents - Farewells
Go back via the arrow in the top left, and create a new Intent. This time name it Farewells, and add some appropriate examples. Once you're ready, go back by pressing the arrow on the top left.
Dialog - Greetings
Open the Dialog -tab on the right and create a new node by pressing the Add node -button. Name it Greetings and select the Greetings -Intent we previously made. Write a response that you want the bot to give.
Dialog - Farewells
Nor redo the previous steps, but this time create a node called Farewells and set up an appropriate response.
Now we're done setting up Watson Assistant, great! Let's head over to Node-RED to finish our work.
With the IoT, we’re headed to a world where things aren’t liable to break catastrophically – or at least we’ll have a hell of a heads’ up. We’re headed to a world where our doors unlock when they sense us nearby. -Scott Weiss, venture capitalist at Andreessen Horowitz
Now it's time to connect Watson Assistant with the Telegram bot we made. We'll do this through Node-RED.
Creating a Node-RED connection
Now we need to connect our Watson Assistant to Node-RED. To get started, go back to the Resource list and open the Watson Assistant.
Creating a Connection
On the left side of the menu, select Connections. Now you can see the Create connection -button on the top right corner. Click it and choose the Node-RED we've used in the previous lessons and press Create.
Now the app should be connecting. A pop-up should appear informing that Node-RED needs to be restaged. Press the Restage -button. If you have Node-RED open, you can close it now as the restaging is happening.
Now you can go back to Node-RED through the app URL. You can find it by first going to the Resource list and selecting Node-RED from under the Cloud Foundry Apps. Then, click the Visit App URL -link on the top of the page. To see the flow editor, press the Go to your Node-RED flow editor -button.
New Flow and Telegram Nodes
Double click the bar on the top right to create a new flow. Then, open the menu on the top right and select Manage palets. Search for the Telegram nodes and install them.
Now drag the Telegram command -node to the flow. Double click the node to open it and write /echo in the command -field.
A New Bot
Click the pen -icon next to the bot -field. Write the bot's name you previously created into the Bot-Name -field and enter the token you got from BotFather into the appropriate slot. We still need the chat ID.
The Chat ID
Go back to Telegram and search for @getidbot. With the /start -command, the bot will give you the chat ID. Copy and paste it back into the Chatids -field in Node-RED. Then click Add and Done on the right corner.
The Sender Node
Now drag a Sender -node from the left to the flow. Add the same bot to it you just added to the previous node. All the other information will be automatically copied from the previous node. Now, click the Deploy -button on the top right corner. Now your bot should be active.
Testing the Bot
Now, go on Telegram and search for your bot. With the /echo -command, you should now get an echo and the bot should repeat whatever you write after the /echo in the message.
Connecting Watson Assistant
Now to get Watson Assistant working, you'll need to add more nodes to the flow. Add two function -nodes and the assistant -node between the previously added two nodes. From the command -node, change the command to /watson.
The First Function Node
Open the first function -node and rename it as "Prepare for Conversation". Then write down the following code.
The Second Function Node
Rename the second function node as "Prepare for Telegram" and write down the following code.
Connecting the Nodes
Make sure that the nodes are connected like so.
Getting the Skill ID - Part 1
Now you need the skill ID for the assistant -node. Let's go back to Watson Assistant's Skill -menu. From the ChatBot, press View API details.
Getting the Skill ID - Part 2
Now copy the workspace ID from the URL, it's the number between workspaces/ and /message. Paste the workspace ID in the assistan -node in the Workspace ID -field. Then, click Deploy.
Test the Bot
Now, go to Telegram and test your bot out.
Congratulations! Now you've successfully created your first Telegram bot using Node-RED and the Watson Assistant. That's no easy task, be proud of yourself!