Feb 23, 2021

“Look Ma, No Hands!” WordPress Loop is running on Alexa!

The latest Convoworks WP update (0.20) includes the WP Core package (convo-wp-core), which in its very first version includes several post orientated components. These components will enable you to read the post data in a quite familiar way, using the WordPress loop in a slightly adjusted, conversational manner.

Let’s briefly check them all.

WP Query Context

As you probably know, the loop is a WordPress way to fetch and navigate through the post based data. The core of the WordPress loop is the WP_Query class and we are using it here in Convoworks too. We are defining it through WP Query Context component and its parameters. Through this component you are defining arguments which are directly passed to the real WP_Query. Meaning that everything you are using usually, in your WordPress templates, you can use here. When you are defining those arguments, you can use expressions to access service level variables (e.g. defined in service variables or through the Set parameter element).

This component keeps track of the posts pagination and selection state across the user session. Because of that, connected components do not have to bother with it, resulting in much cleaner conversation definition.

WP Query Context also defines two handy structures, which are injected into evaluation context and available for use by the connected components and their child components.

Page info fields

Page info has page orientated information and direct access to the WP_Query object.

  • last, boolean, is the current page last page
  • first, boolean, is the current page first page
  • page_no, integer, current page, starting from 1
  • query, actual WP_Query object

Post info fields

Post info has post orientated information and direct access to the WP_Post object.

  • abs_last, boolean, is the current post last post in the results
  • abs_first, boolean, is the current post first post in the results
  • abs_post_no, integer, position of the post in all results, starting from 1
  • last, boolean, is the current post last post on the current page
  • first, boolean, is the current post first post on the current page
  • post_no, integer, position of the post on the current page, starting from 1
  • post, actual WP_Post object
  • meta, loaded meta for the current post. Associative array, in the key=>value manner

WP Loop Page and Post Blocks

When you have multiple results you’ll probably want to display it (or say, if you are a smart speaker) in a paginated manner. What you will firstly note in the WP Loop Page Block is that it has plenty of the flows which you can use.

First three flows are for reading the current page results. First to say or display something before the loop starts, then for each post in the loop and lastly, to optionally say/do something after the loop has ended.

When a user selects an item, this block will trigger Post Selected Flow, where you can decide what to do with the selected post. Direct the user to some details step in the conversation, do some action and stay on the current step, whatever your service requires.

Besides standard Failback flow, here you can define different responses or actions if the user asks for an impossible navigation option. If left empty, the Failback flow will take the place.

If you want to add some other user request you might want to handle, you can do it by adding processors to the Process Phase (e.g. Help, Repeat …)

WP Loop Post Block is much simpler and besides the post and page info, here you got two additional, invalid navigation flows.

WP Query and WP Loop Elements

WP Query Element is a simple element that enables you to check were there any results yielded by the WP Query Context. It has two child flows, one for the case when there are and one when the results are empty. It is quite handy if you are not sure if there will be results and you have to cover both flows.

When you do not need pagination through results or for any other reason you just want to iterate through each post, you can use WP Loop Element.

Both elements are using the WP Query Context as a datasource and will read it’s current results page.

Built in intents

As you already noticed, Loop Page and Loop Post Blocks have navigation and selection as built in ability. While navigation uses standard intents (by saying: Next, Previous), selection is using two intents defined by wp-convo-core package: SelectPostIntent and SelectLastIntent. They are both defined in a quite generic manner.

WP functions

The wp-convo-core package defines some base set of specific WP functions to start with. These functions are directly bound to the real WP functions, so everything that works for original, should work here too.

Currently used WP functions

  • get_the_excerpt()
  • get_the_post_thumbnail_url()
  • get_the_author()
  • wp_strip_all_tags()
  • wp_trim_words()

The list will certainly grow through the time and if you have an idea which WP functions should we cover first, be free to give your suggestions in the comments section below.

Example service

Here is the simple service that demonstrates some basic ways of how the WP components might be used in your service. Import it into your Convoworks WP installation (Import/Export view), connect to Alexa or Dialogflow and enjoy.

There are just three steps in this service. Home just welcomes the user and asks for a search term. The user can respond with phrases like Search for bags, Find me bags, Show me all (check the SearchPostsIntent and ShowAllIntent intents).

After that we end up on the Search Results step where we can navigate through paged results (by saying: Next, Previous) or we can select one of the results (Select first, Select last, Show me the second …). Selection will lead us to the Post Details step where we can again use next and previous commands, to navigate through individual posts this time, or we can request to return to results.

The user can change the search term at any moment (by saying: Search for bags, …) or change sort order (Show oldest first, Show me the newest).

Test service: loop-test

Conclusion

This package is quite a good example of how the Convoworks can be extended and optimized in various ways. With such custom packages, you can create much more capable and suitable conversation components, while keeping flexibility which Convoworks GUI allows in the first place.

If you already haven’t, download and install Convoworks WP and see it for yourself.