diff --git a/src/command-hive.ts b/src/command-hive.ts index c513247..040425b 100644 --- a/src/command-hive.ts +++ b/src/command-hive.ts @@ -8,6 +8,7 @@ import { imageToStickerText } from '@services/external/image-to-sticker-meme' import { log } from '@services/internal/log' import { indoSlangQuote } from '@services/internal/quote-indo-slang' import { getPpCouple } from '@services/internal/pp-couple' +import { ghola } from '@services/external/ghola' type Commands = { [key: string]: (client: Client, message: Message) => any @@ -30,7 +31,7 @@ const commands: Commands = { '.ppcouple': getPpCouple, // * AI-generated - + '.tanya': ghola, // * Translation '.engtoindo': translateEnglishToIndo, diff --git a/src/services/external/ghola.ts b/src/services/external/ghola.ts new file mode 100644 index 0000000..e371347 --- /dev/null +++ b/src/services/external/ghola.ts @@ -0,0 +1,72 @@ +import 'dotenv/config' +import axios from 'axios' +import { Executor } from '@/command-hive' +import * as wweb from '@utils/wweb' +import config from '@/env' +import * as logger from '@utils/logger' + +const GHOLA_TOKEN_ENDPOINT = 'https://www.ghola.ai/api/v1/chat/init'; +const GHOLA_CHAT_ENDPOINT = 'https://www.ghola.ai/api/v1/chat'; + +const retrieveGholaToken = async () => { + const response = await axios.post(GHOLA_TOKEN_ENDPOINT, { + token: process.env.GHOLA_TOKEN, + email: process.env.GHOLA_EMAIL, + profileId: process.env.GHOLA_AI_PROFILE_ID + }, { + headers: { + 'Content-Type': 'application/json' + } + }); + + return response.data.jwt ?? null; +} + +const askGhola = async (token: string, message: string) => { + const response = await axios.post(GHOLA_CHAT_ENDPOINT, { + messages: [ + { + role: "user", + content: message + } + ] + }, { + headers: { + 'Authorization': 'Bearer ' + token, + 'Content-Type': 'application/json', + }, + }); + + return response.data?.message ?? null; +} + +const ghola: Executor = async (client, message) => { + const contact = await message.getContact(); + + // Extract question from message + const question: string = message.body.split(' ').slice(1).join(' '); + + if (question.length === 0) { + wweb.replyMessage(message, `Berikan pertanyaan kamu kepada ${config.botShortName}, contoh : \`.tanya Hai, siapa kamu?\``); + return 0; + } + + const token: string | null = await retrieveGholaToken(); + if (token == null) { + wweb.replyMessage(message, `${config.botShortName} tidak dapat menjawab pertanyaanmu saat ini, maaf :(`); + logger.logError('tanyaGhola - Failed to retrieve Ghola AI token by ' + contact.pushname ?? 'unknown'); + return 0; + } + + const answer: string = await askGhola(token, question); + if (answer != null) { + wweb.replyMessage(message, answer); + } else { + wweb.replyMessage(message, `${config.botShortName} tidak dapat menjawab pertanyaanmu saat ini, maaf :(`); + logger.logError('tanyaGhola - Failed to retrieve Ghola question by ' + contact.pushname ?? 'unknown'); + } +} + +export { + ghola +} \ No newline at end of file diff --git a/src/services/external/image-to-sticker-meme.ts b/src/services/external/image-to-sticker-meme.ts index bc827d7..9d013b8 100644 --- a/src/services/external/image-to-sticker-meme.ts +++ b/src/services/external/image-to-sticker-meme.ts @@ -158,7 +158,7 @@ const imageToStickerText: Executor = async (client, message) => { } catch (error) { const contact = await message.getContact(); const err = error as AxiosError; - logger.logError('imageToStickerText - ' + err.response?.data ?? err.message + ' by ' + contact?.pushname ?? 'unknown'); + logger.logError('imageToStickerText - ' + err.message + ' by ' + contact?.pushname ?? 'unknown'); wweb.replyMessage(message, `${config.botShortName} gagal memproses gambar yang anda tujukan, mohon coba lagi dengan mengirim gambar baru.`) }