File size: 2,879 Bytes
d04e364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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});
}