Archived content

Archived date: 2019-05-21

This content is no longer being updated or maintained. The content is provided “as is.” Given the rapid evolution of technology, some content, steps, or illustrations may have changed.

There are many services and open source projects for developing chatbots, IBM’s Watson Assistant service is one of the best environments for implementing one. Some of the features that make Assistant service great are the ease of creation and integration. Let’s develop a chatbot in under 30 minutes using only IBM Cloud services and Telegram as the user interface.

Learning objectives

After completing this How-to you will understand how to:

  • Create a Node-RED application
  • Create a basic chat dialog using Watson Assistant service
  • Integrate Telegram as an interface for Watson Assistant service using Node-RED

Prerequisites

In order to complete this how-to, you will need the following prerequisites:

Estimated time

  • Creating the dialog on Assistant service should take less then 10 minutes
  • Developing the complete application on Node-RED should take a maximum 25 minutes.
  • Overall the total time for completing of How-to should take approximately 35 minutes.

Steps

Creating a Telegram bot

After installing Telegram on a mobile phone of your choice, search for botFather.

node

Once found, send a /newbot command and follow these instructions:

  • Set a name
  • Set a username
  • Save the access token for future use

node

Creating Assistant Service

  • Login to your IBM Cloud account and go to the link provided in the Prerequisites section for Watson Assistant service.
  • Set your service name (should be an unique name), then click Create.

node

  • After creating the Assistant service you will be directed to the Service Details page. Click on Launch tool. Leave the Service Details page open, you will need this page later.

node

  • After launching the Assistant service, create a new workspace. Name your workspace as you wish.

node

  • Click on the Deploy menu on the left and save your Workspace ID, you will need that in later sections.

node

  • Go to the Intents tab then select Add intent.

  • Add #Hello and #Goodbye intents respectively

node

node

  • After creating intents go to the Dialog tab and click Create to create a dialog.

Welcome and Anything else nodes come by default. The Welcome node is for the opening message, the Anything else node is for the unhandled text input.

  • By clicking the 3 dots on the Welcome node, add a different node below and name it hello. Configuration of the hello node can be seen at the image below.

node

  • Click the three dots on the hello node then add a new node below. This time name it goodbye.

node

  • Now you can test your chat bot by clicking the upper right conversation icon.

node

Sample chat dialog should look like this:

node

Creating and Configuring Node-RED instance

  • Now go to the link provided in the Prerequisites section for Node-RED starter.
  • Name the instance as you want then create it. It can take up to 5 minutes for the instance to be ready.

node

When you see the Running state click on Visit App URL.

node

  • After going to your App URL follow the follow the instructions provided. You need to set Username and Password for accessing your Node-RED application. After setting your credentials you will be able to access the Node-RED interface.
  • Select manage palette from the top right menu.

node

  • At the manage palette menu click on the Install tab then search for telegram.
  • Install node-red-contrib-telegrambot. After installation is completed close the palette menu.

node

Search for telegram from the upper left filter section then drag and drop Telegram receiver and Telegram sender nodes. node Double click on the Telegram receiver node and click on the pencil icon for configuring your bot credentials

node

Fill the bot-name and token fields according to the bot credentials you created earlier.

node

In Telegram sender node select the bot credentials you created in Telegram receiver node(example_bot in my case).

node

Now you have configured the Telegram part on Node-RED. You can test it by connecting the Telegram receiver node to the Telegram sender node.

node

You can send a message to your bot on Telegram and it will echo the message you wrote. That’s because we forwarded the message payload directly to the Telegram sender.

node

Now that bot interface is ready, let’s start the integration of Watson Assistant service. In the first part there was a Service Details page for the Assistant service. Go to that page and find the Connections tab.

node

  • After opening the Connections tab, click on Create connection and find the Node-RED application you created, then make the connection.
  • After making the connection go back to your Node-RED interface and search for conversation in the search bar in the upper left corner. After finding the conversation node just drag and drop it.

node

  • We need 2 function nodes for preparing the message object in Json format. One is for preparing the message before sending it to the conversation node. The other one is for preparing the message before sending it to the Telegram sender node.
  • Search for function in the upper right search bar as before, then drag and drop 2 function nodes. Name your function nodes Prepare1 and Prepare2.

Content of the function nodes:

node

Finally, your Nodes should look like the one below:

node

Click on Deploy button in the upper right, then test your application on Telegram.

node

Summary

Now you have created a chat bot with a Telegram interface. This is a really simple demonstration of developing a bot. Node-RED can make things easy for this use case. The dialog created is very basic, it can be modified and enhanced in respect to your needs.