Skip to Content
Technical Articles

Developing News bot with SAP Conversational AI

COVID-19 pandemic has accelerated the shift towards a more digital world, every kind of business is moving to the online world, be it a small size, medium, or large enterprise. Every industry making its presence online, Finance/Banking, Healthcare, Travel, Education, Legal, HR, Real Estate, Media, E-commerce, Government because you want to know the status of your request, product/food delivery status, a complaint about faulty product or service, help in course enrollment, airport virtual assistant, getting health/car insurance quotes, etc. All these results in a huge spike in calls to service centers and they are unable to cope with this situation due to staff shortage, pandemic situation, which results in a bad customer experience. And intelligent technology seems the only cost-effective answer to many of the problems.

The world is now conversational, and AI-based chatbots have proven effective at delivering high-quality customer and employee experiences. SAP Conversational AI is an end-to-end chatbot platform to build enterprise-ready chatbots with AI technology from SAP. By deploying conversational interfaces embedded into SAP and non-SAP solutions, you can manage business tasks more smoothly and create a better user experience. Smart chatbots allow customers to get immediate responses to their questions, and employees to access the internal resources they need.

In this article, we’ll create a news chatbot with SAP Conversational AI, which will provide news headlines based on the user’s interest, recognize the user’s reaction, and react accordingly, calls an API to get the latest news headlines.

 

Create SAP Conversational AI account

Go to https://cai.tools.sap/, and click Sign Up in the upper-right corner.

Follow the instructions for creating an account.

 

Create a new bot project

Fill in the following details

 

There are 4 stages in a bot’s life :

 

  • Train: Here you teach your boat to understand what is expected. Here you will create intents – ideas your bot will recognize – using expressions that the bot should recognize coming from the user.
  • Build: At this stage, you give your boat the ability to meet these expectations, by giving the skills it can perform. Here you will create skills – things your bot can do – and define when they will be triggered. Your skills are defined by triggersrequirements (information your bot must collect), and actions.
  • Connect: Making your boat live to available platforms.
  • Monitor: Train your bot to make it sharper, and get insights on its usage.

 

Train

In the training phase, we build intents. Intents are things which means telling bot what is intended. Within an intent, we indicate all the possible expressions that a person might use to communicate that intent.

Go to Train tab –> Search for @news-selector intent

Click Fork for the @news-selector intent.

There are many existing intents and you can select or you can create your own based on your requirement

Explore the intent by clicking on it.

In the intent we forked, there are around 150 expressions to discern if someone wants to be told the news interest.

From the Add an expression field you can add expressions if you are creating new intent or update the existing intent with new ones.

 

Create an entity for news type

Our bot must be able to extract the type of news from within the conversation. Such data extracted from a conversation is defined as entities. Here you’ll create entity news-type.

Click on the #news-type entity and open it to explore. Here you will see the values of our custom entity tagged in your intent.  ( To tag the values you need to goto intent and then under the expression list edit the expression, then select the keyword you want to tag. )

 

 

Create a second intent @news-interest, we are creating this intent to make our bot understand the user reactions.

You can simply search for @news-interest intent and Fork it to use in your project.

Build

Now that your bot knows how to understand people who talk to it, it’s time to give your robot some skills. Each skill represents a task/action that your bot will do to achieve what is intended from it.

Like human behavior, when a command is given to us, first we understand what is expected, and then based on certain conditions we evaluate and then perform some action.

Just like intents, you can create a skill from scratch or inherit skills from other bots you’ve created or from other people’s bots.

In our project, choose the predefined skill Greetings.

The Greetings skill – like all skills – has 4 tabs:

  • README.md: A description of the purpose of your skill
  • Triggers: The conditions that must occur – generally the intents that the user must express – for the skill to be executed
  • Requirements: Information that must be collected in order for the skill to be executed
  • Actions: The action to take (basically, this is the skill)

 

In the Build tab, we’ll create a new skill show-news.

 

Click on the newly created skill show-news name and open it.

Under the triggers tab, we added one condition that must occur to trigger the action.

As our bot will call an external API service to get the latest news headlines, we need to pass some data to that service to get the type of news we want like sports, entertainment, health, etc.

So we will set the requirement in the Requirements tab. I hope you remember that entity we created earlier, we’ll set the requirement that this information is required before executing actions.

 

Now we’ll set what action to take when #news-type is complete.

Set the Memory Field, we’ll use this field later, when we’ll call the external API service.

{
  "raw": "{{memory.news-type.raw}}",
  "value": "{{memory.news-type.raw}}"
}

And when #news-type is missing, Then simply send the Message back to the user.

Ohhh !! I’m not taught to bring this news to you.

 

Finally, we’ll call the API service to fetch the news in the Actions tab.

First, check a condition if news-type is present or not. If it is present only then the API service will be called.

Here you can add the URL of the API service which will send the latest news headlines based on the news-type information we share. You can create your own service and deploy it on the SAP Cloud Platform or use any existing service which sends the results in JSON format.

I used the NewsAPI service and the following is the URL

https://newsapi.org/v2/top-headlines?q={{memory.news-type.raw}}&apiKey=2399fd7ecfc841ebaae8f5841f589f51

We are passing {{memory.news-type.raw}} tag, which will be replaced with the actual value, that our boat stored in memory.

Then add the Namespace to the Response tab – The API service response will be stored under this name. We have added news as our namespace.

To show the news results to the user, click on the + button sign below API Service Configuration and the output type of your choice.

 

