It is a LINE chatbot written in Google Apps Script (Javascript) with Google sheet as its backend. It features that it works properly without the Google Apps Script editor.
(click to expand or hide)
- All the commands in this file are run in a command prompt (terminal) in the working directory of the project in a single line per command block. To enter into the working directory, you have to
- Open a command prompt in Windows
- Open the File Explorer and copy the path of the working directory.
- Enter there by the following command (replace
<dir>
with the copied path):
cd <dir>
- Tips: If you clone this repo, you can just double-click the
enterCurrentDirectory.bat
in the file explorer of the Windows.
- After the first time of the setup, this section can be skipped for the projects in the future.
- Functionalities: Run clasp.
- Installation instructions: https://phoenixnap.com/kb/install-node-js-npm-on-windows
- Functionalities: Editing codes off line
- Installation instructions:
- Download and install VS Code from its official website.
- Make the following
jsconfig.json
file in the root of the working directory to use IntelliSense to reference stuffs among different files. (This step can be skipped if you clone this repo.){ "compilerOptions": { "module": "commonjs", "allowSyntheticDefaultImports": true, }, }
- Setup Google-apps-script word-completion in VS Code (Ref.):
- Run the command.
npm install @types/google-apps-script -g
- Run the command.
- Functionalities: Update the changes of codes on a computer to Google.
- Installation instructions (Ref.):
-
Run the command:
npm install @google/clasp -g
-
Note: It is recommended to install clasp globally with the
-g
flag as the above. As a result, there will not be duplicated 170MB files in multiple projects. -
Install inquirer if you want. (It is not necessary)
npm install inquirer -g
-
- Note: Clasp cannot be run in PowerShell.
- Functionalities: Version contorl and collaboration
- Installation instructions: Download from its official website and install it.
-
Set up things of Github
- Sign up on Github.
- Create an empty folder locally as the working directory.
- Clone the repo to the working directory with the commands (Ref.1 Ref.2). If the folder is not empty, try the commands on stack overflow.
git clone https://github.com/Rays314/LinebotSample
- Remove the commit history in the original repository.
rm -rf .git
(in Linux)
rmdir .git /S /Q
(in Windows) - Remove the readme file in the original repository. (You may want to have a link to this file in your readme file to help people set things up.)
rm README.md
(in Linux)
del README.md
(in Windows) - Create a new repository and copy the repo path (hyperlink) as
<path>
. - Create the first commit to the new repository.
git remote add origin <path>
git add .
git commit -m "Initial Commit"
git push -u origin master
-
Change the default editor of git to VS Code locally. (Ref.)
git config core.editor "code --wait"
-
Google sheet and project
- Create a Google sheet.
- Create a Google apps script project with "Extensions" -> "Apps script" in the toolbar.
- Login to Google with clasp.
clasp login
- Link to the project and download a removable
code.js
file to thesrc
folder under the working directory with the command. (Note that<script ID>
can be found in the project settings.)
clasp clone <script ID> --rootDir ./src
-
Deploy on Google with the following instructions in the classic editor (Why classic: #1)
- Switch to the classic editor by entering the script editor and click the “Use classic editor” on the upper-right corner.
- Click “Publish” on the toolbar and then "Deploy as web app..."
- Project version: New
- Description: Initial deployment
- Execute the app as: Me
- Who has access to the app: Anyone , even anonymous
- Edit files.
- Upload to Github (Why git before clasp: #3)
git add .
git commit
git push
- Upload to Google
clasp push
- Note: The above-mentioned commands can be automated by
-
ctrl + shift +
` (backtick) to open an integrated terminal in VS Code -
Run the batch file
batch\push.bat
by typing
b<tab>\p<tab><enter>
-
-
Get the
<deployment ID>
. It is followed by“@1 - Initial deployment”
from the the result of the command. (Why you should get it: #2)
clasp deployments
-
Deploy on Google
clasp deploy -d <description> -i <deployment ID>
Or equivalently, (Ref.)
clasp deploy --description <description> --deploymentId <deployment ID>
-
Note: The above-mentioned commands can be automated by
- (Do it once) Get the
<deployment ID>
and replace the two"deployment ID"
inbatch\deploy.bat
. Save the file. ctrl + shift +
` (backtick) to open an integrated terminal in VS Code- Run the batch file. (Replace
<description>
by the double-quoted (surrounded by ") ASCII description of the version to be deployed.)
batch\deploy.bat <description>
by typingb<tab>\d<tab> <description><enter>
- (Do it once) Get the
Reference tutorial: https://youtu.be/vS00zQ75xRg
- Login to LINE developers. Follow the instructions on it to create an account.
- Create a new provider (by clicking the green icon and follow it).
- Create a messaging API channel.
- Switch from the "Basic settings" tab to the "Messaging API" tab.
- In the "LINE Official Account features" section, in the row of "Allow bot to join group chats", click the "Edit" button to switch to "LINE Official Account Manager". In the section of "Toggle features", in the row of "Group and multi-person chats", switch to "Allow account to join groups and multi-person chats".
- In the section of "Account details", copy the "Basic ID" (with the @ sign). Add the bot as friend in the LINE platform. It should be able to echo a message now. Otherwise, go to the debug section.
- In the side bar, switch from "Account settings" to "Response settings". Disable "Greeting message", disable "Auto-response" and enable "Webhooks"
- Go back to the LINE Developers. In the bottom of the Messaging API tab, issue the channel access token and copy it.
- Duplicate the
SettingsTemplate.js
file in thesrc
folder and rename the duplicated one intoSettings.js
. - Paste the channel access token to the
Settings.js
file. It will be like the followig sample but with a longer token.const CHANNEL_ACCESS_TOKEN = 'jhgfdj149qdagy1j/sdhnaf=';
- In the section of "Webhook settings", edit the URL as follows. (Note that the project should have been publish on Google as a web app with the above instructions of preparations. And the
<deploy ID>
can be found with those of deploying on Google.)
https://script.google.com/macros/s/<deploy ID>/exec
- Toggle the below option into green to use webhhook.
- Deploy a new version with the above instructions of deploying on Google.
- Login to LINE Notify.
- In my registered services page, add a service by filling all blanks. Notice that the "Service URL" and "Callback URL" should be as follows.
https://script.google.com/macros/s/<deploy ID>/exec
- Verify your identify with the mail in your mailbox that you just filled in.
- Click the icon of the registered service. Copy the client ID and the client secret to the
Settings.js
file. It should be like the following sample.const LINENOTIFY = { CLIENTID: 'f5JSD2JkQWI18a', CLIENTSECRET: '4FS3FDJsuU1USi49SA53', };
- Deploy a new version with the above instructions of deploying on Google.
- Add the bot to a group and send a message of exactly
連動
. Click the icon that
When the bot does not react properly. Follow the instructions below to debug.
- Open the Google sheet linked to this project and check for the
Exe log
and theFetch log
tabs. - Use
clasp open
command to open the Apps Script editor. (Or in the tool bar of the Google sheet, click "Extensions" -> "Apps script".) Swtich to the execution tab to check the function that emits an exception. - Save and deploy a new version to Google.
- Insert
logImmediately
function to some potential function to log the info to theExe log
sheet. - Follow the function-call stack and check for the newest added codes.