Skip to content

Commit f51dcc2

Browse files
authored
Merge pull request #12 from BLazzeD21/fix-bugs-and-refactor-code
Bug fixes and code refactorings
2 parents f9007f8 + 62df121 commit f51dcc2

File tree

12 files changed

+195
-27
lines changed

12 files changed

+195
-27
lines changed

.dockerignore

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
1-
README.md
1+
# Logs
2+
logs
3+
*.log
4+
5+
# Git files
6+
.git
7+
8+
# Markdown
9+
*.md
10+
11+
# Dependency directory
212
node_modules
13+
14+
# Configuration files
315
config/default.json
416
config/config.md
5-
/img
17+
18+
# File with instructions required to create a container image
19+
Dockerfile
20+
21+
# File with a set of instructions for building and launching
22+
Makefile
23+
24+
# License
25+
LICENSE
26+
27+
# Linter configuration
28+
.eslintrc.cjs
29+
30+
# Folder with images for the bot
31+
img

.gitignore

Lines changed: 133 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,135 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
.pnpm-debug.log*
9+
10+
# Diagnostic reports (https://nodejs.org/api/report.html)
11+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
12+
13+
# Runtime data
14+
pids
15+
*.pid
16+
*.seed
17+
*.pid.lock
18+
19+
# Directory for instrumented libs generated by jscoverage/JSCover
20+
lib-cov
21+
22+
# Coverage directory used by tools like istanbul
23+
coverage
24+
*.lcov
25+
26+
# nyc test coverage
27+
.nyc_output
28+
29+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
30+
.grunt
31+
32+
# Bower dependency directory (https://bower.io/)
33+
bower_components
34+
35+
# node-waf configuration
36+
.lock-wscript
37+
38+
# Compiled binary addons (https://nodejs.org/api/addons.html)
39+
build/Release
40+
41+
# Dependency directories
42+
node_modules/
43+
jspm_packages/
44+
45+
# Snowpack dependency directory (https://snowpack.dev/)
46+
web_modules/
47+
48+
# TypeScript cache
49+
*.tsbuildinfo
50+
51+
# Optional npm cache directory
52+
.npm
53+
54+
# Optional eslint cache
55+
.eslintcache
56+
57+
# Optional stylelint cache
58+
.stylelintcache
59+
60+
# Microbundle cache
61+
.rpt2_cache/
62+
.rts2_cache_cjs/
63+
.rts2_cache_es/
64+
.rts2_cache_umd/
65+
66+
# Optional REPL history
67+
.node_repl_history
68+
69+
# Output of 'npm pack'
70+
*.tgz
71+
72+
# Yarn Integrity file
73+
.yarn-integrity
74+
75+
# dotenv environment variable files
76+
.env
77+
.env.development.local
78+
.env.test.local
79+
.env.production.local
80+
.env.local
81+
82+
# parcel-bundler cache (https://parceljs.org/)
83+
.cache
84+
.parcel-cache
85+
86+
# Next.js build output
87+
.next
88+
out
89+
90+
# Nuxt.js build / generate output
91+
.nuxt
92+
dist
93+
94+
# Gatsby files
95+
.cache/
96+
# Comment in the public line in if your project uses Gatsby and not Next.js
97+
# https://nextjs.org/blog/next-9-1#public-directory-support
98+
# public
99+
100+
# vuepress build output
101+
.vuepress/dist
102+
103+
# vuepress v2.x temp and cache directory
104+
.temp
105+
.cache
106+
107+
# Docusaurus cache and generated files
108+
.docusaurus
109+
110+
# Serverless directories
111+
.serverless/
112+
113+
# FuseBox cache
114+
.fusebox/
115+
116+
# DynamoDB Local files
117+
.dynamodb/
118+
119+
# TernJS port file
120+
.tern-port
121+
122+
# VSCode
123+
.vscode-test
1124
.vscode
2-
node_modules
125+
126+
# yarn v2
127+
.yarn/cache
128+
.yarn/unplugged
129+
.yarn/build-state.yml
130+
.yarn/install-state.gz
131+
.pnp.*
132+
133+
# Configuration files
3134
config/default.json
4-
config/production.json
5-
voices/
135+
config/production.json

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node.js-chatgpt-bot",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "\"\"",
55
"main": "index.js",
66
"type": "module",
@@ -14,7 +14,7 @@
1414
},
1515
"keywords": [],
1616
"author": "",
17-
"license": "ISC",
17+
"license": "MIT License",
1818
"bugs": {
1919
"url": "https://github.com/BLazzeD21/Node.js-ChatGPT-Bot/issues"
2020
},

