Blog by Daniel Hauck. Sysadmin, Developer, Blogger.

In the first part of the tutorial, I showed you how easy the setup and the usage of is. We can now ask questions and can see how wit categorizes and analyzes them. Now it’s time to use this data, to give the user something he is asking for.

In today’s part of the tutorial I want to show you how to parse the data and give the user back the blog posts he is looking for.

Please be aware that I trained my AI a bit before. So be sure to add another value new to the blog entity, if you want to code along with me!

Extracting the entities

To know what the user wants from our bot, we first need to extract the given entities. For example, has he asked for our new fantastic blog posts, we should get the blog entity with the value new. Is he asking for the latest video on youtube, it should be video and latest.

This is how the answer looks for ‘what’s the latest blog post’:

To get down to the actual response, we first need to figure out, is there an entity called blog. Let’s add and if statement to our loop, right after getting the response:

if resp['entities'].has_key('blog'):

Next will be to determine if the user wants only the latest, or maybe a few new blog posts. We will use two values here. So let’s check first for the latest value and give the user some feedback. In the if statement, add another on, to check if the user wants the latest:

Query WordPress for blog posts

Now we know, the user wants the latest post, so let’s build the function get_latest_post, which will query my WordPress blog:

For convenience let’s add another function, to get the latest five blog posts as JSON:

Little hint here, I’ve put the WordPress code in a separate file called to keep the code more readable.

So let’s add an else path to our if statement, to give the user five blog posts, as a standard answer. Back in your while loop should look like this now:

I am still keeping the raw response as an output, just for debugging and adjusting purposes.

Test it!

Ok, we did something awesome 😉 Let’s test it! Start your crappy chat with python and ask it some questions:

And you can see, it already works and gives you the latest blog post. Of course, in a web-based chat, we could style it and get the images and so on, but for this basic example, it works.

If you now differ to what’s new, you will get five instead of one:


Is your output different? Then you should check your entities and values and give your app a little bit of training.

Enough chatting for today 😉 In the next part of the tutorial, I will add more entities and maybe add a little frontend.

To stay up to date, check my social media channels and don’t forget to subscribe to my youtube channel!

There are no comments.

Leave a Reply