8

   # index.js

const functions = require('firebase-functions');
const DialogflowApp = require('actions-on-google').DialogflowApp;

exports.handler = functions.https.onRequest((req, res) => {

    const app = new DialogflowApp({ request: req, response: res });
    console.log("Request", req);

    console.log('Request Processing ');
    function responseHandler(app) {
        let intent = app.getIntent();
        console.log("INFO Intent : ", intent);
        switch (intent) {
            case 'input.welcome':
                console.log("INFO : UserId: ", app.getUser().userId);
                app.ask("Welcome  to notify Applcation")
                break;

            case 'finish_permission':
                if (app.isPermissionGranted()) {
                    console.log("INFO : UserId: ", app.getUser().userId);
                    app.ask("Ok, I'll start alerting you");
                } else {
                    app.ask("Ok, I won't alert you");
                }
                break;

            case 'check_overdue_tasks':
                if (app.isPermissionGranted()) {
                    console.log("INFO : UserId: ", app.getUser().userId);
                    app.ask("Ok, I'll start alerting you");
                } else {
                    app.ask("Ok, I won't alert you");
                }
                break;


            case 'setup_update':
                app.askForUpdatePermission('check_overdue_tasks');
                break;
        }
    }
    app.handleRequest(responseHandler);

})


################################################## send ##############################################


var request = require('request')
const google = require('googleapis');
const key = require('../config/Agent33-e4a3b7e88308.json');


let notif = {
    userNotification: {
        title: 'Pay Parking tickets',
    },
    target: {
        userId: 'ABwppHF74yXbA9Z1ptgyOVwwkU8p9meRgs3H51Aw6_AqQZTzUgFzdz1twy6ki1aI-CjziWJPlqSdJUdbzQ',
        intent: 'check_overdue_tasks'
    }
}


let jwtClient = new google.auth.JWT(
    key.client_email, null, key.private_key,
    ['https://www.googleapis.com/auth/actions.fulfillment.conversation'],
    null
);

jwtClient.authorize(function (err, tokens) {
    if (err) {
        console.log("ERROR on jwt CLIENT");
    }
    else {
        console.log("Token : ", JSON.stringify(tokens) + "\n Notification Msg : " + JSON.stringify(notif));

        request.post('https://actions.googleapis.com/v2/conversations:send', {
            'auth': {
                'bearer': tokens.access_token
            },
            'json': true,
            'body': { 'customPushMessage': notif, 'isInSandbox': true }
        }, function (err, httpResponse, body) {
            console.log(httpResponse.statusCode + ': ' + httpResponse.statusMessage)
        });
    }
});


############################################################################################

I exactly followed the steps illustrated in documentation :

https://developers.google.com/actions/assistant/updates

The issues encountered are :

ISSUE 1 :

app.askForUpdatePermission(INTENT) : is not updating the permission to send push notification for the intent but it says the permission is granted. and in case tried to execute the 'final_permission' intent again, its say the permission is granted .

As when I try to execute app.isPermissionGranted() , it returned false.

ISSUE 2 : The Server 'https://actions.googleapis.com/v2/conversations:send' returned 500 or 400 Error randomly.

I am using free plan of FireBase (Spark) Is it because of that ?

JISHNU T U
  • 186
  • 1
  • 11

1 Answers1

0

Make sure you have set exact intentname like setup_update and text should be exact for Suggestion chips. Otherwise your assistant won't recognize text.

Make sure you have enable webhook for specific intent if you had handle using code.

Hope you read and follow their Action On Google document.

Test it on a real device. You will get a notification.

ChintaN -Maddy- Ramani
  • 5,156
  • 1
  • 27
  • 48