[CREATING A ROBOT — PART 1] Teaching it to chat

Pedro Vallese
The Startup
Published in
5 min readJun 14, 2020

--

Since the beginning of June I started a new project to develop a robot that could talk, walk, and think on its own. It seems like an impossible job but using technologies and services already available it can be made. I’m not sure how to do the rest but I got the chatting figured out! In this article I’m going to tell how I made my robot to chat using Golang and DialogFlow.

What is speaking?

The first thing we need to understand is that what is the nature of speak, talk, and language. How does someone (or something) build a sentence to demonstrate a thought and share with someone else? (or something else!) The state of the art of this matter is abundant and it would take me an entire article just to explain how it works. If we could boil it down to one thing this thing would be natural language processing, a topic from artificial intelligence that studies how robots can communicate with humans and vice-versa. If you wish to dig down to the philosophy of this matter I recommend the works of a great guy called Noam Chomsky. For a quick glance of how it works I suggest this article made by Dr Michael J.

Fortunately we have a service that abstracts all the complexities and delivers a very intuitive and easy tool called DialogFlow. It’s a great product made by Google for people that wish to build chat bots without facing the struggle of NLP complexities.

Photo by Jens Johnsson on Unsplash

For in-depth explanation of how DialogFlow works I suggest searching its documentations and content. To keep this article less dense there are just two things to have in mind when using DialogFlow:

  • Agent: It’s the main topic of a chat robot, it’s used to keep one “Chat robot” separated from another, for example you can have an agent for small chat purposes, or an agent that speaks about the weather.
  • Intent: It’s an Agent segment used to describe the intent of one’s conversation. If you say “good bye”, dialogFlow triggers the “good bye” intention and then it will search for responses inside this Intent.

Keep in mind, there a lot of conceptions inside DialogFlow, these two presented above are just the extremely basic things when talking about Google’s Chatbot Service DialogFlow.

A true coder codes the code

Enough of conceptions and explanations. Since talk is cheap, let’s see the code! For real integration between Go’s API and DialogFlow Service I suggest checking this article which most of my work is based. For now, I’ll show my code that implements the solution. Alright! Let’s Go!

Photo by Laura Ockel on Unsplash

The full code can be found on my GITHUB. I’m using some cool concepts like Hexagonal Architecture and SOLID modulation, if you find it somewhat messy or missing something few free to comment your critic! Below I explain my code:

main.go

This is the code that contains the main instance of the HTTP server, It gets the PORT from the environment variable setup and calls for the CreateRouter() function which creates a router. A router is used so you can wrap all server’s endpoints and configuration into a single object for easy manipulation (We’ll see more details soon). After ther router is created it opens up the server with the specified port and router.

router.go

There are two libraries imported, one is gorilla/mux. A library used to wrap endpoints and configurations and the other is rs/cors used to make cors configurations easier.

On the CreateRouter() is the function called by the main.go file. It creates a mux router and passes to the function routes that puts contents inside its object.

On function routes() first I handle the endpoints, since I only have one endpoint I only need to write this line of code:

router.HandleFunc("/",inputInterface.HelloWorld).Methods("POST")

First it says which endpoint is: / then tells which function is triggered: inputInterface.HelloWorld last but not least it is described what method triggers the function: POST.

Then there is Cors configuration:

c := cors.New(cors.Options{AllowedOrigins: []string{"*"},AllowCredentials: true,})

It handles the request and checks if the one that is making the request can do so. Consider it as an “Application Firewall” (I know it’s not but pretend it is, it helps to understand). On my cors configuration I’m saying that any method from any origin is allowed, since my application dosen’t send any sensitive information I might not have trouble.

Dtos

On Dtos are the types, more specific the objects received on the request and the response given, they are pretty simple but it’s good to know and a good pratice.

Both are extremely simple, just a struct with a single variable, it was made to keep the clean code concepts.

Input

Following Hexagonal Architecture, this is the single input that the server has to handle. It receives the input, decodes the json and delievers it’s content to the service function PrimaryService. . Gets it’s response and delivers it back to the client.

NOTE: It was wrongly implemented, this code behaves more like a Controller than a Hexagonal Architecture’s Input. I might fix this issue as soon as possible. It works anyway though.

Services

Finally! Our service! This is where we actually communicate with Dialogflow, send the message received, and then he returns a response based on the Intent. The response then is returned to the caller which sends back to the client.

It’s done! With this code we have a CHATTING ROBOT!

Finally, I guess — I took this GIF from GIPHY

I made it avaliable on a heroku cloud server, it’s in Portuguese but it’s fully functional:

I might deploy an english version soon (Let me know in the comments if you guys want it).

GITHUB CODE:
Below is the back-end code developed, it needs to be set up with the dialogFlow integration’s configuration.

I don’t know if it helps but here’s the code to the front end application also, I made in REACT (I strongly advise against it, I’m awful at front-end application, check the code and you’ll know why)

That’s it! Let me know your thoughts in the comment! Any feedback is really important for me!

--

--