How do I make my bot to perform a (google) search on my website?
An example use case can be the FAQ bot which has a fallback to do a full-text search within your website or knowledge base (wiki) and presents the results directly inside the bot to keep the user focused.
In order to implement this, we’ll make use of a short text step, fold-out step, and bot variables:
- Create a Google custom search engine for your website:https://cse.google.com/cse/create/new and write down its Search engine ID
- Create a Google API key:https://console.developers.google.com/apis/credentials, note that it’s recommended to restrict it to your website only. Write down the API key.
- Add a short text step, and name it to search input, this will be used to ask for the search query
- Create such bot variables:
- searchInput, (type step), select the ‘search input’ step
- searchResults (type script, developer mode needs to be switched on), NOTE that you need to replace [API key] and [Search engine ID] with the previously obtained API key and Search engine ID 1.
function(callback, variables) {
var searchInput = variables.searchInput.value;
var url = "https://www.googleapis.com/customsearch/v1?key=[API key]&cx=[Search engine ID]&q=" + encodeURIComponent(searchInput);
quriobot.ajax(url, function (responseText) {
var response = JSON.parse(responseText);
var result = response.items ? response.items.map(function (item) {
return '<article><a target="_blank" href="' + item.link + '">' + item.htmlTitle + '</a><br>' + item.htmlSnippet + '</article>'
}).join('<br>') : 'no results found, try to change your search criteria'
callback('<div class="panel--wrapper">' + result + '</div>')
})
}
- Add a fold-out step, and set its name to search results, this will be used to display the search results
- Select the display type -> HTML and use the previously created variable: 1.
{{searchResults}}