Habitica Wiki
注册
Habitica Wiki
(翻译中)
标签源代码编辑
(翻译中)
标签源代码编辑 移动版编辑 移动版网页编辑
第50行: 第50行:
 
#依次点击“Publish(发布)”、“Deploy as web app...(部署为web应用程序……)”{{Clr}}[[File:Screenshot_DeployAsWebApp.png|thumb|left|216px]]{{Clr}}
 
#依次点击“Publish(发布)”、“Deploy as web app...(部署为web应用程序……)”{{Clr}}[[File:Screenshot_DeployAsWebApp.png|thumb|left|216px]]{{Clr}}
 
#在“Project version:(项目版本:)”下,输入“Added script(添加的脚本)”{{Clr}}[[File:Screenshot_ProjectVersion.png|thumb|left|359px]]{{Clr}}
 
#在“Project version:(项目版本:)”下,输入“Added script(添加的脚本)”{{Clr}}[[File:Screenshot_ProjectVersion.png|thumb|left|359px]]{{Clr}}
#Under “Execute the app as:”, make sure it says “Me”{{Clr}}[[File:Screenshot_ExecuteTheAppAs.png|thumb|left|222px]]{{Clr}}
+
#确保“Execute the app as:(以何种身份执行此应用程序:)”下显示“Me(我)”{{Clr}}[[File:Screenshot_ExecuteTheAppAs.png|thumb|left|222px]]{{Clr}}
 
#Under “Who has access to the app:”, make sure it says “Anyone, even anonymous”{{Clr}}[[File:Screenshot_WhoHasAccessToTheApp.png|thumb|left|193px]]{{Clr}}
 
#Under “Who has access to the app:”, make sure it says “Anyone, even anonymous”{{Clr}}[[File:Screenshot_WhoHasAccessToTheApp.png|thumb|left|193px]]{{Clr}}
 
#Click “Deploy”{{Clr}}[[File:Button_Deploy.png|thumb|left|79px]]​​​​​​{{Clr}}
 
#Click “Deploy”{{Clr}}[[File:Button_Deploy.png|thumb|left|79px]]​​​​​​{{Clr}}

2021年8月3日 (二) 00:18的版本

手动运行时间驱动的脚本相比,事件驱动(webhook)脚本所需安装步骤更多。用户需要依次安装两部分内容:

  1. 其一,处理Habitica发送的自动请求的外部脚本。
  2. 其二,只要指定事件发生便会触发Habitica发送自动请求的触发器,即“webhook”。

本指南将逐步说明如何完成这两个部分。

安装精简脚本

精简脚本是使用精简的事件驱动脚本模板的脚本。用户可以借助这些脚本使用较少的步骤安装事件驱动脚本,而无需在不同工具之间来回切换。

