Git Product home page Git Product logo

dialogflow-updates-nodejs's Introduction

Actions on Google: Daily Updates and Push Notifications Sample

⚠️ Warning: Conversational Actions will be deprecated on June 13, 2023. For more information, see Conversational Actions Sunset.

This sample demonstrates Actions on Google user engagement features for use on Google Assistant including daily updates and push notifications -- using the Node.js client library and deployed on Cloud Functions for Firebase.

⚠️ This code sample was built using Dialogflow. We now recommend using Actions Builder or the Actions SDK to develop, test, and deploy Conversational Actions.

Setup Instructions

Prerequisites

  1. Node.js and NPM
    • We recommend installing using NVM
  2. Install the Firebase CLI
    • We recommend using version 6.5.0, npm install -g [email protected]
    • Run firebase login with your Google account

Configuration

Actions Console

  1. From the Actions on Google Console, New project > Create project > under More options > Conversational
  2. From the top menu under Develop > Actions (left nav) > Add your first action > BUILD (this will bring you to the Dialogflow console) > Select language and time zone > CREATE.
  3. In the Dialogflow console, go to Settings ⚙ > Export and Import > Restore from zip using the agent.zip in this sample's directory.

Cloud Platform Console

  1. In the Google Cloud Platform console, select your Project ID from the dropdown
  2. From Menu ☰ > APIs & Services > Library > select Actions API > Enable
  3. Under Menu ☰ > APIs & Services > Credentials > Create Credentials > Service Account Key.
  4. From the dropdown, select New Service Account
    • name: service-account
    • role: Project/Owner
    • key type: JSON > Create
    • Your private JSON file will be downloaded to your local machine
  5. Save private key as service-account.json in functions/

Daily Updates and Push Notifications

  1. Back in the Actions on Google console > under the top menu Develop > Actions (left nav):
  2. Select the tell_tip intent > under User engagement:
    • Enable Would you like to offer daily updates to users?
    • Title: advice Alert > Save
  3. Select the tell_latest_tip intent > under User engagement:
    • Enable Would you like to send push notifications? If yes, user permission will be needed
    • Title: latest info Alert > Save

Firestore Database

  1. From the Firebase console, find and select your Actions on Google Project ID
  2. In the left navigation menu under Develop section > Database > Create database button > Select Start in test mode > Enable

Firebase Deployment

  1. On your local machine, in the functions directory, run npm install
  2. Run firebase deploy --project {PROJECT_ID} to deploy the function
    • To find your Project ID: In Dialogflow console under Settings ⚙ > General tab > Project ID.

Dialogflow Console

  1. Return to the Dialogflow Console > select Fulfillment > Enable Webhook > Set URL to the Function URL that was returned after the deploy command > SAVE.
    Function URL (aogTips): https://${REGION}-${PROJECT_ID}.cloudfunctions.net/aogTips
    
  2. In a browser, go to https://${REGION}-${PROJECT}.cloudfunctions.net/restoreTipsDB, to add data to the Firestore database.
  3. From the left navigation menu, click Integrations > Integration Settings under Google Assistant > Enable Auto-preview changes > Test to open the Actions on Google simulator then say or type Talk to my test app.
    • To test daily updates, choose a category and below the tip, there will be a Send daily suggestion chip to subscribe for daily updates.
    • To test push notifications, choose most recent and below the tip, there will be an Alert me of new tips suggestion chip to subscribe for push notifications. Then you will need to add a tip to Firestore DB to receive the push notification.

Push Notifications Configuration

  1. Then add a new tip to the Firestore Database to trigger a notification to the subscribed users. In the tips collection > select Add document:
    • Document ID: select Auto ID
    • field: category, type: string, value: tools
    • field: created_at, type: string, value: 2019-04-29T015:00:00.000Z and modify value to current date/time
    • field: tip, type: string, value: Here's the most recent info about tools
    • field: url, type: string, value: https://developers.google.com/actions/assistant/updates/notifications

Running this Sample

  • You can test your Action on any Google Assistant-enabled device on which the Assistant is signed into the same account used to create this project. Just say or type, “OK Google, talk to my test app”.
  • You can also use the Actions on Google Console simulator to test most features and preview on-device behavior.

Troubleshooting

  • When testing on an iOS device, ensure that notifications are enabled for Assistant under settings.

References & Issues

Make Contributions

Please read and follow the steps in the CONTRIBUTING.md.

License

See LICENSE.

Terms

Your use of this sample is subject to, and by using or downloading the sample files you agree to comply with, the Google APIs Terms of Service.

dialogflow-updates-nodejs's People

Contributors

atulep avatar smishra2 avatar taycaldwell avatar yoichiro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dialogflow-updates-nodejs's Issues

logged out of my test app

When I try the sample after running through the setup steps I get the following response:

Alright. Here's the test version of my test app.
It looks like you've been logged out of my test app. To log back in, you'll need to ask to talk to them again.

What am I forgetting or what did I do wrong?

crashes after entering category for daily updates

After I get my first tip, I select "Send Daily." It then asks for the category, to which I reply "random." It then crashes. Error log below:

2018-03-16T16:33:45.162Z I aogTips: Fri, 16 Mar 2018 16:33:45 GMT actions-on-google:debug getArgument: argName=category
2018-03-16T16:33:45.162Z I aogTips: Fri, 16 Mar 2018 16:33:45 GMT actions-on-google:debug askToRegisterDailyUpdate: intent=tell_tip, intentArguments=[object Object], dialogState=undefined
2018-03-16T16:33:45.162Z I aogTips: Fri, 16 Mar 2018 16:33:45 GMT actions-on-google:debug fulfillSystemIntent_: intent=actions.intent.REGISTER_UPDATE, specType=type.googleapis.com/google.actions.v2.RegisterUpdateValueSpec, intentSpec={"intent":"tell_tip","triggerContext":{"timeContext":{"frequency":"DAILY"}},"arguments":[{"name":"category","textValue":"random"}]}, promptPlaceholder=PLACEHOLDER_FOR_REGISTER_UPDATE dialogState=undefined
2018-03-16T16:33:45.162Z I aogTips: Fri, 16 Mar 2018 16:33:45 GMT actions-on-google:debug buildResponse_: textToSpeech=PLACEHOLDER_FOR_REGISTER_UPDATE, expectUserResponse=true, noInputs=undefined
2018-03-16T16:33:45.162Z I aogTips: Fri, 16 Mar 2018 16:33:45 GMT actions-on-google:debug isSsml_: text=PLACEHOLDER_FOR_REGISTER_UPDATE
2018-03-16T16:33:45.162Z I aogTips: Fri, 16 Mar 2018 16:33:45 GMT actions-on-google:debug getUser
2018-03-16T16:33:45.162Z I aogTips: Fri, 16 Mar 2018 16:33:45 GMT actions-on-google:debug isNotApiVersionOne_
2018-03-16T16:33:45.162Z I aogTips: Fri, 16 Mar 2018 16:33:45 GMT actions-on-google:debug Response {"speech":"PLACEHOLDER_FOR_REGISTER_UPDATE","contextOut":[{"name":"actions_on_google","lifespan":100,"parameters":{"category.original":"random","category":"random"}}],"data":{"google":{"expectUserResponse":true,"noInputPrompts":[],"isSsml":false,"systemIntent":{"intent":"actions.intent.REGISTER_UPDATE","data":{"@type":"type.googleapis.com/google.actions.v2.RegisterUpdateValueSpec","intent":"tell_tip","triggerContext":{"timeContext":{"frequency":"DAILY"}},"arguments":[{"name":"category","textValue":"random"}]}}}}}
2018-03-16T16:33:45.164Z I aogTips: Fri, 16 Mar 2018 16:33:45 GMT actions-on-google:debug undefined
2018-03-16T16:33:45.164172343Z D aogTips: Function execution took 48 ms, finished with status code: 200

Tiny Suggestion in tipsDB.json

To be perfect, second tip should have "created_at" instead of "timestamp", i think. Thank you :)

//original

{
      "category": "tools",
      "url": "https:\/\/www.youtube.com\/watch?v=sjpbu4y6F_M&list=PLOU2XLYxmsILvfJcIASBDbgfxloFz_XsU&index=5",
      "tip": "If you don't know how to code, you can use templates to build an App! Learn more about it in our Build an App for the Google Assistant with Templates and No Code video.",
      "created_at": "2017-12-25T08:00:00.000Z"
    },
    {
      "category": "voice user interface",
      "url": "https:\/\/developers.google.com\/actions\/assistant\/best-practices",
      "timestamp": "2017-12-21T08:00:00.000Z",
      "tip": "Remember that your users may need your app to repeat itself at any time, so keep track of your last prompt and be prepared to repeat it."
    },

//suggestion

{
      "category": "tools",
      "url": "https:\/\/www.youtube.com\/watch?v=sjpbu4y6F_M&list=PLOU2XLYxmsILvfJcIASBDbgfxloFz_XsU&index=5",
      "tip": "If you don't know how to code, you can use templates to build an App! Learn more about it in our Build an App for the Google Assistant with Templates and No Code video.",
      "created_at": "2017-12-25T08:00:00.000Z"
    },
    {
      "category": "voice user interface",
      "url": "https:\/\/developers.google.com\/actions\/assistant\/best-practices",
      "tip": "Remember that your users may need your app to repeat itself at any time, so keep track of your last prompt and be prepared to repeat it.",
      "created_at": "2017-12-21T08:00:00.000Z"
    },

tipDB JSON is not uploading

I am trying to implement addTips() function but after deploying it I am not seeing any changes in my firestore. If we manually need to create a database for this Please submit a picture.
Sorry I don't know it is an issue or not I am trying to implement push notif for my action

Getting Access token undefined

I am getting (node:8560) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: Cannot read property 'access_token' of undefined

Cancelling push notifications

Is there a way to cancel a Google Assistant push notification after it's been sent?

As IoT becomes more prevalent we could be notifying users via multiple platforms. If they've interacted with a notification somewhere else, we need a way to cancel that notification on other platforms.

Problem with latest firebase-tools

When using latest firebase-tools, I'm getting the following error:

Firebase config variables are not available. Please use the latest version of the Firebase CLI to deploy this function.

Solved installing [email protected]. This link helped me, documentation should reflect one of the two workarounds (downgrading firebase-tools or updating to latest firebase-admin and firebase-functions).

why Configure updates event is needed in setup_update intent

hi,

why Configure updates event is actually needed in setup_update intent? I assume this event is called when intent actions.intent.CONFIGURE_UPDATES is triggered. On the other hand setup_intent is triggered via utterance 'Send daily' (as a result of updates opt-in via suggestion chip).

