Skip to content

Commit

Permalink
Remove dangerous commands
Browse files Browse the repository at this point in the history
  • Loading branch information
memochou1993 committed Dec 14, 2022
1 parent 40e8886 commit 276af9c
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 86 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ GPT AI Assistant 是基於 OpenAI API 與 LINE Messaging API 實作的範例應
名稱 | 說明
--- | ---
`/version` | 取得版本資訊
`/env` | 取得環境變數
`/env KEY=VALUE` | 設置環境變數

## 環境變數

Expand Down
17 changes: 0 additions & 17 deletions assistant/assistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import Prompt from './prompt.js';
class Assistant {
version;

owner;

prompts = new Map();

constructor() {
Expand All @@ -45,24 +43,13 @@ class Assistant {
* @returns {Object}
*/
async handleEvent(event) {
if (!this.owner) this.setOwner(event.userId);
if (event.isCommandGetVersion) {
event.pushReply(this.version);
if (this.version !== (await fetchVersion())) {
event.pushReply('A new version of GPT AI Assistant is available. Please update source code.');
}
return event;
}
if (event.userId === this.owner) {
if (event.isCommandGetEnv) {
event.handleCommandGetEnv(config);
return event;
}
if (event.isCommandSetEnv) {
event.handleCommandSetEnv(config);
return event;
}
}
try {
const prompt = this.getPrompt(event.userId);
prompt.write(`${PARTICIPANT_HUMAN}: ${event.text}?`);
Expand All @@ -77,10 +64,6 @@ class Assistant {
}
}

setOwner(userId) {
this.owner = userId;
}

/**
* @param {string} userId
* @returns {Prompt}
Expand Down
25 changes: 0 additions & 25 deletions assistant/event.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
COMMAND_GET_VERSION,
COMMAND_GET_ENV,
COMMAND_SET_ENV,
} from '../constants/command/index.js';

class Event {
Expand Down Expand Up @@ -29,29 +27,6 @@ class Event {
return this.text.toLowerCase() === COMMAND_GET_VERSION;
}

get isCommandGetEnv() {
return this.text.toLowerCase() === COMMAND_GET_ENV;
}

get isCommandSetEnv() {
return this.text.toLowerCase().startsWith(COMMAND_SET_ENV);
}

get getCommandArgs() {
return this.text.slice(COMMAND_SET_ENV.length).trim().split('=');
}

handleCommandGetEnv(config) {
this.pushReply(`${JSON.stringify(config, null, 2)}`);
}

handleCommandSetEnv(config) {
const [key, value] = this.getCommandArgs;
config.set(key, value);
this.pushReply(`${key}=${config.get(key)}`);
return this;
}

pushReply(reply) {
this.replies.push(reply);
}
Expand Down
40 changes: 0 additions & 40 deletions assistant/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {
} from '../services/line/index.js';
import {
COMMAND_GET_VERSION,
COMMAND_GET_ENV,
COMMAND_SET_ENV,
} from '../constants/command/index.js';
import Assistant from './assistant.js';

Expand Down Expand Up @@ -73,41 +71,3 @@ test('get version command works', async () => {
]);
expect(actual).toEqual(expected);
}, TIMEOUT);

test('get env command works', async () => {
const assistant = new Assistant();
const events = createEvents([COMMAND_GET_ENV]);
let actual;
try {
actual = await assistant.handleEvents(events);
} catch (err) {
console.error(err.toJSON());
}
const expected = expect.arrayContaining([
expect.objectContaining({
replies: expect.arrayContaining([
`${JSON.stringify(config, null, 2)}`,
]),
}),
]);
expect(actual).toEqual(expected);
}, TIMEOUT);

test('set env command works', async () => {
const assistant = new Assistant();
const events = createEvents([`${COMMAND_SET_ENV} APP_PORT=80`]);
let actual;
try {
actual = await assistant.handleEvents(events);
} catch (err) {
console.error(err.toJSON());
}
const expected = expect.arrayContaining([
expect.objectContaining({
replies: expect.arrayContaining([
'APP_PORT=80',
]),
}),
]);
expect(actual).toEqual(expected);
}, TIMEOUT);
2 changes: 0 additions & 2 deletions constants/command/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export const COMMAND_GET_VERSION = '/version';
export const COMMAND_GET_ENV = '/env';
export const COMMAND_SET_ENV = '/env ';

export default null;

0 comments on commit 276af9c

Please sign in to comment.