第1部分:安装外部脚本

  1. 前往https://script.google.com
    • 初次访问该网站,请点击“Start Scripting(开始编写脚本)”
      Button StartScripting
    • 如果出现谷歌登录页面的提示,请输入你的Gmail地址和密码
  2. 点击屏幕左上角的“New Project(新建项目)”
    Button NewProject
    • 将会打开一个包含新项目的新选项卡
  3. 点击最右侧的“Use legacy editor(使用旧版编辑器)”
  4. 关闭弹窗“Editor Opt-out Survey(编辑器退出调查)”
  5. 如果提示“Try the brand new Apps Script editor(尝试全新应用程序脚本编辑器)”,请点击“Don't ask me again(不再询问)”
  6. 依次点击“Edit(编辑)”、“Select all(全选)”
    Screenshot SelectAll
  7. 按下键盘上的Delete键,删除整个myFunction示例函数
    • 此时脚本应为完全空白
  8. 点击精简的事件驱动脚本列表中你想要安装的脚本名称
  9. 点击“代码”章节的链接
    • 将会打开一个包含代码的新选项卡
  10. 复制上一步所打开的新选项卡的所有文本
  11. 返回包含空白项目的选项卡(前面的步骤2所创建的选项卡),然后粘贴复制的文本
  12. 前往https://habitica.com/user/settings/api​​​​​​​​​
  13. 找到屏幕左侧的“用户ID”
    Screenshot UserId
  14. 复制整串数字和字母
    • 包括连字符在内,复制整串字符
  15. 选中新脚本所在选项卡第4行的“PasteYourUserIdHere(在此粘贴用户ID)”(译者注:也有可能是选中“#HabiticaUserID#”等类似文本,而该文本不一定位于第4行)
    • 请勿选中引号,从而确保粘贴时不会将其覆盖
      Screenshot PasteYourUserIdHere
  16. 粘贴前面步骤中复制的用户ID
    • 请注意,引号应仍保留在该处
      Screenshot PastedUserId
  17. 返回https://habitica.com/user/settings/api​​​​​​​​​​​​
  18. 点击“显示API令牌”
    Screenshot ShowApiToken
    • 将会显示第二组数字和字母
      Screenshot ApiToken
  19. 复制整串数字和字母
    • 包括连字符在内,复制整串字符
    • 请注意:请勿向任何人展示/分享此API令牌,请参阅下面的警告:
      Screenshot ApiTokenWarning

      图片翻译:这些字符可用于复制到第三方应用程序。但请注意,你的API令牌相当于密码,有时候别人会向你索要用户ID,但是永远不要把你的API令牌分享给其他人,包括Github。

  20. 选中新脚本所在选项卡第5行的“PasteYourApiTokenHere(在此粘贴API令牌)”(译者注:也有可能是选中“#HabiticaAPIToken#”等类似文本,而该文本不一定位于第5行)
    • 请勿选中引号,从而确保粘贴时不会将其覆盖
      Screenshot PasteYourApiTokenHere
  21. 粘贴前面步骤中复制的API令牌
    • 请注意,引号应仍保留在该处
      Screenshot PastedApiToken
  22. 如果脚本页面的描述包含了“Required Customizations(必要定制)”章节,那么请检查该章节所列条目,并修改指定数值
  23. [Optional]如果脚本描述包含了“Optional Customizations(可选定制)”章节,那么请检查该章节所列条目,并修改指定数值
  24. 依次点击“File(文件)”、“Save(保存)”
    Screenshot Save
  25. 要求输入项目名称时,只需输入脚本页面的脚本名称即可,然后点击“OK”
    Screenshot EditProjectName
  26. 依次点击“Publish(发布)”、“Deploy as web app...(部署为web应用程序……)”
    Screenshot DeployAsWebApp
  27. 在“Project version:(项目版本:)”下,输入“Added script(添加的脚本)”
    Screenshot ProjectVersion
  28. 确保“Execute the app as:(以何种身份执行此应用程序:)”下显示“Me(我)”
    Screenshot ExecuteTheAppAs
  29. Under “Who has access to the app:”, make sure it says “Anyone, even anonymous”
    Screenshot WhoHasAccessToTheApp
  30. Click “Deploy”
    Button Deploy
    ​​​​​​
  31. If the following message is displayed, click on “Review Permissions”
    Screenshot AuthorizationsRequired
    • A new window will open
  32. Choose the Google account you used in step 1
    Screenshot ChooseAnAccount
  33. On the lower left, click “Advanced”, then click “Go to <name of script> (unsafe)”
    • The app being mentioned here that isn’t verified is the new project you made
      Screenshot ThisAppIsntVerified
  34. On the lower right, click “Allow”
    Screenshot WantsToAccessYourGoogleAccount
  35. Click “OK”
    Screenshot DeployAsAWebApp

The first part is now done. Part 2 below is shorter and also needs to be performed.

Part 2. Set Up Habitica Webhook

  1. Click on “Publish”, then “Deploy as web app...”
    Screenshot DeployAsWebApp
  2. Select the complete URL under “Current web app URL:”, then copy
    Screenshot DeployAsWebAppCopy
  3. Click “Cancel”
    Button Cancel
  4. On line number 6, select PasteGeneratedWebAppUrlHere​​​​​​
    • Don’t select the quotation marks. This will make sure they are not overwritten when you paste.
      Screenshot PasteGeneratedWebAppUrlHere
  5. Paste the URL copied from the previous steps
    • Note that the quotation marks should still be there
      Screenshot PastedWebAppUrl
  6. Click on “Run”, then “Run function”, then “doOneTimeSetup”
    Screenshot doOneTimeSetup
    • ​​​A yellow message will momentarily pop up on the top of your screen, no need to click anything
      Screenshot RunningDoOneTimeSetup
    • If either "doOneTimeSetup" is not on the choices, or a red error message is shown, it could mean that one of the previous steps was not done properly. Please repeat the steps starting from Part 1. Set Up External Script
  7. [Optional] Go to https://habitica.com/ and click on the Sync button in case the script creates reward task buttons

If the yellow message on step 6 simply disappears with no red error message shown, you’ve successfully completed all the steps! Enjoy your new script!

If you have any questions or concerns regarding unexpected behavior encountered while setting up or using scripts, please go to the scripts troubleshooting page.

YouTube step by step instructions (Setting auto-accept quests on this example)


Setting Up Traditional Scripts

Part 1. Set Up External Script

Setting up the external script for traditional scripts is similar to the steps for streamlined scripts. The only difference is that the lines where you need to put your user ID, API token, and any other user-defined customizations may look a little bit different. Reading the the script's accompanying documentation will help.

Part 2. Set Up Habitica Webhook

Traditionally, to set up a webhook, you need to learn how to use some third-party tools and set up the webhook manually using these tools.

Applying Updates to Streamlined Scripts

There are times when an author updates their script, either to fix some problems or to add new features. The sections below show the three parts that possibly need to be done to apply these updates on your deployment of the script. For each particular script update, the author of the script will indicate which part among these three parts below are necessary to apply that specific update.

Part 1. Delete Items Created by doOneTimeSetup

  1. Go to https://script.google.com and open the script you want to update
  2. Take note of the URL on line 6
  3. Go to https://habitica.com/user/settings/api
  4. On the right side, under Webhooks, look for all the lines with the same URL as the one on step 2 above
    • You may need to press the right arrow key on your keyboard several times to see the entire URL
    • There could be more than one line with this URL, for the case of scripts which use more than one webhook
  5. Click "Delete" on all those lines with matching URL
    • Note: There will never be a need for you to manually add a URL here in later steps
  6. Go to https://habitica.com
  7. If the script created some reward buttons before when you were initially setting it up, delete all of these created reward buttons

Part 2. Update External Script

  1. Click on the name of the script you want to update from the list of Streamlined Event-Driven Scripts
  2. On the section that says "Code", click on the link
    • A new tab will open with the code
  3. Copy all the text below the line that says /* [Users] Required customizations to fill in */
  4. Go to https://script.google.com and open the script you want to update
  5. Delete all the text below the line that says /* [Users] Required customizations to fill in */
  6. Paste the text copied from step 3
  7. Only if lines 4-6 have "habiticaUserId", "habiticaApiToken", and "webAppUrl", replace them with "USER_ID", "API_TOKEN", and "WEB_APP_URL" respectively.
  8. If the section called "Required Customizations" exist on the script's description on the scripts page, check through the items listed on this section and modify the indicated values
  9. [Optional] If the section called "Optional Customizations" exist on the script's description, check through the items listed on this section and modify the indicated values
  10. Click on “File”, then “Save”
  11. Click on “Publish”, then “Deploy as web app...”
  12. Under “Project version:”, type “Updated script”
  13. Click “Update”
  14. If the "Authorization required" message is displayed, click on “Review Permissions”
    • If the "Authorization required" message is not displayed, just go directly to step 16
  15. Choose the Google account you used when you initially set up the script
  16. On the lower left, click “Advanced”, then click “Go to <name of script> (unsafe)”
  17. On the lower right, click “Allow”
  18. Click “OK”

Part 3. Recreate Items Created by doOneTimeSetup

  1. Click on “Run”, then “Run function”, then “doOneTimeSetup”
    • ​​​A yellow message will momentarily pop up on the top of your screen, no need to click anything
  2. [Optional] Go to https://habitica.com/ and click on the Sync button in case the script creates reward task buttons

If you have completed the steps in all the parts indicated by the script's author, you have successfully updated the script.

If you have any questions or concerns regarding unexpected behavior encountered while setting up or using scripts, please go to the scripts troubleshooting page.