src/converter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class OggConverter {
4242

4343
async create(url, filename) {
4444
try {
45-
const oggPath = resolve(__dirname, '../voices', `${filename}.ogg`);
45+
const oggPath = resolve(__dirname, 'voices', `${filename}.ogg`);
4646
const response = await axios.get(url, {
4747
responseType: 'stream',
4848
});

src/handlers/errorHandler.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { menuKeyboard } from '../keyboards/keyboards.js';
44
class ErrorHandler {
55
responseError(ctx, handler) {
66
return (error) => {
7-
console.log(`${error.name} ${handler}: ${error.message}`,
7+
console.log(`${ctx.from.id} - ${error.name} ${handler}: ${error.message}`,
88
menuKeyboard);
99
ctx.reply(
1010
`${LEXICON_EN['noResponce']}\n\n${error.name}: ${error.message}`,
11+
{ disable_web_page_preview: true },
1112
);
1213
};
1314
}

src/handlers/openaiHandlers.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { LEXICON_EN } from '../lexicon/lexicon_en.js';
33

44
import { openai } from '../openai.js';
55
import { converter } from '../converter.js';
6+
import { deleteFile } from '../utils/deleteFile.js';
67

78
import { menuKeyboard } from '../keyboards/keyboards.js';
89
import { createInitialSession } from '../utils/createSession.js';
@@ -65,8 +66,8 @@ class OpenAIHandlers {
6566
const sessionId = ctx.message.chat.id;
6667
sessions[sessionId] ??= createInitialSession();
6768

68-
const processing = await ctx.reply(
69-
code(LEXICON_EN['processingVoice']),
69+
const processingTranscription = await ctx.reply(
70+
code(LEXICON_EN['processingTranscription']),
7071
menuKeyboard);
7172

7273
ctx.sendChatAction('typing');
@@ -80,6 +81,10 @@ class OpenAIHandlers {
8081
openai
8182
.transcription(mp3Path)
8283
.then(async (text) => {
84+
const processingVoice = await ctx.reply(
85+
code(LEXICON_EN['processingVoice']),
86+
menuKeyboard);
87+
8388
sessions[sessionId].messages.push({
8489
role: openai.roles.USER,
8590
content: text,
@@ -88,11 +93,15 @@ class OpenAIHandlers {
8893
openai
8994
.chat(sessions[sessionId].messages)
9095
.then(this.sendResponse(ctx, sessions, sessionId))
91-
.catch(ErrorHandler.responseError(ctx, 'transcription'));
96+
.catch(ErrorHandler.responseError(ctx, 'transcription'))
97+
.finally(async () => {
98+
await ctx.deleteMessage(processingVoice.message_id);
99+
});
92100
})
93101
.catch(ErrorHandler.responseError(ctx, 'voiceHandler'))
94102
.finally(async () => {
95-
await ctx.deleteMessage(processing.message_id);
103+
await deleteFile(mp3Path);
104+
await ctx.deleteMessage(processingTranscription.message_id);
96105
});
97106
};
98107
};
@@ -109,6 +118,7 @@ class OpenAIHandlers {
109118
const requestText = ctx.message.text.replace('/image', '').trim();
110119

111120
if (!requestText) {
121+
await ctx.deleteMessage(processing.message_id);
112122
await ctx.reply(LEXICON_EN['empty'], { parse_mode: 'HTML' });
113123
return;
114124
}

src/handlers/userHandlers.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ class UserHandlers {
1313
return async (ctx) => {
1414
const sessionId = ctx.message.chat.id;
1515
sessions[sessionId] = createInitialSession();
16-
await ctx.reply(LEXICON_EN['start'], menuKeyboard);
16+
await ctx.reply(LEXICON_EN['start'],
17+
{ parse_mode: 'Markdown' },
18+
menuKeyboard,
19+
);
1720
};
1821
};
1922

src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ bot.command('show', AdminHandlers.showHandler(config));
5555

5656
bot.command('new', UserHandlers.newHandler(config, store));
5757
bot.command('help', UserHandlers.helpHandler(config));
58-
bot.command('chatid', UserHandlers.chatIDHandler());
5958
bot.command('password', UserHandlers.passwordHandler());
6059
bot.command('image', OpenAIHandlers.imageHandler(config));
6160

src/lexicon/lexicon_en.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ export const commands = [
33
{ command: 'new', description: 'Open a new session' },
44
{ command: 'help', description: 'Find out the bot\'s capabilities' },
55
{ command: 'image', description: 'Creating an image based on a text query' },
6-
{ command: 'chatid', description: 'Find the chat ID and your ID' },
7-
{ command: 'add', description: 'Adding new users' },
8-
{ command: 'remove', description: 'Removing users' },
9-
{ command: 'show', description: 'List of all elevated users' },
106
];
117

128
const date = new Date();
@@ -18,9 +14,11 @@ export const LEXICON_EN = {
1814
start:
1915
'Hello, welcome to an artificial intelligence chatbot ' +
2016
'that will help you with everything! 🤖\n\nYou can find ' +
21-
'the source code of the bot here:\nhttps://github.com/BLazzeD21/Node.js-ChatGPT-Bot',
17+
'the source code of the bot here: [click](https://github.com/BLazzeD21/Node.js-ChatGPT-Bot)\n' +
18+
'Developer: @blazzed21\n',
2219
deniedAccess: 'This functionality is not available to you ⛔️',
2320
processingText: 'Text accepted for processing',
21+
processingTranscription: 'Voice message accepted for translation into text',
2422
processingVoice: 'Voice message accepted for processing',
2523
processingImage: 'The request to generate an'+
2624
'image was accepted for processing',

0 commit comments

Comments
 (0)