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.
The cloud functions start and stop are used to register/unregister the Circuit webhooks
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
- Cloud Function Emulator to run the functions locally
- Cloud Datastore Emulators to store the data locally
- ngrok or a similar tool is required to expose your local functions as public URLs so they can be registered for Circuit webhooks
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.
functions config set projectId <your project id>
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
- Create gcloud project and enable the Cloud Functions API
- Clone this repo
- Authorize gcloud to access the Cloud Platform via:
gcloud auth login
- Set the project via:
gcloud config set project my-trivia-game(where my-trivia-game is your project name)
- Setup a service account for authenticating the cloud APIs https://cloud.google.com/docs/authentication/getting-started
- Create a dialogFlow project and import trivia.zip
.env.yamland update it with your configuration
- Deploy the Datastore indexes via:
gcloud app deploy index.yaml
- Deploy the functions via:
cd manage;gcloud beta functions deploy start --runtime nodejs8 --env-vars-file ../.env.yaml --trigger-http
- Deploy the remaining functions in the same way
- Navigate to the start function to register the webhooks, e.g. https://us-central1-my-trivia-game.cloudfunctions.net/start