
The demand for Voice technology is rising and it is likely to revolutionize the way publishing websites engage with their audience. The Internet-connected virtual assistant is seeing a significant rise, but the question is how publishers can use this tech to grow their audience base and ultimately increase revenue? Here, we will explore how to use Actions on Google for a new project and an existing one followed up by an integration with Drupal 8 website.
Let’s have a look.
Integrating Actions on Google with a device
Integrating Actions on Google with an electronic gadget or smart speakers allow us to trigger voice command to control various Drupal commands such as:
- Clearing cache
- Count number of node
- Sending an email
- Run external as well as internal cron
- Control Drupal application to respond or insite defined action
- Integrating Google voice assistance with Drupal 8
1->Go to https://console.actions.google.com
- Create new Add/import project.
- Enter your project name and Country /region.
- Add a new project
Create actions on Dialog Flow
- Build a Dialog Flow, you will land to given page as per below screenshot.
- Now create a new intent and enter the following details as shown in the screenshot
Fill below field
- User says
- Response → Default → “voice control text which can speak”
- Select the field of Fulfillment and then save.
- Now click on Fulfillment.
- Enable Webhook
- Enter URL of your controller and save
Module development in Drupal 8
We are creating this module to hit '/ok-google-test’ page and return test reply setup after performing the process.
Create a custom module and a routing file:
okgoogle.test: | |
path: '/ok-google-test' | |
defaults: | |
_controller: '\Drupal\okgoogle\Controller\test::handleRequest' | |
_title: 'Ok Google' | |
requirements: | |
_access: 'TRUE' |
Create Controller:
<?php | |
namespace Drupal\okgoogle\Controller; | |
use Drupal\Core\Controller\ControllerBase; | |
use Drupal\Core\Logger\LoggerChannelFactoryInterface; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Symfony\Component\HttpFoundation\JsonResponse; | |
use Symfony\Component\HttpFoundation\RequestStack; | |
/** | |
* Class DefaultController. | |
class test extends ControllerBase { | |
/** | |
* Symfony\Component\HttpFoundation\RequestStack definition. | |
* | |
* @var \Symfony\Component\HttpFoundation\RequestStack | |
*/ | |
protected $requestStack; | |
/** | |
* The logger factory. | |
* | |
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface | |
*/ | |
protected $loggerFactory; | |
/** | |
* Constructs a new DefaultController object. | |
*/ | |
public function __construct(RequestStack $request_stack, LoggerChannelFactoryInterface $loggerFactory) { | |
$this->requestStack = $request_stack; | |
$this->loggerFactory = $loggerFactory; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function create(ContainerInterface $container) { | |
return new static( | |
$container->get('request_stack'), | |
$container->get('logger.factory') | |
); | |
} | |
/** | |
* Handlerequest. | |
* | |
* @return mixed | |
* Return Hello string. | |
*/ | |
public function handleRequest() { | |
$this->loggerFactory->get('droogle')->info('droogle triggered'); | |
$this->processRequest(); | |
$data = [ | |
'speech' => 'Cache Rebuild Completed for the Site by bisu', | |
'displayText' => 'Cache Rebuild Completed nishant', | |
'data' => '', | |
'contextOut' => [], | |
'source' => 'uniworld', | |
]; | |
return JsonResponse::create($data, 200); | |
} | |
protected function sent_mail(){ | |
} | |
protected function processRequest() { | |
$params = $this->requestStack->getCurrentRequest(); | |
// Here we will process the request to get intent | |
$to = "nishant@valuebound.com"; | |
$subject = "Drush cleared"; | |
$txt = "Drush cleared!"; | |
$headers = "From: nishant@valuebound.com" . "\r\n" ; | |
"CC: somebodyelse@example.com"; | |
drupal_flush_all_caches(); | |
mail($to,$subject,$txt,$headers); | |
// and fulfill the action. | |
} | |
} |
Now go to your bot.
console.dialogflow.com/api-client/#/agent/a128xxxx-711a-4c7f-b6b6-dxxxxxxxxxxx/integrations
You will see the similar screen where you can say clear caches.
With this integration, we can interact with our voice-enabled devices to use various Drupal functionalities, like cache clear, external cron run, sending mail, and others. Hope now you can integrate Actions on Google with your Drupal website. If you have any suggestions or queries please do comment below.
Below given is the presentation on "Integration of Google Assistant with Drupal".