Skip to content

Commit 155ebde

Browse files
committed
error message always showing up and update openai to v4
1 parent 5054f92 commit 155ebde

File tree

5 files changed

+113
-34
lines changed

5 files changed

+113
-34
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "WhatsApp chatbot",
55
"main": "src/index.ts",
66
"scripts": {
7-
"start": "yarn ts-node src/index.ts",
7+
"start": "yarn nodemon src/index.ts",
88
"dev": "yarn nodemon src/index.ts"
99
},
1010
"repository": "https://github.com/veigamann/sydney-whatsapp-chatbot.git",
@@ -32,7 +32,7 @@
3232
"fs": "^0.0.1-security",
3333
"keyv": "^4.5.2",
3434
"node-schedule": "^2.1.1",
35-
"openai": "^3.2.1",
35+
"openai": "4.2.0",
3636
"os": "^0.1.2",
3737
"path": "^0.12.7",
3838
"qrcode-terminal": "^0.12.0",

src/clients/openai.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { Configuration, OpenAIApi } from "openai";
2-
const configuration = new Configuration({
3-
apiKey: process.env.OPENAI_API_KEY
4-
});
1+
import OpenAI from "openai";
52

6-
export const openai = new OpenAIApi(configuration);
3+
export const openai = new OpenAI({
4+
apiKey: process.env.OPENAI_API_KEY, // This is also the default, can be omitted
5+
});

src/handlers/audio-transcription.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ export async function transcribeAudio(audioBuffer: Buffer) {
3030
// Deletes the ogg file as it is no longer needed
3131
fs.unlinkSync(oggPath);
3232

33-
const transcription = await openai.createTranscription(
34-
fs.createReadStream(wavPath),
35-
"whisper-1"
36-
);
37-
return transcription.data.text;
33+
const transcription = await openai.audio.transcriptions.create({
34+
model: "whisper-1",
35+
file: fs.createReadStream(wavPath),
36+
});
37+
38+
return transcription.text;
3839
}

