diff --git a/.env.example b/.env.example index cf4729a5..7da9b88d 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,3 @@ -APP_ENV=local APP_DEBUG=true APP_URL= APP_PORT=3000 diff --git a/.gitignore b/.gitignore index 32eef8e3..c2cbf968 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules .vscode .env +.env.* diff --git a/README.md b/README.md index 1f360869..fa2f6ea7 100644 --- a/README.md +++ b/README.md @@ -135,34 +135,14 @@ Install dependencies. npm ci ``` -Copy `.env.example` to `.env`. - -```bash -cp .env.example .env -``` - -Set the environment variables as follows: - -```env -APP_ENV=local -APP_DEBUG=true -APP_PORT=3000 - -# optional -# VERCEL_GIT_REPO_SLUG=gpt-ai-assistant -# VERCEL_ACCESS_TOKEN= -# VERCEL_DEPLOY_HOOK_URL= +### Tests -# optional -# OPENAI_API_KEY= +Copy `.env.example` to `.env.test`. -# optional -# LINE_CHANNEL_ACCESS_TOKEN= -# LINE_CHANNEL_SECRET= +```bash +cp .env.example .env.test ``` -### Tests - Run the tests. ```bash @@ -172,7 +152,7 @@ npm run test Check the results. ```bash -> gpt-ai-assistant@1.0.0 test +> gpt-ai-assistant@0.0.0 test > jest console.info @@ -180,26 +160,36 @@ Check the results. AI: 嗨!我可以怎麼幫助你? Human: 嗨? - AI: OK - - at Assistant.info [as debug] (assistant/assistant.js:55:28) - - PASS assistant/index.test.js - ✓ assistant works (1689 ms) + AI: OK! Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total -Time: 2.579 s, estimated 4 s -Ran all test suites. +Time: 1 s ``` ### Using Proxy Server +Copy `.env.example` to `.env`. + +```bash +cp .env.example .env +``` + Set the environment variables as follows: ```env -APP_ENV=production +APP_DEBUG=true +APP_PORT=3000 + +VERCEL_GIT_REPO_SLUG=gpt-ai-assistant +VERCEL_ACCESS_TOKEN= +VERCEL_DEPLOY_HOOK_URL= + +OPENAI_API_KEY= + +LINE_CHANNEL_ACCESS_TOKEN= +LINE_CHANNEL_SECRET= ``` Start a local server. @@ -214,7 +204,7 @@ Start a proxy server. ngrok http 3000 ``` -Go back to the website, modify the "Webhook URL" to e.g. "" and click the "Update" button. +Go back to the [LINE](https://developers.line.biz/) website, modify the "Webhook URL" to e.g. "" and click the "Update" button. Send a message from the LINE mobile app. @@ -231,50 +221,6 @@ Human: 嗨? AI: 很高興見到你!有什麼可以為你服務的嗎? ``` -### Using Local Server - -Start a local server. - -```bash -npm run dev -``` - -Send a dummy request to the local server. - -```bash -curl --request POST \ - --url http://localhost:3000/webhook \ - --header 'Content-Type: application/json' \ - --data '{ - "events": [ - { - "type": "message", - "source": { - "type": "user", - "userId": "000000" - }, - "message": { - "type": "text", - "text": "我是誰" - } - } - ] - }' -``` - -Check the results. - -```bash -> gpt-ai-assistant@1.0.0 dev -> node api/index.js - -=== 000000 === - -AI: 嗨!我可以怎麼幫助你? -Human: 我是誰? -AI: OK -``` - ## Changelog Detailed changes for each release are documented in the [release notes](https://github.com/memochou1993/gpt-ai-assistant/releases). diff --git a/README.zh.md b/README.zh.md index be1073ad..7d670a71 100644 --- a/README.zh.md +++ b/README.zh.md @@ -169,35 +169,15 @@ cd gpt-ai-assistant npm ci ``` -建立 `.env` 檔。 - -```bash -cp .env.example .env -``` - -設置環境變數如下: - -```env -APP_ENV=local -APP_DEBUG=true -APP_PORT=3000 - -# optional -# VERCEL_GIT_REPO_SLUG=gpt-ai-assistant -# VERCEL_ACCESS_TOKEN= -# VERCEL_DEPLOY_HOOK_URL= +### 測試 -# optional -# OPENAI_API_KEY= +建立 `.env.test` 檔。 -# optional -# LINE_CHANNEL_ACCESS_TOKEN= -# LINE_CHANNEL_SECRET= +```bash +cp .env.example .env.test ``` -### 測試 - -在終端機使用以下指令,運行測試,向 OpenAI 伺服器發送請求。 +在終端機使用以下指令,運行測試。 ```bash npm run test @@ -206,7 +186,7 @@ npm run test 查看結果。 ```bash -> gpt-ai-assistant@1.0.0 test +> gpt-ai-assistant@0.0.0 test > jest console.info @@ -214,26 +194,36 @@ npm run test AI: 嗨!我可以怎麼幫助你? Human: 嗨? - AI: - - at Assistant.info [as debug] (assistant/assistant.js:55:28) - - PASS assistant/index.test.js - ✓ assistant works (1689 ms) + AI: OK! Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total -Time: 2.579 s, estimated 4 s -Ran all test suites. +Time: 1 s ``` -### 反向代理 +### 使用代理伺服器 + +建立 `.env` 檔。 + +```bash +cp .env.example .env +``` -修改環境變數如下: +設置環境變數如下: ```env -APP_ENV=production +APP_DEBUG=true +APP_PORT=3000 + +VERCEL_GIT_REPO_SLUG=gpt-ai-assistant +VERCEL_ACCESS_TOKEN= +VERCEL_DEPLOY_HOOK_URL= + +OPENAI_API_KEY= + +LINE_CHANNEL_ACCESS_TOKEN= +LINE_CHANNEL_SECRET= ``` 在終端機使用以下指令,啟動一個本地伺服器。 @@ -248,7 +238,7 @@ npm run dev ngrok http 3000 ``` -回到 Line 平台,修改「Webhook URL」,例如「」,點選「Update」按鈕。 +回到 [LINE](https://developers.line.biz/) 平台,修改「Webhook URL」,例如「」,點選「Update」按鈕。 使用 LINE 手機應用程式發送訊息。 @@ -265,50 +255,6 @@ Human: 嗨? AI: 很高興見到你!有什麼可以為你服務的嗎? ``` -### 模擬請求 - -在終端機使用以下指令,啟動一個本地伺服器。 - -```bash -npm run dev -``` - -在另一個終端機使用以下指令,模擬 LINE 伺服器向本地伺服器發送請求,再由本地伺服器向 OpenAI 伺服器發送請求。 - -```bash -curl --request POST \ - --url http://localhost:3000/webhook \ - --header 'Content-Type: application/json' \ - --data '{ - "events": [ - { - "type": "message", - "source": { - "type": "user", - "userId": "000000" - }, - "message": { - "type": "text", - "text": "我是誰" - } - } - ] - }' -``` - -查看結果。 - -```bash -> gpt-ai-assistant@1.0.0 dev -> node api/index.js - -=== 000000 === - -AI: 嗨!我可以怎麼幫助你? -Human: 我是誰? -AI: -``` - ## 更新日誌 請到「[Releases](https://github.com/memochou1993/gpt-ai-assistant/releases)」頁面查看發布通知。 diff --git a/config/index.js b/config/index.js index ce34642a..3d0aab79 100644 --- a/config/index.js +++ b/config/index.js @@ -1,11 +1,13 @@ import dotenv from 'dotenv'; -dotenv.config(); - const { env } = process; +dotenv.config({ + path: env.NODE_ENV ? `.env.${env.NODE_ENV}` : '.env', +}); + const config = Object.freeze({ - APP_ENV: env.APP_ENV || 'production', + APP_ENV: env.NODE_ENV || 'production', APP_DEBUG: env.APP_DEBUG === 'true' || false, APP_URL: env.APP_URL || null, APP_PORT: env.APP_PORT || null,