Skip to content

Commit

Permalink
fix: build
Browse files Browse the repository at this point in the history
Signed-off-by: sarthakjdev <jsarthak448@gmail.com>
  • Loading branch information
sarthakjdev committed Jul 14, 2024
1 parent f366e32 commit 1d109c4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 60 deletions.
82 changes: 41 additions & 41 deletions apps/wapi-ai-chatbot/src/index.ts
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)
})
4 changes: 2 additions & 2 deletions apps/wapi-ai-chatbot/src/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import NodeCache from 'node-cache'
const cache = new NodeCache()

export const setCache = (key: string, value: any, ttl: number = 3600) => {
cache.set(key, value, ttl)
cache.set(key, value, ttl)
}

export const getCache = (key: string) => {
return cache.get(key)
return cache.get(key)
}
32 changes: 16 additions & 16 deletions apps/wapi-ai-chatbot/src/utils/gpt.ts
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
}
}
3 changes: 2 additions & 1 deletion packages/create-wapi-app/src/create-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export async function createWhatsappBot(options: {
if (bun) {
await cp(
new URL(
`../template/Bun/${isTypescriptEnabled ? "typescript" : "javascript"
`../template/Bun/${
isTypescriptEnabled ? "typescript" : "javascript"
}/package.json`,
import.meta.url,
),
Expand Down

0 comments on commit 1d109c4

Please sign in to comment.