Background

Back when we were actually in the office, our team efficiency was plagued with a recurring problem — we never knew where we wanted to eat. Team lunches became a difficult task to plan around, but as developers, we held both the great power and responsibility to automate trivial problems with new and popular technologies.

With our latest internal hackathon at NCR, we were given the opportunity to hack with Slack and create Slack apps with the help and advice from the Slack team. Our hackathon team came together to create a solution to this problem once and for all in our Slack workspace. With the use of Dialogflow, a platform that offers an interface for integrating natural language into apps, and FastAPI, a simple yet performant API framework for Python, we were easily able to create an interactive chatbot, named Eatware, that would do our bidding.

GIF demonstration of Eatware, our Slack bot

If you don’t know where to eat, try Eatware!

Here’s how we quickly set up our application using these three technologies.

Application Structure

We can split up our application structure into three layers:

  • Our presentation layer (Slack), where the user interacts with the bot and receives information from it
  • Our language processing layer (Dialogflow), which directly receives info from Slack
  • Our backend layer (Eatware API), receives webhook requests from Dialogflow, pulls restaurant information from external APIs, and sends the results back to Slack.

Diagram of application structure

Beginning with the presentation layer, we have the user prompt the bot to begin the experience. We identified the need for the user to converse with the bot in natural language to be a core part of the experience, so it blends in naturally with a team channel’s conversation (and it’s also a fun excuse to use the technology).

The user tags the bot and asks it in natural language that they’re looking for some food.

@Eatware, I’m looking for a bite to eat.

Dialogflow, the language-processing layer, is configured with Slack to read all the requests that mention the registered @Eatware application. It reads the sentence, uses the information we’ve trained it with to identify the user’s intent and any parameters they may have supplied. Dialogflow will respond in a conversation flow, prompting the user for any missing parameters, and when it’s reached a satisfactory point, it will make a webhook call to a location of our choosing. In our case, it’s an API we’ve set up to receive this call.

Our backend layer, built with FastAPI, receives the now-complete payload of parameters from the user and makes a call to an external service with our restaurant search terms. After this call is performed, our API maps the external response to a Slack payload, and sends the response to Slack in order to respond as the bot.

Now that we’ve covered how the application is set up, let’s get into the finer details of each part.

Dialogflow

Dialogflow is an E2E dev tool used to create conversational interfaces for websites and apps. Once the model is trained, it is compatible with multiple platforms including Facebook, LINE, Telegram, and more. For our particular project we integrated with Slack, where the user base composed of office workers enabled us to test features with potential users directly.

Dialogflow on Eatware is our middle layer, taking in user inputs flexibly from Slack, parsing relevant information out from a statement, then sending it as a webhook request to our API. When the API returns a response, Dialogflow takes the information, pieces it together into a user-friendly format, then outputs it back to the user as a Slack message.

Image for post

The main factor here is training our Dialogflow agent to recognize common sentence patterns that the user might use and determine keywords that fall into a category.

Initially, when presented with the sentence:

“Help me find an affordable Chinese restaurant nearby”

Dialogflow will not recognize these words as data. However, by manually training it by setting up a few words in the dictionary, such as “affordable = price” , “Chinese = food preference”, and “nearby = address”, the sentence will become strings of data to our agent.

Image for post

And thanks to its detection functionality, we don’t have to individually bind every word to a category; Dialogflow will attempt to do it itself when it finds a similar sentence structure. So next time when we ask the bot to “help me find an expensive Italian restaurant in Atlanta”, the agent detects the similarities in the sentence, deduces the user’s intent, and already correctly assumes the respective categories. As with all machine learning, Dialogflow improves its NLP prowess through everyday usage and user feedback.

Image for post

Once the parameters are identified, we can allow Dialogflow to passes these to a fulfillment webhook request to our API.

#chatbot #python #dialogflow #api #developer

How to Built a Slackbot with Dialogflow and FastAPI
13.45 GEEK