import axios from "axios"; export const config = { "name": "pretend", "url": "/api/pretend", "description": "Pretending to be someone, generate response!", "query": "prompt, name", "response": "text", "testURL": "./api/pretend?prompt=hello&name=goku" } const getBotData = async (name) => { const url = `https://api.exh.ai/strapi-secondary/api/bots?filters[name][$containsi]=${name}&pagination[page]=1&sort=messagesCount:desc`; let res = await axios.get(url); if (res.data.data.length < 1){ res = await axios.get(`https://api.exh.ai/strapi-secondary/api/bots?filters[description][$containsi]=${name}&pagination[page]=1&sort=messagesCount:desc`) } let data = res.data.data[0].attributes.name.toLowerCase().split(' ').includes(name.toLowerCase())? res.data.data[0] : res.data.data[1]; if (name.split(' ').length > 1) { data = res.data.data[0] } console.log(data) //console.log( res.data.data[0].attributes.name.toLowerCase().split(' ').includes(name.toLowerCase())) return data; }; const getResponse = async (prompt, name) => { const botData = await getBotData(name); const tokenData = await axios.get( "https://botify.ai/static/js/main.4f49c262.js" ); let token = ""; //console.log(response.data); const regex = /ks\.setToken\("([^"]+)"\)/; const match = tokenData.data.match(regex); if (match && match[1]) { const _sValue = match[1]; token = _sValue; //console.log(_sValue); } else { //console.log("No match found."); } const url = "https://api.exh.ai/chatbot/v1/get_response"; //console.log(botData.attributes.personaFacts.map((element)=>{ return element.content })) const firstdata = { name: botData.attributes.name, context: [ { message: prompt, turn: "user" }, ], strapi_bot_id: botData.id, persona_facts: botData.attributes.personaFacts.map((element)=>{ return element.content }) || [], access_level: "basic", }; const headers = { Authorization: "Bearer " + token, }; const newResponse = await axios.post(url, firstdata, { headers, }) const botFirstMessage = newResponse.data.response; console.log(botFirstMessage) const data = { name: botData.attributes.name, context: [ { message: botFirstMessage, turn: "bot" }, { message: prompt, turn: "user" }, ], strapi_bot_id: botData.id, persona_facts: botData.attributes.personaFacts.map((element)=>{ return element.content }) || [], access_level: "basic", }; const response = await axios.post(url, data, { headers, }); console.log(response.data.response) return {...response.data, input:data}; }; export default async function handler(req, res) { const prompt = req.query.prompt; const name = req.query.name; const response = await getResponse(prompt, name); res.status(200).json({...response}); }