Custom Tokens Bring Page Awareness to Drupal AI Chatbots

Teaching Your Drupal AI Chatbot to Understand Where It Is

A new blog post by Mag. Andreas Mayr describes how to give Drupal AI chatbots real-time awareness of their current page context by extending the Drupal AI module with custom tokens and server-side context reconstruction.

Traditional chatbots often struggle to interpret user queries that rely on the context of the page currently being viewed. To solve this, Mayr introduces a method for extending the AI Agents system using an event subscriber for the BuildSystemPromptEvent, allowing tokens from the existing current-page group to be referenced within system prompts.

A custom token named [current-page:ai-context] is defined and linked to a helper function that dynamically reconstructs the user’s location by parsing the current_route payload sent through the chatbot API. When entity context such as a node or commerce product is successfully resolved, structured metadata like title, type, and canonical URL are returned to guide AI responses.

This enhancement allows the chatbot to prioritize entity-specific answers when applicable, dramatically improving its relevance in context-rich environments such as product catalogs. The full code saamples and architectural notes are available on the Agora Design blog.

Disclosure: This content is produced with the assistance of AI.

Disclaimer: The opinions expressed in this story do not necessarily represent that of TheDropTimes. We regularly share third-party blog posts that feature Drupal in good faith. TDT recommends Reader's discretion while consuming such content, as the veracity/authenticity of the story depends on the blogger and their motives. 

Note: The vision of this web portal is to help promote news and stories around the Drupal community and promote and celebrate the people and organizations in the community. We strive to create and distribute our content based on these content policy. If you see any omission/variation on this please reach out to us at #thedroptimes channel on Drupal Slack and we will try to address the issue as best we can.

Related Organizations

Upcoming Events