You can select Text or any other control ( i have added a list ), In the text box, we can write the following to get the title and description from the results.

{{api_service_response.news.body.articles[0].title}}
{{api_service_response.news.body.articles[0].description}}

 

At the end click on Update Conversation and then select Reset Conversation. We reset the memory so that the user can enter another news type.

 

 

Connect

Now it’s time to deploy our bot to various platforms. There are various kinds of options available you can deploy chatbots to a website, Slack, Facebook, Microsoft Teams, and various other platforms.

Here we’ll deploy our news bot to a website.

Go to the Connect tab and perform the following steps.

Copy the Webchat script code, the javascript code for your HTML page.

Add the code to your website. Simply copy/paste the code on your website page where you want to make this bot live.

For testing, I created an index.html file and pasted this code inside <body></body> tag. Now open this index.html in your favorite browser.

<html>
<head>
<title>Developing News bot with SAP Conversational AI</title>
</head>
<body>
<center>Welcome to my website Newsbot</center>

<script
src="https://cdn.cai.tools.sap/webchat/webchat.js"
channelId="xxx959a556c-bceb-48bc-a12xxxxxxxxxx"
token="xxxf754bc21dd5dc4f0f018fxxxxxxxxxx"
id="cai-webchat">
</script>

</body>
</html>

Congratulations !! Your news bot is now live.

Hope you find that helpful! Let me know your thoughts on this in the comments section.

Thanks for reading!

6 Comments
You must be Logged on to comment or reply to a post.
  • That bot is a great example, and the blog is great 😃

    I have a few questions:

    • Why did we need to add the entity value #news-type into memory. Aren’t all requirements automatically entered into memory?
    • I wasn’t clear if you meant the #news-type to be used as part of the API’s free-text search (q parameter) or as one of the news types (category parameter). By the entity, it seems that you are signifying a category (restricted entity) but the way its used in API it seems like its free text (free entity).
    • Furthermore, I think the design is clever, and may really really work, but it seems that ideally there is an intent for getting news, and then there is an entity for what news (or 2 entities, one for category and one for free text). On the one hand, the second news intent lets me tell the bot I want to change the subject, on the other it seems unnecessary. But I don;t know.

    Anyway, I think its a great example — of writing a chatbot, using API, and using scripting.  👍

    Please check out more SAP Conversation AI tutorials at https://developers.sap.com/tutorial-navigator.html?tag=products:technology-platform/sap-conversational-ai/sap-conversational-ai.

     

    • Thanks, Daniel for your inputs 🙂

      Answers to your questions.

      Q. Why did we need to add the entity value #news-type into memory. Aren’t all requirements automatically entered into memory?

      You are right, the complete conversation is in memory, but which particular conversation triggered our intent, and from that conversation which entity-type, we are storing that value in a field.
      Because we need that information to API service to get the news of that type.

      For example - User type: I want to read the sports news. Here we are storing the word "sports" from that conversation.

      Q. I wasn’t clear if you meant the #news-type to be used as part of the API’s free-text search (q parameter) or as one of the news types (category parameter). By the entity, it seems that you are signifying a category (restricted entity) but the way its used in API it seems like its free text (free entity).

      We created this entity as free-text because our user can write anything, not a specific news category. but categories are tagged in our intent for that news-entity, we let machine learning to detect possible values.
      When user type "entertainment news headlines", ML extract word "entertainment" from this free-text entity and store in a variable, Later we pass this variable to API service (as q parameter).

      Q. Furthermore, I think the design is clever, and may really really work, but it seems that ideally there is an intent for getting news, and then there is an entity for what news (or 2 entities, one for category and one for free text). On the one hand, the second news intent lets me tell the bot I want to change the subject, on the other it seems unnecessary. But I don;t know.

      You are right we have one intent to understand user inputs and getting news. Users can change the subject anytime as we are resetting the conversation after our action. But the second intent of @news-interest is not to change the subject, it is to understand user reactions to provided news headlines.
      For example, user type- not interested in this / what else / any other thing In this case first intent will not trigger, now our second intent will trigger and ask the user to provide the news type like which another type of news you would like to read - sports/entertainment/heath, etc.

      Let me know if I missed something.

  • Thanks -- it still seems to me that the 2 intents are 2 closely related. They both answer "show me some news", sometimes with specific news type and some without. If I type "show me some news" -- and other examples -- it will go to news-selector and not be able to discern the news type. Maybe this can be solved by monitoring and learning over time.

    Why not a single intent where some of the expressions include news type, and some not. If the skill does not discern an entity, then you ask for one in free text from a second intent. See this bot: https://cai.tools.sap/thecodester/newsbot -- I think I could work on eliminating duplication of entity for new type and memory for new type, and also a little training. Just another possibility.

    Also it would be nice to have scripting to handle cases where the API returns fewer than 3 news items, but this is small point.

    All in all I like your bot 👏

    • Thanks, Daniel for your detailed analysis. You are right in most of the problems can be solved by monitoring and learning over time.

      Technically we can have single intent to do multiple jobs, but I think the bot script becomes complex over the period. After a period of learning in a productive bot we keep adding more and more skills. If we keep multiple intents, it will be easy to manage different types of skills, and easy to test the flow.

      Yes, there are a lot of cases that need to be handled in scripting, intent, and skills to make it a super productive news bot, it just a test of end to end working scenario. I'll keep updating this bot.

      Thanks for your valuable inputs.