Creating a Wikipedia bot in Api.Ai using Wikipedia API and integrate it in Telegram

This post briefly explains how to use Wikipedia’s Open Search API to search Wikipedia and return the result to our api.ai Bot.

Wikipedia API

Example Call:

https://en.wikipedia.org/w/api.php?format=json&action=opensearch&search=global%20warming&limit=2&profile=fuzzy

Parameters:

Format – any of json,jsonfm,xml,xmlfm  More in the Docs

action –  opensearch

search – our term to search for, here it’s “Global Warming”

limit – Max number of results to return, we set it to 2. To make the call faster as it would return fewer data.

profile – more in the Docs

 

Example Result:

[
   "global warming",
   [
      "Global warming",
      "Global warming controversy"
   ],
   [
      "Global warming, also referred to as climate change, is the observed century-scale rise in the average temperature of the Earth's climate system and its related effects.",
      "The global warming controversy concerns the public debate over whether global warming is occurring, how much has occurred in modern times, what has caused it, what its effects will be, whether any action should be taken to curb it, and if so what that action should be."
   ],
   [
      "https://en.wikipedia.org/wiki/Global_warming",
      "https://en.wikipedia.org/wiki/Global_warming_controversy"
   
]

 

Api.ai Intent

Create a new Intent for our agent. In what user says section add few sentences like below screenshot. When you enter these sentence api.ai might pre-select few words as system entities. You would want to remove any preselection and select the term to search of in all the sentences and define it as @sys.any

In the action, you will find the parameter, name it “wikisearchterm”. You can name it anything but our woobhook is made to recognize this parameter. Also, make it Required and define prompts.

 

And Check “Use Webhook” under fulfillment. We will be using the same URL as in the last tutorials.

Url is: https://apiapiwebhooksamples.herokuapp.com/webhook

 

WebHook

You can check the complete code for the webhook at my GitHub. It just a simple GET request to Wikipedia. And returns text for api.ai and telegram integration. I am putting all the webhook so far in one file. So you will see other webhook there as well.

 

Bot In Action

Telegram bot: @iameurekabot

 

Api.ai Web integrated Bot:

 

Creating Follow-up intent for Recipe bot

In recipe bot post, we created a bot that helps you find a recipe for a food item. Obviously, there are many ways you can make a certain dish and there are many recipes available for the same item. We will handle that scenario here and let bot give user more options using Follow-up intents.

Api.ai introduced Follow-up intents recently ( in March 2017) and it’s a great way to organize your intents. Read more about it at their blog.

We will create a new follow up Intent of type “more”, show me more recipes, under our Recipe-Search Intent. So hover over parent intent, Recipe-Search, and there will be an option to create a follow-up intent. Choose “more” from the drop-down.

You can see it grouped under parent intent.

You will not have to, almost, set up any thing in this intent apart from hooking it up with WebHook from Fulfillment section. It will be pre-populated with predefined user inputs. Of course, you can add more customized user inputs.

The good thing here is that you don’t need to setup contexts, as they are automatically setup for us. However, we will need to add the FoodItem param to be passed down to our hook.

Set it a new param named “FoodItem” associated @FoodItem entity and value as #FoodItem.FoodItem ( #FoodItem is our parent context).

 

Don’t forget to Check “Use Webhook” under fulfillment.

Our webhook already randomize the recipes so we don’t need to customize it. Every time, you say “more”, there will be a new recipe.

Here is a screenshot of this bot working with Telegram:

 

Create a simple Recipe finder bot with Api.ai and webhook hosted on Heroku

Recently, one of the colleagues at work introduced api.ai and I was quite intrigued. So I started taking a look and did a small recipe finder bot. I wanted to document the process for others and as well as for my future reference.

Requisites:

  • An account in Api.ai (it’s free)
  • Heroku Account ( it’s free as well). For hosting our webhook used by the bot.

… 

 
%d bloggers like this: