A trivia game for Circuit written with Google Cloud Functions, Google Cloud Datastore, DialogFlow and the Circuit REST API.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.



A trivia game for Circuit written with Google Cloud Functions, Google Cloud Datastore, DialogFlow and the Circuit REST API.


Add the trivia bot to a conversation. Then any participant can ask the bot to post a new trivia question, list the categories, show the stats and more.

For example: @Trivia Bot ask an easy question in sports

Any participant can answer the posted question. After 20 seconds the correct answer and the winners are shown.


Technical Overview

The cloud functions start and stop are used to register/unregister the Circuit webhooks CONVERSATION.ADD_ITEM and USER.SUBMIT_FORM_DATA via the Circuit REST API.

The cloud function webhook is called whenever a text item is posted in a conversation the bot is a member of. This function checks if the bot is mentioned, and if so passes the text content (utterance) to DialogFlow via the DialogFlow SDK. The official DialogFlow Node.js Client is used.

DialogFlow then returns the matched intent. The category and difficulty are provided to the function if DialogFlow was able to recognize them.

If the intent is New Question the cloud function performs a REST API call to opentdb.com to find a corresponding question. If the user does not specify a difficulty and category, random values are used.

The cloud function then posts the question in the conversation using the new Circuit SDK Forms feature, and adds the question to Cloud Datastore.

When a user submits an answer the same cloud function webook is called. If the timeout hasn't been reached and the user hasn't already submitted an answer to this question, then the submission is added to Cloud Datastore.

After the 20s timeout, the webhook function looks up the submissions and posts a reply with the correct answer and the winners.

The webhook cloud function also handles the intents to show statistics and to list the categories.

Run locally for development


Deploy locally

Follow the same steps as in cloud deploy below. If you followed the emulator installation steps above, the deployment will be to your local emulators.

  • gcloud beta emulators datastore start
  • export the env variables, e.g. export DOMAIN=https://circuitsandbox.net
  • functions config set projectId <your project id>
  • functions start
  • cd manage;functions deploy start --env-vars-file ../.env.yaml --trigger-http;cd ..
  • cd manage;functions deploy stop --env-vars-file ../.env.yaml --trigger-http;cd ..
  • cd webhook;functions deploy webhook --env-vars-file ../.env.yaml --trigger-http;cd ..


Use vscode to debug, but before starting the debugger you need to start the functions emulator debugger via functions inspect webhook for debugging the webhook function.

Deploy to Google Cloud


  • gcloud account with billing enabled


  1. Create gcloud project and enable the Cloud Functions API
  2. Clone this repo
  3. Authorize gcloud to access the Cloud Platform via: gcloud auth login
  4. Set the project via: gcloud config set project my-trivia-game (where my-trivia-game is your project name)
  5. Setup a service account for authenticating the cloud APIs https://cloud.google.com/docs/authentication/getting-started
  6. Create a dialogFlow project and import trivia.zip
  7. Rename .env.yaml.template to .env.yaml and update it with your configuration
  8. Deploy the Datastore indexes via: gcloud app deploy index.yaml
  9. Deploy the functions via: cd manage;gcloud beta functions deploy start --runtime nodejs8 --env-vars-file ../.env.yaml --trigger-http
  10. Deploy the remaining functions in the same way
  11. Navigate to the start function to register the webhooks, e.g. https://us-central1-my-trivia-game.cloudfunctions.net/start