Something went wrong. Try again.
A comprehensive digital solution dedicated to bridging the gap between those offering services and those seeking them, providing a centrali… gigwork.co.in
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171import { Env } from "hono";import { queryGeminiService } from "../config/gemini";import { getProfilesBySubCategoryOption } from "../services/profile";import { getAllSubCategoryOptions, getSubCategoryOptionByName } from "../services/subCategoryOption";
/** * Process the service check request * @param {string} message - The user message to classify * @returns {Promise<string[] | "NOT_FOUND">} - An array of the top 3 services or "NOT_FOUND" */
export const processCheckService = async (message: string, env: Env): Promise<string[] | "NOT_FOUND"> => { try {
const allOptions = await getAllSubCategoryOptions();
if (!allOptions || allOptions.length === 0) { console.log("No options found"); return "NOT_FOUND"; }
const prompt = ` You are a smart service classifier for a platform that offers over 250 services.
Based on the user message below, rank the **top 3 most appropriate** services from this list: ${JSON.stringify(allOptions)}
Only return the **exact matching service names** from the list above as an array of up to 3 items. If you can't confidently find any matches, return "NOT_FOUND".
User message: "${message}" `.trim();
try {
const response = await queryGeminiService(prompt, env);
if (response && typeof response === "string") {
if (response.includes("NOT_FOUND")) { console.log("Response indicates no match found"); return "NOT_FOUND"; }
const parsedResponse = JSON.parse(response);
if (Array.isArray(parsedResponse) && parsedResponse.length > 0) { const validMatches = parsedResponse.filter(option => allOptions.includes(option)); if (validMatches.length === 0) { return "NOT_FOUND"; } return validMatches.slice(0, 3); }
console.log("No valid matches found in response"); return "NOT_FOUND";
} console.log("Invalid response format"); return "NOT_FOUND";
} catch (error) { console.error("Error querying Gemini service:", error); throw error;
}
} catch (error) { console.error("Error processing service check:", error); throw error; }};
/** * Process the service request * @param {string} service - The service requested by the user * @param {any} location - The user's location * @returns {Promise<any>} - A formatted message with the profiles or an error message */
export const processRequestService = async (service: string, location: any): Promise<any> => { return new Promise(async (resolve, reject) => { try {
const subCategoryOption = await getSubCategoryOptionByName(service); if (!subCategoryOption) { resolve(); } const profiles = await getProfilesBySubCategoryOption(subCategoryOption.id, location); if (!profiles || profiles.length === 0) { resolve(); } const formattedMessage = profiles.map((profile, index) => { return `${index + 1}️⃣ *${profile.name}* \n📞 Phone: ${profile.user.phone || 'N/A'} \n👤 Owner: ${profile.user.name || 'N/A'} \n🔗 View Profile: https://gigwork.co.in/${profile.slug}`; }).join('\n\n'); resolve({ message: formattedMessage, profiles: profiles }); } catch (error) { console.error("Error processing service request:", error); throw error; } })}
export const sendMessageToInterakt = async (otp: string, phone: string, env: Env) => { const url = `https://api.interakt.ai/v1/public/message/`;
console.log('[Interakt Debug] Sending OTP:', { phone, otp, apiKeyExists: !!env.INTERAKT_API_KEY });
const payload = { type: "Template", phoneNumber: phone, countryCode: "91", callbackData: "user_login_otp", template: { name: "gigwork", languageCode: "en", headerValues: [], bodyValues: [otp], buttonValues: { "0": [otp] } } };
try { console.log('[Interakt Debug] Request payload:', JSON.stringify(payload, null, 2));
const response = await fetch(url, { method: "POST", headers: { "Authorization": `Basic ${env.INTERAKT_API_KEY}`, "Content-Type": "application/json", "Accept": "application/json" }, body: JSON.stringify(payload) });
const responseData = await response.json(); console.log('[Interakt Debug] API Response:', responseData);
if (!response.ok) { throw new Error(`Interakt API error: ${response.status} - ${response.statusText}`); }
return responseData; } catch (error) { console.error("[Interakt Debug] Error details:", { message: error.message, stack: error.stack, response: error.response?.data }); throw new Error(`Failed to send WhatsApp OTP: ${error.message}`); }};