Making Chatbot with Postman

Tae Hoshino
5 min readAug 9, 2021

--

Postman now supports WebSocket request with Socket.IO library!
In this article, I want to make a chatbot within Postman app which returns a relevant URL link based on a search keyword.

But before starting, let me cover some basics.

How is WebSocket different from REST APIs?

WebSocket protocol is an asynchronous and bi-directional (two-way) communication between server and client.
In comparison, REST APIs are synchronous — meaning that we have “request” and corresponding “response”.

With WebSocket protocol, client and server can talk to each other more freely like people’s conversations. (Exactly! That’s why it’s been used for chat apps)

Socket.IO

Socket.IO is one of the most popular libraries that enables real-time, bidirectional, and event-based communication between web clients and servers. It supports not only NodeJS but also other popular languages like Python and Java.

Product Spec

I want to make a chatbot which does the following:

  • returns a relevant URL link within Postman domain (*.postman.com) and its page snippet when a user enters a search word. (This is done using Google Custom Search API)
  • asks if the provided link was useful and prompt a user to enter “yes” or “no”
  • returns the next search result until user enters “yes”

The following diagram shows the flowchart of this process.

Workflow

Building Chatbot

First thing first — as always, run npm init to initialise and then install various libraries. This chatbot will use the following libraries:

Each library can be installed by npm i socket.io etc and imported with require call.

A local server can then be established by a few lines of code:

Go to Terminal and run the code with node test.js

Check the connection from Postman — go to your Postman and click on “new” to select “WebSocket Request”.

Select “Socket.IO” from the drop-down menu.

Enter ws://localhost:5000 in the address field and click on “Connect”. We can see the connection to localhost is established.

Now, since we are using Google Custom Search API to fetch search results, we first need to set up custom search and get API key.

For this chatbot, I have set up a custom search within *.postman.com domains.

Once we have custom search engine ID (that is tied to the custom search you created above) and API key, we can send the API request from code:

Next up, we should add “event” handlers. We will register two client-side events:

  • “help” — this is when a user enters a search word
  • “answer” — this is when a user confirms the information was helpful or not (input message is either “yes” or “no”)

Let’s also add acknowledgement of “help” event:

For the sake of simplicity, I will not include the rest of actual code, but please do feel free to fork from my GitHub repo.

Try it out in Postman!

Once your server is up, let’s go to your Postman and connect to the server. The server sends all the responses to “message” event, so let’s add “message” event under “Events” tab and toggle the “Listen on connect” option on. With this, Postman will automatically listen to this event as soon as the connection gets established.

Now let’s connect to the server and send a message! Turn on “Acknowledge” checkbox to receive the callback message. In the event field, enter “help” and in the message field, enter any search word.

To answer to the question “Was it helpful?”, enter “answer” in event field and type either “yes” or “no”. If “no” is entered, the chatbot will give you the next search result.

Conclusion

  • Postman now supports WebSocket request and Socket.IO library
  • This is still beta feature and Postman team is actively working on improving it and very actively listening to user feedback
  • Since this is beta feature, there are still some limitations (like WebSocket requests cannot be saved in a collection etc) — BUT, it is SUPER useful feature for developers and coding learners (like me) to quickly test out their backend code 👍

--

--

Tae Hoshino
Tae Hoshino

Written by Tae Hoshino

Hobby coder | Mother | Product manager

Responses (1)