Streamlined Event-Driven (Webhook) Script Template

Streamlined scripts allow the user to set up event-driven scripts with fewer number of steps and without going back and forth between different tools. The main difference is that webhook creation and setup are done entirely in the doOneTimeSetup() function of the script itself, requiring nothing from the user aside from running that function once during script setup, instead of asking the user to manually create a webhook and ensure the triggers are correct using another tool.
== Examples ==
Sample scripts that use the below template can be found here.
== Template ==
 /* ========================================== */
 /* [Users] Required script data to fill in    */
 /* ========================================== */
 const USER_ID = "PasteYourUserIdHere";
 const API_TOKEN = "PasteYourApiTokenHere"; // Do not share this to anyone
 const WEB_APP_URL = "PasteGeneratedWebAppUrlHere";
 
 /* ========================================== */
 /* [Users] Required customizations to fill in */
 /* ========================================== */
 // [Developers] Place all mandatory user-modified variables here
 // - e.g, skill to use, number of times to use, task to use skill on, etc.
 
 /* ========================================== */
 /* [Users] Optional customizations to fill in */
 /* ========================================== */
 // [Developers] Place all optional user-modified variables here
 // - e.g. enable/disable notifications, enable/disable script features, etc.
 
 /* ========================================== */
 /* [Users] Do not edit code below this line   */
 /* ========================================== */
 // [Developers] Place your user ID and script name here
 // - This is used for the "X-Client" HTTP header
 // - See https://habitica.fandom.com/wiki/Guidance_for_Comrades#X-Client_Header
 const AUTHOR_ID = "PasteAuthorUserIdHere";
 const SCRIPT_NAME = "TypeScriptNameHere";
 const HEADERS = {
   "x-client" : AUTHOR_ID + "-" + SCRIPT_NAME,
   "x-api-user" : USER_ID,
   "x-api-key" : API_TOKEN,
 }
 
 // [Developers] Add global variables here
 // - Note that these do not persist in between script calls
 // - If you want to save values between calls, use PropertiesService
 // - See https://developers.google.com/apps-script/reference/properties/properties-service
 
 function doOneTimeSetup() {
   // [Developers] These are one-time initial setup instructions that we'll ask
   //   the user to manually execute only once, during initial script setup
   // - Add api_createWebhook() here, already set up to activate the trigger to the
   //   event that you want to service
   // - Feel free to do all other one-time setup actions here as well
   //   e.g. creating tasks, reward buttons, etc.
 }
 
 function doPost(e) {
   // [Developers] This is the function that will be executed whenever Habitica
   //   encounters the designated event
 
   const dataContents = JSON.parse(e.postData.contents);
   const webhookType = dataContents.type;
 
   // [Developers] Add script actions here
 
   return HtmlService.createHtmlOutput();
 }
 
 // [Developers] Place all other functions below doOneTimeSetup() and doPost()
 // - Ideally prefix functions that access the API with "api_" to quickly see which ones
 //   access the API and be able budget your 30 requests per minute limit well
== Alternative ==
As an alternative to this template, the Habitica GAS Template is also available. It offers the same features, but provides more helper functions and handling of common tasks.
zh:精简的事件驱动脚本模板
Category:Extensions, Add-Ons, and Customizations