-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: sarthakjdev <[email protected]>
- Loading branch information
1 parent
f366e32
commit 1d109c4
Showing
4 changed files
with
61 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,55 @@ | ||
import { TextMessage, TextMessageEvent } from '@wapijs/wapi.js' | ||
import { TextMessage, type TextMessageEvent } from '@wapijs/wapi.js' | ||
import { whatsappClient } from './utils/client' | ||
import { askAi } from './utils/gpt' | ||
import { getCache, setCache } from './utils/cache' | ||
|
||
async function init() { | ||
try { | ||
whatsappClient.on('Ready', () => { | ||
console.log('Client is ready') | ||
}) | ||
|
||
whatsappClient.on('Error', error => { | ||
console.error('Error', error.message) | ||
}) | ||
|
||
whatsappClient.on('TextMessage', async (event: TextMessageEvent) => { | ||
const cachedResponse = await getCache(event.text.data.text) | ||
console.log({ cachedResponse }) | ||
let response = 'Sorry, I am not able to understand that.' | ||
if (cachedResponse) { | ||
response = String(cachedResponse) | ||
} else { | ||
const aiResponse = await askAi(event.text.data.text) | ||
response = aiResponse || response | ||
if (aiResponse) { | ||
setCache(event.text.data.text, aiResponse) | ||
} | ||
} | ||
|
||
console.log({ response }) | ||
await event.reply({ | ||
message: new TextMessage({ | ||
text: response | ||
}) | ||
}) | ||
}) | ||
|
||
whatsappClient.initiate() | ||
} catch (error) { | ||
console.error(error) | ||
// ! TODO: you may prefer to send a notification to your slack channel or email here | ||
} | ||
try { | ||
whatsappClient.on('Ready', () => { | ||
console.log('Client is ready') | ||
}) | ||
|
||
whatsappClient.on('Error', error => { | ||
console.error('Error', error.message) | ||
}) | ||
|
||
whatsappClient.on('TextMessage', async (event: TextMessageEvent) => { | ||
const cachedResponse = await getCache(event.text.data.text) | ||
console.log({ cachedResponse }) | ||
let response = 'Sorry, I am not able to understand that.' | ||
if (cachedResponse) { | ||
response = String(cachedResponse) | ||
} else { | ||
const aiResponse = await askAi(event.text.data.text) | ||
response = aiResponse || response | ||
if (aiResponse) { | ||
setCache(event.text.data.text, aiResponse) | ||
} | ||
} | ||
|
||
console.log({ response }) | ||
await event.reply({ | ||
message: new TextMessage({ | ||
text: response | ||
}) | ||
}) | ||
}) | ||
|
||
whatsappClient.initiate() | ||
} catch (error) { | ||
console.error(error) | ||
// ! TODO: you may prefer to send a notification to your slack channel or email here | ||
} | ||
} | ||
|
||
init().catch(error => console.error(error)) | ||
|
||
process.on('unhandledRejection', error => { | ||
console.error('unhandledRejection', error) | ||
process.exit(1) | ||
console.error('unhandledRejection', error) | ||
process.exit(1) | ||
}) | ||
|
||
process.on('uncaughtException', error => { | ||
console.error('uncaughtException', error) | ||
process.exit(1) | ||
console.error('uncaughtException', error) | ||
process.exit(1) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
import OpenAI from 'openai' | ||
import { OpenAI } from 'openai' | ||
|
||
const openAiApiKey = process.env.OPEN_AI_API_KEY | ||
|
||
if (!openAiApiKey) { | ||
throw new Error('OPEN_AI_API_KEY not defined!') | ||
throw new Error('OPEN_AI_API_KEY not defined!') | ||
} | ||
|
||
const OpenApiClient = new OpenAI({ | ||
apiKey: openAiApiKey, | ||
project: 'proj_viwVq5nzCR5Mj4TnIw4FQoNO', | ||
organization: 'org-wwfaGhYXIA7CBUSg6x8DbKYj' | ||
apiKey: openAiApiKey, | ||
project: 'proj_viwVq5nzCR5Mj4TnIw4FQoNO', | ||
organization: 'org-wwfaGhYXIA7CBUSg6x8DbKYj' | ||
}) | ||
|
||
export async function askAi(message: string): Promise<string | null> { | ||
try { | ||
const chatCompletion = await OpenApiClient.chat.completions.create({ | ||
messages: [{ role: 'user', content: message }], | ||
model: 'gpt-3.5-turbo' | ||
}) | ||
try { | ||
const chatCompletion = await OpenApiClient.chat.completions.create({ | ||
messages: [{ role: 'user', content: message }], | ||
model: 'gpt-3.5-turbo' | ||
}) | ||
|
||
console.log(JSON.stringify({ chatCompletion })) | ||
console.log(JSON.stringify({ chatCompletion })) | ||
|
||
return chatCompletion.choices[0].message.content | ||
} catch (error) { | ||
console.log({ error }) | ||
return null | ||
} | ||
return chatCompletion.choices[0].message.content | ||
} catch (error) { | ||
console.log({ error }) | ||
return null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters