{"id":23139039,"url":"https://github.com/circuit/trivia","last_synced_at":"2025-08-26T06:33:07.360Z","repository":{"id":40752182,"uuid":"159419148","full_name":"circuit/trivia","owner":"circuit","description":"A trivia game for Circuit written with Google Cloud Functions, Google Cloud Datastore, DialogFlow and the Circuit REST API.","archived":false,"fork":false,"pushed_at":"2022-12-02T17:40:38.000Z","size":1034,"stargazers_count":1,"open_issues_count":14,"forks_count":2,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-04-04T10:11:29.705Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/circuit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-28T00:23:45.000Z","updated_at":"2020-09-11T12:12:35.000Z","dependencies_parsed_at":"2023-01-22T22:45:57.292Z","dependency_job_id":null,"html_url":"https://github.com/circuit/trivia","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/circuit/trivia","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/circuit%2Ftrivia","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/circuit%2Ftrivia/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/circuit%2Ftrivia/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/circuit%2Ftrivia/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/circuit","download_url":"https://codeload.github.com/circuit/trivia/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/circuit%2Ftrivia/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272186211,"owners_count":24888333,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-26T02:00:07.904Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-17T13:13:09.084Z","updated_at":"2025-08-26T06:33:07.311Z","avatar_url":"https://github.com/circuit.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Trivia\n\nA trivia game for Circuit written with [Google Cloud Functions](https://cloud.google.com/functions/docs/), [Google Cloud Datastore](https://cloud.google.com/datastore/docs/), [DialogFlow](https://dialogflow.com/docs) and the [Circuit REST API](https://circuitsandbox.net/rest/v2/swagger/ui/index.html).\n\n## Usage\nAdd 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.\n\nFor example: \u003ci\u003e\u003ca href=\"\"\u003e@Trivia Bot\u003c/a\u003e ask an easy question in sports\u003c/i\u003e\n\nAny participant can answer the posted question. After 20 seconds the correct answer and the winners are shown.\n\n### Screenshots\n\u003ckbd\u003e\u003cimg src=\"images/question.png\" width=\"425px\"\u003e\u003c/kbd\u003e\n\u003ckbd\u003e\u003cimg src=\"images/question2.png\" width=\"425px\"\u003e\u003c/kbd\u003e\n\u003ckbd\u003e\u003cimg src=\"images/answer.png\" width=\"425px\"\u003e\u003c/kbd\u003e\n\u003ckbd\u003e\u003cimg src=\"images/stats.png\" width=\"425px\"\u003e\u003c/kbd\u003e\n\u003ckbd\u003e\u003cimg src=\"images/categories.png\" width=\"425px\"\u003e\u003c/kbd\u003e\n\n## Technical Overview\nThe cloud functions \u003cb\u003estart\u003c/b\u003e and \u003cb\u003estop\u003c/b\u003e are used to register/unregister the Circuit webhooks \u003ccode\u003eCONVERSATION.ADD_ITEM\u003c/code\u003e and \u003ccode\u003eUSER.SUBMIT_FORM_DATA\u003c/code\u003e via the Circuit REST API.\n\nThe cloud function \u003cb\u003ewebhook\u003c/b\u003e 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](https://dialogflow.com/docs/sdks). The official [DialogFlow Node.js Client](https://www.npmjs.com/package/dialogflow) is used.\n\nDialogFlow then returns the matched intent. The category and difficulty are provided to the function if DialogFlow  was able to recognize them.\n\nIf the intent is \u003cb\u003eNew Question\u003c/b\u003e the cloud function performs a REST API call to [opentdb.com](https://opentdb.com/api.php) to find a corresponding question. If the user does not specify a difficulty and category, random values are used.\n\nThe cloud function then posts the question in the conversation using the new Circuit SDK Forms feature, and adds the question to Cloud Datastore.\n\nWhen a user submits an answer the same cloud function \u003cb\u003ewebook\u003c/b\u003e 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.\n\nAfter the 20s timeout, the \u003cb\u003ewebhook\u003c/b\u003e function looks up the submissions and posts a reply with the correct answer and the winners.\n\nThe \u003cb\u003ewebhook\u003c/b\u003e cloud function also handles the intents to show statistics and to list the categories.\n\n\n## Run locally for development\n\n### Prerequisites\n* [Cloud Function Emulator](https://cloud.google.com/functions/docs/emulator) to run the functions locally\n* [Cloud Datastore Emulators](https://cloud.google.com/datastore/docs/tools/datastore-emulator) to store the data locally\n* [ngrok](https://ngrok.com/) or a similar tool is required to expose your local functions as public URLs so they can be registered for Circuit webhooks\n\n### Deploy locally\nFollow the same steps as in cloud deploy below. If you followed the emulator installation steps above, the deployment will be to your local emulators.\n\n* \u003ccode\u003egcloud beta emulators datastore start\u003c/code\u003e\n* export the env variables, e.g. \u003ccode\u003eexport DOMAIN=https://circuitsandbox.net\u003c/code\u003e\n* \u003ccode\u003efunctions config set projectId \u0026lt;your project id\u003e\u003c/code\u003e\n* \u003ccode\u003efunctions start\u003c/code\u003e\n* \u003ccode\u003ecd manage;functions deploy start --env-vars-file ../.env.yaml --trigger-http;cd ..\u003c/code\u003e\n* \u003ccode\u003ecd manage;functions deploy stop --env-vars-file ../.env.yaml --trigger-http;cd ..\u003c/code\u003e\n* \u003ccode\u003ecd webhook;functions deploy webhook --env-vars-file ../.env.yaml --trigger-http;cd ..\u003c/code\u003e\n\n### Debugging\nUse vscode to debug, but before starting the debugger you need to start the functions emulator debugger via \u003ccode\u003efunctions inspect webhook\u003c/code\u003e for debugging the webhook function.\n\n## Deploy to Google Cloud\n\n### Prerequisites\n* gcloud account with billing enabled\n\n### Deploy\n1. Create gcloud project and enable the [Cloud Functions API](https://console.cloud.google.com/functions)\n1. Clone this repo\n1. Authorize gcloud to access the Cloud Platform via: \u003ccode\u003egcloud auth login\u003c/code\u003e\n1. Set the project via: \u003ccode\u003egcloud config set project my-trivia-game\u003c/code\u003e (where my-trivia-game is your project name)\n1. Setup a service account for authenticating the cloud APIs\nhttps://cloud.google.com/docs/authentication/getting-started\n1. Create a [dialogFlow](https://console.dialogflow.com) project and import trivia.zip\n1. Rename \u003ccode\u003e.env.yaml.template\u003c/code\u003e to \u003ccode\u003e.env.yaml\u003c/code\u003e and update it with your configuration\n1. Deploy the Datastore indexes via: \u003ccode\u003egcloud app deploy index.yaml\u003c/code\u003e\n1. Deploy the functions via: \u003ccode\u003ecd manage;gcloud beta functions deploy start --runtime nodejs8 --env-vars-file ../.env.yaml --trigger-http\u003c/code\u003e\n1. Deploy the remaining functions in the same way\n1. Navigate to the start function to register the webhooks, e.g. https://us-central1-my-trivia-game.cloudfunctions.net/start\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcircuit%2Ftrivia","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcircuit%2Ftrivia","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcircuit%2Ftrivia/lists"}