This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Content sources
The chatbot answers from the content of your site. Posts and pages of WordPress are indexed by themselves. Content that is not a WordPress post (documentation served from Markdown files, a catalogue read from another database, a knowledge base kept by another plugin) is added through a content source: a few lines of PHP in the plugin that serves that content.
A document of a source is then indexed like a post: cut into passages at its headings, found by the exact-word search and by the search by meaning, read whole by the chatbot when needed, and linked to in its answers.
1. Declare the source
add_filter( 'lrob_chatbot_sources', function ( array $sources ): array {
$sources['docs'] = array(
'label' => __( 'Documentation', 'my-docs' ),
'list' => 'my_docs_list', // callable( int $page ): array{ items: list, more: bool }
'document' => 'my_docs_document', // callable( string $id ): ?array
);
return $sources;
} );
- The key (
docs) identifies the source: 1 to 40 characters amonga-z 0-9 _ -. Keep it: a new key is a new source. label: the name shown in the settings, translated by your plugin.- The filter is read only while the index works (WP-Cron, WP-CLI, the settings): never while a page is served.
2. list( int $page ): the documents
Called with $page = 1, 2, 3… until more is false. About 100 items per page.
function my_docs_list( int $page ): array {
$all = my_docs_all(); // your own list, in a stable order
$items = array();
foreach ( array_slice( $all, ( $page - 1 ) * 100, 100 ) as $doc ) {
$items[] = array(
'id' => $doc['lang'] . '/' . $doc['slug'], // stable, unique, ≤ 190 characters
'url' => $doc['url'], // https://example.com/docs/install/
'title' => $doc['title'], // Install
'lang' => $doc['lang'], // en, fr, pt-br
'modified' => gmdate( 'c', $doc['mtime'] ), // or 'hash' => md5 of the content
'breadcrumb' => array( 'Guide', 'Getting started' ), // optional
);
}
return array( 'items' => $items, 'more' => $page * 100 < count( $all ) );
}
| Field | Required | What it is |
|---|---|---|
id |
yes | Stable identifier of the item, unique in the source, at most 190 characters. |
url |
yes | Public address of the document, absolute (https://…). Answers link to it. |
title |
yes | Title of the document. |
lang |
yes | Language code of the document: en, fr, pt-br… |
modified or hash |
one of them | Date of the last change (ISO 8601, UTC), or a fingerprint of the content. A document whose date or fingerprint has not changed is never read again. |
breadcrumb |
no | The titles above the document (["Guide", "Getting started"]): given with each of its passages, so that a passage keeps its context. |
Several languages. One item per document and per language, each with its own id,
its own url and its lang:
array( 'id' => 'en/install', 'url' => 'https://example.com/docs/install/', 'lang' => 'en', … )
array( 'id' => 'fr/install', 'url' => 'https://example.com/fr/docs/install/', 'lang' => 'fr', … )
The chatbot then searches and links in the visitor's language, as for posts. Only the languages ticked in Knowledge base › Indexed languages are indexed.
translations (optional): the addresses of the same document in the other languages,
language code → address. When the site indexes only one language, the chatbot still links a
visitor to the page of their language:
array(
'id' => 'fr/install',
'url' => 'https://example.com/fr/docs/install/',
'lang' => 'fr',
'translations' => array( 'en' => 'https://example.com/docs/install/' ),
…
)
Give it on each item (list and document). Only http/https addresses are kept. A change of
the translations alone does not change modified / hash: send lrob_chatbot_source_changed
for those documents, or lrob_chatbot_source_reset, so that they are read again.
3. document( string $id ): one document
Returns the item (same fields as in the list) with its content, or null when it no longer
exists (it is then removed from the index).
function my_docs_document( string $id ): ?array {
$doc = my_docs_find( $id );
if ( null === $doc ) {
return null;
}
return array(
'id' => $id,
'url' => $doc['url'],
'title' => $doc['title'],
'lang' => $doc['lang'],
'modified' => gmdate( 'c', $doc['mtime'] ),
'breadcrumb' => $doc['breadcrumb'],
'html' => my_docs_render( $doc ), // the body only: no header, menu or footer
);
}
html(preferred): the body of the document as your plugin shows it. Itsh2andh3headings cut the passages; a heading with anidgives the passage its anchor (https://example.com/docs/install/#requirements).- or
text: plain text, paragraphs separated by an empty line. - At most 2 MB. Scripts and styles are ignored.
- Throwing an exception is fine: the document is set aside, shown in Knowledge base among the contents not indexed after an error, with Try again.
4. Tell the chatbot what changed
// Added or changed: indexed again in the background.
do_action( 'lrob_chatbot_source_changed', 'docs', array( 'en/install', 'fr/install' ) );
// Deleted: removed from the index.
do_action( 'lrob_chatbot_source_removed', 'docs', array( 'en/old-page' ) );
// Everything may have changed (a new version of all the files): the list is read again.
do_action( 'lrob_chatbot_source_reset', 'docs' );
These actions only queue the work: they return at once. When the chatbot is not installed, nothing listens and nothing happens.
Safety net: once a day, the chatbot reads the list again and compares modified / hash. A
source that sends no action is still right, at the latest the next day.
A document and a post at the same address
A source may give a document whose url is also the address of a WordPress post that is
indexed (the public page of an event, for example). The source wins: the post is not indexed
again at that address, so that the chatbot reads the page once, with what the source gives
(dates, place…). When the source no longer gives that document, the post is indexed again.
What the site's administrator sees
- Knowledge base › Other sources: one box per declared source, ticked by default, with its numbers (documents, passages, last reading of the list) and its errors.
- Chatbot › Content it answers from: the sources next to the post types.
- Knowledge base › Addresses left out applies to the documents of a source too.
- A source whose plugin is deactivated keeps its passages, marked source missing, until Index again.
Security
- The content is treated as text: never run, escaped wherever it is shown.
- Give only public content: everything indexed can be quoted to any visitor.
urlmust be anhttporhttpsaddress.