From AoG doc (https://developers.google.com/actions/assistant/updates/daily#actionsdk) it is nto quite clear for what purpose actions.intent.CONFIGURE_UPDATES is actually needed. actionssdk example seems to be working fine without it. Can this be clarified?

thank you

Importing Agent Doesn't Work

Hi,
This may be a bug in Dialogflow, but when I try to import the agent.zip file via the import/export, the dialog box just stays gray. The file doesn't seem to upload.
image I tried from both a Mac and a Windows machine.

mismatch Action Name of the intent "tell_latest_tip"

The Action Name Intent:tell_latest_tip is different between index.js and DialogFlow.
Beacause of this, I can't try the sample project and needed to modify Action:tell_latest_tip of DialogFlow Project to tell.latest.tip.

Please check this 🙏

tell.latest.tip
tell_latest_tip

tell_latest_tip.json from unziped AoG-Tips.zip

{
  "id": "90926160-1418-410b-81a8-3a2f481c9f9f",
  "name": "tell_latest_tip",
  "auto": true,
  "contexts": [],
  "responses": [
    {
      "resetContexts": false,
      "action": "tell_latest_tip",   <----- Here
      "affectedContexts": [],
      "parameters": [],
      "messages": [
        {
          "type": 0,
          "lang": "en",
          "speech": []
        }
      ],
      "defaultResponsePlatforms": {},
      "speech": []
    }
  ],
  "priority": 500000,
  "webhookUsed": true,
  "webhookForSlotFilling": false,
  "lastUpdate": 1515003380,
  "fallbackIntent": false,
  "events": []
}

Push notifications are not sent

When making POST request to https://actions.googleapis.com/v2/conversations:send its getting below 400 error
Body

{
	"customPushMessage": {
		"target":{
			"userId": "XXXXXXXXXXXXXX",
			"intent": "save_sugar_test_result",
			 "locale": "en-US"
		},
		"userNotification": {
			"text": "Personal reminder"
		}
	},
	"isInSandbox": true
}
{
    "error": {
        "code": 400,
        "message": "Request contains an invalid argument.",
        "status": "INVALID_ARGUMENT"
    }
}

When I remove userNotification object its returning 200 ok. But notification is not triggered.
Guess https://actions.googleapis.com/v2/conversations:send endpoint was updated which needs to be updated in sample.

Also it would be helpful if there is any links to documentation on the API for https://actions.googleapis.com/v2/conversations:send there is no clue of other params or endpoints available on that REST API.

conv.arguments.get('UPDATES_USER_ID') returns undefined

I use this script to send the notifications for testing:

const request = require('request');
const {JWT} = require('google-auth-library');
const serviceAccount = require('./service-account.json');
let jwtClient = new JWT(
  serviceAccount.client_email, null, serviceAccount.private_key,
  ['https://www.googleapis.com/auth/actions.fulfillment.conversation'],
  null
);
jwtClient.authorize((authErr, tokens) => {
  let notification = {
    userNotification: {
      title: process.argv[2],
    },
    target: {
      userId: USERID,
      intent: 'tell_latest_status',
      // Expects a IETF BCP-47 language code (i.e. en-US)
      locale: 'en-US'
    },
  };
  request.post('https://actions.googleapis.com/v2/conversations:send', {
    'auth': {
      'bearer': tokens.access_token,
    },
    'json': true,
    'body': {
      'customPushMessage': notification, 'isInSandbox': true
    },
  }, (reqErr, httpResponse, body) => {
    console.log(httpResponse.statusCode + ': ' + httpResponse.statusMessage);
  });
});

It only works if I use conv.user.id as USERID.

conv.arguments.get('UPDATES_USER_ID') returns undefined, so I never managed to even get one.

when I use conv.user.profile.payload.sub as suggested here: https://developers.google.com/actions/identity/user-info the AoG API returns "SendToConversation response: Invalid user id for target."

Anonymous User Identity is deprecated starting from May 31st 2018 so conv.user.id won't work much longer, I guess.

Is there any way to make this work with Google Sign-In?
Has anyone made this work? I mean with the UPDATES_USER_ID field?

Notifications on Google Assistant display

I have this pushing notifications to my smartphone but it doesn't seem to support Google Assistant displays.

Is this something that can be coded? Or it it simply not supported at this time?

I noticed in the Device Settings it mentions notifications and they are enabled, so that makes me think this feature should be supported on the display. I am testing on the Lenovo display.

import Error in tipsDB.json

After i trid to run function addTips() in exports.restoreTipsDB
function addTips() { const tips = require('./tipsDB.json'); let batch = db.batch();

i got some error

SyntaxError: /user_code/tipsDB.json: Unexpected token } in JSON

so i fixed it
way 1.

const tips = [{
      "category": "tools",
      "url": "https:\/\/www.youtube.com\/watch?v=sjpbu4y6F_M&list=PLOU2XLYxmsILvfJcIASBDbgfxloFz_XsU&index=5",
      "tip": "If you don't know how to code, you can use templates to build an App! Learn more about it in our Build an App for the Google Assistant with Templates and No Code video.",
      "created_at": "2017-12-25T08:00:00.000Z",
    },
    {
      "category": "voice user interface",
      "url": "https:\/\/developers.google.com\/actions\/assistant\/best-practices",
      "tip": "Remember that your users may need your app to repeat itself at any time, so keep track of your last prompt and be prepared to repeat it.",
      "created_at": "2017-12-21T08:00:00.000Z",
    },];

way 2.
in index.js
function addTips() { const tips = require('./tipsDB');
in tipsDB.js

module.exports= [ {
      "category": "tools",
      "url": "https:\/\/www.youtube.com\/watch?v=sjpbu4y6F_M&list=PLOU2XLYxmsILvfJcIASBDbgfxloFz_XsU&index=5",
      "tip": "If you don't know how to code, you can use templates to build an App! Learn more about it in our Build an App for the Google Assistant with Templates and No Code video.",
      "created_at": "2017-12-25T08:00:00.000Z",
    },];

Is there way to import tipsDB.json without upper error??

+To be perfect, second tip should have "created_at" instead of "timestamp", i think. Thank you :)

//original

{
      "category": "tools",
      "url": "https:\/\/www.youtube.com\/watch?v=sjpbu4y6F_M&list=PLOU2XLYxmsILvfJcIASBDbgfxloFz_XsU&index=5",
      "tip": "If you don't know how to code, you can use templates to build an App! Learn more about it in our Build an App for the Google Assistant with Templates and No Code video.",
      "created_at": "2017-12-25T08:00:00.000Z"
    },
    {
      "category": "voice user interface",
      "url": "https:\/\/developers.google.com\/actions\/assistant\/best-practices",
      "timestamp": "2017-12-21T08:00:00.000Z",
      "tip": "Remember that your users may need your app to repeat itself at any time, so keep track of your last prompt and be prepared to repeat it."
    },

//suggestion

{
      "category": "tools",
      "url": "https:\/\/www.youtube.com\/watch?v=sjpbu4y6F_M&list=PLOU2XLYxmsILvfJcIASBDbgfxloFz_XsU&index=5",
      "tip": "If you don't know how to code, you can use templates to build an App! Learn more about it in our Build an App for the Google Assistant with Templates and No Code video.",
      "created_at": "2017-12-25T08:00:00.000Z"
    },
    {
      "category": "voice user interface",
      "url": "https:\/\/developers.google.com\/actions\/assistant\/best-practices",
      "tip": "Remember that your users may need your app to repeat itself at any time, so keep track of your last prompt and be prepared to repeat it.",
      "created_at": "2017-12-21T08:00:00.000Z"
    },

Mistake in the official document

js sample code syntax is incorrect.

https://developers.google.com/actions/assistant/updates/daily#finalize_the_subscription
https://developers.google.com/actions/assistant/updates/notifications#finalize_the_subscription

// incorrect 
appMap.set('finish.update.setup', function(app)) {
  if (app.isUpdateRegistered()) {
    app.tell("Ok, I'll start giving you daily updates.");
  } else {
    app.tell("Ok, I won't give you daily updates.");
  }
}

// correct 
appMap.set('finish.update.setup', function(app) {
  if (app.isUpdateRegistered()) {
    app.tell("Ok, I'll start giving you daily updates.");
  } else {
    app.tell("Ok, I won't give you daily updates.");
  }
})

also , Is correct both appMap and actionMap? I think actionMap is better.

Please check this 🙏 thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.