The Ins and Outs of Building a Chatbot for Railways

About 10 years ago, no one would have thought that smartphones would play a significant role in e-Commerce transactions. Today, we can’t afford not to use them. Besides its basic utility, it’s a quick and convenient way to shop or order food.

Sebastian Mękal
May 29, 2018 · 8 min read

The time of Machine Learning & AI gives us new opportunities, opens new sales channels and at the same time simplifies our lives even more.

Why not order food by voice? Why not choose a new TV with the help of a chatbot where the sales agent is an algorithm that offers the best choice?

In this article, I will focus mainly on the technical side of chatbots. However, I will also put in a few facts showing how smart speakers operated by our voice or by a simple chat can become prospective sales channels.

Social media and Smart Speakers vs. existing e-Commerce systems

The power of social media networks is gigantic. Over 2 billion people now use Facebook each month. The scale effect combined with the knowledge these networks have about us (favourite band, favourite food, profession, last activity) can be extremely important from the e-commerce industry point of view.

Secondly, we have smart speakers. Devices that considerably simplify our lives using only our voice. It is worth mentioning that in 2017, around 25 million smart speakers were sold and the forecasts indicate further sales increases in 2018.

The statistics that were presented show how large the scale that we are dealing with is. Such a large scale is a massive number of potential customers.

Now, let’s look at traditional sales applications. Most of them support web and mobile channels. A typical sales application consists of the following modules:

  • Cart
  • Payment
  • Checkout
  • Order management
  • Customer management

Our goal is to take advantage of social media and smart speakers capabilities and benefit at the same time from our existing systems as well. To do this, we’ve create a chatbot and integrated it with our e-commerce system.

Currently, there are many platforms that offer a simple way of creating a smart conversation model. Some of them are specifically related to the device, like Amazon Alexa. Others allow an easy connection with different communication platforms, the best example here being DialogFlow.

Things to consider during integration of an existing e-commerce system with a chatbot

I’ve divided them into three main parts:

  • The intent: Get precise information about the user intent from the chatbot. It should additionally contain all the information (parameters) necessary for performing the activity. For example, the intent might be “Check the weather” and the necessary information can be the date and the city.
  • Conversation context: Chatbot should remember some information during the dialogue with the customer. Such information can be a reference to the cart created in our system, or a search criteria used in a previous search.
  • Account linking: Connection of an external user (e.g. Facebook user, Amazon Alexa user) with the customer in our system.

All the points mentioned above need to be taken into account, regardless of the e-commerce system. In this article, I am going to describe them in the context of SAP Hybris. The first and the second points will be described in the section below, while the third one you can find it below and it’s called “How to link chatbot users with the customers in our system”.

How does the e-Commerce system understand what it’s supposed to do?

To understand how our e-Commerce system knows what to do based on text or voice served to chat or Alexa, we need to know how chatbot systems work.

How can chatbots understand the user’s natural language?

Chatbot systems use two techniques to understand the natural language (user input) and break it into parts understandable for other systems:

  • Pattern matching is a technique of matching intentions based on a set of patterns that match it. The more patterns we provide, the more the effectiveness of the chatbot increases.
  • Intent classification is a technique used to break down user input into categories, such as Hamburg is a city, or March 23 is the date. Because of this classification, our e-commerce system knows exactly all parameters needed to process a specific action.

On this basis, we can determine two main components of chatbot systems:

  • Intent — specifies what the user wants to do
  • Entity — defines a parameter important from the Intent point of view. For example, if our chatbot extracts the intent called „Search trains” from the user, things like “departure station”, “arrival station” or “departure date” are actually „geographical city” and „date” entities.

The third and very important component is a way of storing state during the dialogue with the user. Such a „state” can be, for example, the cart number in our system, or search criteria ID that we used in the previous search. This state is provided to us through Context.

For our current chatbot we use the DialogFlow platform. The simple example below shows how easy the creation of intent is:

Figure 1 — Creating Intent in DialogFlow platform

Let’s move to the integration of our smart conversion model with the e-commerce system. Most of the available platforms offer integration through REST. The chatbot recognizes intentions, categorizes parameters and sends all gathered information in the appropriate format to our REST endpoint.

A typical request includes:

  • Intent code
  • Intent parameters. This is the result of the classification algorithms
  • Context
Figure 2 — Simplified sequence diagram of interaction between end user, chatbot and SAP Hybris integration