src/handlers/message.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,13 @@ export async function handleMessage(message: Message) {
201201

202202
if (chat.isGroup)
203203
await upsertLastWAreplyId(chat.id._serialized, reply.id._serialized);
204-
} catch (e) {
204+
} catch (e: Error | unknown) {
205+
if (e instanceof Error && e.name === "Success") {
206+
return; // Skip error handling for "Success" response
207+
}
208+
205209
await react(message, "error");
210+
206211
const error = serializeError(e);
207212
const errorMessage = error.message?.split("\n")[0];
208213

yarn.lock

Lines changed: 95 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@
187187
dependencies:
188188
"@types/node" "*"
189189

190+
"@types/node-fetch@^2.6.4":
191+
version "2.6.4"
192+
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.4.tgz#1bc3a26de814f6bf466b25aeb1473fa1afe6a660"
193+
integrity sha512-1ZX9fcN4Rvkvgv4E6PAY5WXUFWFcRWxZa3EW83UjycOB9ljJCedb2CupIP4RZMEwF/M3eTcCihbBRgwtGbg5Rg==
194+
dependencies:
195+
"@types/node" "*"
196+
form-data "^3.0.0"
197+
190198
"@types/node-schedule@^2.1.0":
191199
version "2.1.0"
192200
resolved "https://registry.yarnpkg.com/@types/node-schedule/-/node-schedule-2.1.0.tgz#60375640c0509bab963573def9d1f417f438c290"
@@ -199,6 +207,11 @@
199207
resolved "https://registry.npmjs.org/@types/node/-/node-20.3.2.tgz"
200208
integrity sha512-vOBLVQeCQfIcF/2Y7eKFTqrMnizK5lRNQ7ykML/5RuwVXVWxYkgwS7xbt4B6fKCUPgbSL5FSsjHQpaGQP/dQmw==
201209

210+
"@types/node@^18.11.18":
211+
version "18.17.11"
212+
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.17.11.tgz#c04054659d88bfeba94095f41ef99a8ddf4e1813"
213+
integrity sha512-r3hjHPBu+3LzbGBa8DHnr/KAeTEEOrahkcL+cZc4MaBMTM+mk8LtXR+zw+nqfjuDZZzYTYgTcpHuP+BEQk069g==
214+
202215
"@types/qrcode-terminal@^0.12.0":
203216
version "0.12.0"
204217
resolved "https://registry.npmjs.org/@types/qrcode-terminal/-/qrcode-terminal-0.12.0.tgz"
@@ -304,6 +317,13 @@ agentkeepalive@^4.1.3:
304317
depd "^2.0.0"
305318
humanize-ms "^1.2.1"
306319

320+
agentkeepalive@^4.2.1:
321+
version "4.5.0"
322+
resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-4.5.0.tgz#2673ad1389b3c418c5a20c5d7364f93ca04be923"
323+
integrity sha512-5GG/5IbQQpC9FpkRGsSvZI5QYeSCzlJHdpBQntCsuTOxhKD8lqKhrleg2Yi7yvMIf82Ycmmqln9U8V9qwEiJew==
324+
dependencies:
325+
humanize-ms "^1.2.1"
326+
307327
aggregate-error@^3.0.0:
308328
version "3.1.0"
309329
resolved "https://registry.npmjs.org/aggregate-error/-/aggregate-error-3.1.0.tgz"
@@ -469,18 +489,16 @@ avvio@^8.2.1:
469489
debug "^4.0.0"
470490
fastq "^1.6.1"
471491

472-
axios@^0.26.0:
473-
version "0.26.1"
474-
resolved "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz"
475-
integrity sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==
476-
dependencies:
477-
follow-redirects "^1.14.8"
478-
479492
balanced-match@^1.0.0:
480493
version "1.0.2"
481494
resolved "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz"
482495
integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==
483496

497+
base-64@^0.1.0:
498+
version "0.1.0"
499+
resolved "https://registry.yarnpkg.com/base-64/-/base-64-0.1.0.tgz#780a99c84e7d600260361511c4877613bf24f6bb"
500+
integrity sha512-Y5gU45svrR5tI2Vt/X9GPd3L0HNIKzGu202EjxrXMpuc2V2CiKgemAbUUsqYmZJvPtCXoUKjNZwBJzsNScUbXA==
501+
484502
base64-js@^1.3.1:
485503
version "1.5.1"
486504
resolved "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz"
@@ -655,6 +673,11 @@ chardet@^0.7.0:
655673
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e"
656674
integrity sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==
657675

676+
677+
version "0.0.2"
678+
resolved "https://registry.yarnpkg.com/charenc/-/charenc-0.0.2.tgz#c0a1d2f3a7092e03774bfa83f14c0fc5790a8667"
679+
integrity sha512-yrLQ/yVUFXkzg7EDQsPieE/53+0RlaWTs+wBrvW36cyilJ2SaDWfl4Yj7MtLTXleV9uEKefbAGUPv2/iWSooRA==
680+
658681
chokidar@^3.5.2:
659682
version "3.5.3"
660683
resolved "https://registry.npmjs.org/chokidar/-/chokidar-3.5.3.tgz"
@@ -828,6 +851,11 @@ cross-spawn@^7.0.3:
828851
shebang-command "^2.0.0"
829852
which "^2.0.1"
830853

854+
855+
version "0.0.2"
856+
resolved "https://registry.yarnpkg.com/crypt/-/crypt-0.0.2.tgz#88d7ff7ec0dfb86f713dc87bbb42d044d3e6c41b"
857+
integrity sha512-mCxBlsHFYh9C+HVpiEacem8FEBnMXgU9gy4zmNC+SXAZNB/1idgp/aulFJ4FgCi7GPEVbfyng092GqL2k2rmow==
858+
831859
crypto@^1.0.1:
832860
version "1.0.1"
833861
resolved "https://registry.yarnpkg.com/crypto/-/crypto-1.0.1.tgz#2af1b7cad8175d24c8a1b0778255794a21803037"
@@ -884,6 +912,14 @@ diff@^4.0.1:
884912
resolved "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz"
885913
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
886914

915+
digest-fetch@^1.3.0:
916+
version "1.3.0"
917+
resolved "https://registry.yarnpkg.com/digest-fetch/-/digest-fetch-1.3.0.tgz#898e69264d00012a23cf26e8a3e40320143fc661"
918+
integrity sha512-CGJuv6iKNM7QyZlM2T3sPAdZWd/p9zQiRNS9G+9COUCwzWFTs0Xp8NF5iePx7wtvhDykReiRRrSeNb4oMmB8lA==
919+
dependencies:
920+
base-64 "^0.1.0"
921+
md5 "^2.3.0"
922+
887923
dotenv@^16.0.3:
888924
version "16.3.1"
889925
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.3.1.tgz#369034de7d7e5b120972693352a3bf112172cc3e"
@@ -1122,20 +1158,28 @@ fluent-ffmpeg@^2.1.2:
11221158
async ">=0.2.9"
11231159
which "^1.1.1"
11241160

1125-
follow-redirects@^1.14.8:
1126-
version "1.15.2"
1127-
resolved "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz"
1128-
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==
1161+
1162+
version "1.7.2"
1163+
resolved "https://registry.yarnpkg.com/form-data-encoder/-/form-data-encoder-1.7.2.tgz#1f1ae3dccf58ed4690b86d87e4f57c654fbab040"
1164+
integrity sha512-qfqtYan3rxrnCk1VYaA4H+Ms9xdpPqvLZa6xmMgFvhO32x7/3J/ExcTd6qpxM0vH2GdMI+poehyBZvqfMTto8A==
11291165

1130-
form-data@^4.0.0:
1131-
version "4.0.0"
1132-
resolved "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz"
1133-
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
1166+
form-data@^3.0.0:
1167+
version "3.0.1"
1168+
resolved "https://registry.yarnpkg.com/form-data/-/form-data-3.0.1.tgz#ebd53791b78356a99af9a300d4282c4d5eb9755f"
1169+
integrity sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==
11341170
dependencies:
11351171
asynckit "^0.4.0"
11361172
combined-stream "^1.0.8"
11371173
mime-types "^2.1.12"
11381174

1175+
formdata-node@^4.3.2:
1176+
version "4.4.1"
1177+
resolved "https://registry.yarnpkg.com/formdata-node/-/formdata-node-4.4.1.tgz#23f6a5cb9cb55315912cbec4ff7b0f59bbd191e2"
1178+
integrity sha512-0iirZp3uVDjVGt9p49aTaqjk84TrglENEDuqfdlZQ1roC9CWlPk6Avf8EEnZNcAqPonwkG35x4n3ww/1THYAeQ==
1179+
dependencies:
1180+
node-domexception "1.0.0"
1181+
web-streams-polyfill "4.0.0-beta.3"
1182+
11391183
11401184
version "0.2.0"
11411185
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.2.0.tgz#2269936428aad4c15c7ebe9779a84bf0b2a81811"
@@ -1429,6 +1473,11 @@ is-binary-path@~2.1.0:
14291473
dependencies:
14301474
binary-extensions "^2.0.0"
14311475

1476+
is-buffer@~1.1.6:
1477+
version "1.1.6"
1478+
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be"
1479+
integrity sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==
1480+
14321481
is-docker@^2.0.0:
14331482
version "2.2.1"
14341483
resolved "https://registry.yarnpkg.com/is-docker/-/is-docker-2.2.1.tgz#33eeabe23cfe86f14bde4408a02c0cfb853acdaa"
@@ -1699,6 +1748,15 @@ make-fetch-happen@^9.1.0:
16991748
socks-proxy-agent "^6.0.0"
17001749
ssri "^8.0.0"
17011750

1751+
md5@^2.3.0:
1752+
version "2.3.0"
1753+
resolved "https://registry.yarnpkg.com/md5/-/md5-2.3.0.tgz#c3da9a6aae3a30b46b7b0c349b87b110dc3bda4f"
1754+
integrity sha512-T1GITYmFaKuO91vxyoQMFETst+O71VUPEU3ze5GNzDm0OWdP8v1ziTaAEPUr/3kLsY3Sftgz242A1SetQiDL7g==
1755+
dependencies:
1756+
charenc "0.0.2"
1757+
crypt "0.0.2"
1758+
is-buffer "~1.1.6"
1759+
17021760
merge-stream@^2.0.0:
17031761
version "2.0.0"
17041762
resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60"
@@ -1853,6 +1911,11 @@ node-addon-api@^4.2.0:
18531911
resolved "https://registry.npmjs.org/node-addon-api/-/node-addon-api-4.3.0.tgz"
18541912
integrity sha512-73sE9+3UaLYYFmDsFZnqCInzPyh3MqIwZO9cw58yIqAZhONrrabrYyYe3TuIqtIiOuTXVhsGau8hcrhhwSsDIQ==
18551913

1914+
1915+
version "1.0.0"
1916+
resolved "https://registry.yarnpkg.com/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5"
1917+
integrity sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==
1918+
18561919
18571920
version "2.6.7"
18581921
resolved "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz"
@@ -1988,13 +2051,19 @@ onetime@^5.1.0, onetime@^5.1.2:
19882051
dependencies:
19892052
mimic-fn "^2.1.0"
19902053

1991-
openai@^3.2.1:
1992-
version "3.2.1"
1993-
resolved "https://registry.npmjs.org/openai/-/openai-3.2.1.tgz"
1994-
integrity sha512-762C9BNlJPbjjlWZi4WYK9iM2tAVAv0uUp1UmI34vb0CN5T2mjB/qM6RYBmNKMh/dN9fC+bxqPwWJZUTWW052A==
2054+
openai@4.2.0:
2055+
version "4.2.0"
2056+
resolved "https://registry.yarnpkg.com/openai/-/openai-4.2.0.tgz#ccaef83bccad50041dd3e733e231b0c38f889b99"
2057+
integrity sha512-zfvpO2eITIxIjTG8T6Cek7NB2dMvP/LW0TRUJ4P9E8+qbBNKw00DrtfF64b+fAV2+wUYCVyynT6iSycJ//TtbA==
19952058
dependencies:
1996-
axios "^0.26.0"
1997-
form-data "^4.0.0"
2059+
"@types/node" "^18.11.18"
2060+
"@types/node-fetch" "^2.6.4"
2061+
abort-controller "^3.0.0"
2062+
agentkeepalive "^4.2.1"
2063+
digest-fetch "^1.3.0"
2064+
form-data-encoder "1.7.2"
2065+
formdata-node "^4.3.2"
2066+
node-fetch "^2.6.7"
19982067

19992068
ora@^5.4.1:
20002069
version "5.4.1"
@@ -2853,6 +2922,11 @@ wcwidth@^1.0.1:
28532922
dependencies:
28542923
defaults "^1.0.3"
28552924

2925+
2926+
version "4.0.0-beta.3"
2927+
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-4.0.0-beta.3.tgz#2898486b74f5156095e473efe989dcf185047a38"
2928+
integrity sha512-QW95TCTaHmsYfHDybGMwO5IJIM93I/6vTRk+daHTWFPhwh+C8Cg7j7XyKrwrj8Ib6vYXe0ocYNrmzY4xAAN6ug==
2929+
28562930
webidl-conversions@^3.0.0:
28572931
version "3.0.1"
28582932
resolved "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz"

0 commit comments

Comments
 (0)