As I’ve mentioned at the beginning, SAP Hybris is our e-Commerce platform. From the architecture point of view, I have created core and web extensions (OCC add-ons). OCC add-ons are responsible for handling requests/responses for specific chatbot engines (like DialogFlow).

For the core extension, we mapped the main chatbot components, such as intent and context to the entities. Keeping these two components in the database has the following advantages:

  • The possibility of automatic detection of configuration changes in the chatbot (name of intent, list of contexts) and their automatic inclusion in our system. Some platforms like DialogFlow can provide such a possibility through its API.
  • Ability to re-use entities (context) in other intents that we’ve created.

Entities are mapped to strategies executing relevant Java implementations:

  • The Intent object is assigned to a strategy responsible for executing the appropriate action (e.g. for the intent “Add to cart”, the action of adding a product to the basket is performed and a result is returned).
  • The IntentContext object is assigned to the strategy responsible for building an easy-to-use context object. This object can be used in actions or returned in the response. The context strategy has a life cycle. It allows to perform operations before and after the action.

From the SAP Hybris point of view, a good example is the cart context. Our strategy restores the cart, based on the code we receive from the incoming request. The diagram below shows everything that was described in a simplified way.

Figure 3 — Mapping between core components of chatbot system and SAP Hybris module

After the action for intent has been executed, we need to send the response in the appropriate format. Usually, the response should include natural language text with a reply and output contexts, if needed, such as context with the Hybris cart number.

Depending on the message channel (Facebook, Slack) and the engine we use (DialogFlow, Alexa), the response may be completely different. It is good practice to have appropriate response strategies depending on the engine and the message channel.

How to link chatbot users with a customer in our system

Dialogues help customers to fulfill their objectives faster, no matter what those are, while at the same time build brand value and loyalty. Another interesting challenge is to conduct a conversation from the perspective of a user logged in to our system. By giving the user the ability to log into our system via the chatbot interface, we can personalize our customer’s experience by offering suggestions of products based on previous orders and we can offer discounts based on each type of customer. Each engine has its own approach for this problem.

We are currently working on the integration of two of them:

  • For Facebook Messenger we can use the Account Linking mechanism. This mechanism can be triggered from the “Log In” button available on the Messenger platform. The whole procedure is about displaying a Messenger Webview with a login form (served by our system). After logging in, an authorization token is introduced byour system. It will be later transferred back through a special Account Linking Webhook. Then is received the Page-scoped ID. This is an identifier assigned to the Messenger user and is retrieved per Facebook page.

After all of the above mentioned steps have been successfully completed, a direct connection between the Messenger user and the user in the SAP Hybris system is established. You can read more about account linking in Messenger here.

Figure 4 — Example of account linking in Messenger / Source:
  • For Amazon Alexa the authentication flow begins when the user starts interacting with the Alexa skill kit and we require authentication for some of operations. In such case, the skill serves the appropriate card in response (LinkAccount card). This card will redirect the user to the authentication URL in our system (Authorization URL).
Figure 5 — Example of account linking for Alexa skill / Source:

Amazon Alexa’s account linking mechanism is based on OAuth 2.0, so our system has to have an OAuth server in place. Authorization code grant or Implicit grant authentication modes are mandatory.

  • If Authorization code grant is used, our system returns the code after a valid login. Based on it, Amazon Alexa requests an Authorization Tokenand a Refresh Token from our OAuth server. Amazon links tokens with the Amazon Alexa user.
  • In the case of Implicit grant, our system serves the authentication code after a successful login. Amazon links authentication code with the Amazon Alexa user.


In this article, I’ve presented only the basic technical aspects of smart conversation platforms and integration with existing e-Commerce systems.

Our journey started with Machine Learning and pattern matching to understand the user’s input to put our SAP Hybris project into practice. Please note that not each use case is suitable for the use of chatbots.

A good dialogue should flow naturally and meet customer expectations. Nevertheless, you should have an end goal in mind and based on that to design a flowchart and pick scenarios. Keep in mind that customers want personality, naturalness and the sense they’re not talking to a machine.

When implementing chatbots, we should make sure that we provide the user all the relevant and necessary information in the most concise form. To achieve this, might be the case that our chatbot is not enough by itself. Other challenges for e-commerce systems might be intelligent product recommendation systems or dynamic pricing.

See our Chatbot Demo

If you want to see how the technical part translates into a real scenario for the railway industry, you can watch our iQRailway Chatbot demo below.

The Why and How

The iQuest Group blog, showcasing our latest work and ideas

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store