File size: 2,615 Bytes
2b023c8
 
 
 
 
 
 
 
 
 
06771c4
 
 
 
 
 
 
2b023c8
 
 
 
 
 
 
bb0fb92
2b023c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3eee2a5
2b023c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const TelegramBot = require("node-telegram-bot-api");
const axios = require("axios");

const TOKEN = process.env.BOT_TOKEN;
const GROQ_API = process.env.GROQ_API;

const SUPABASE_URL = process.env.SUPABASE_URL;
const SUPABASE_KEY = process.env.SUPABASE_KEY;

const bot = new TelegramBot(TOKEN, {
  polling: {
    interval: 300,
    autoStart: true,
    params: {
      timeout: 10
    }
  }
});

console.log("CEO AI Started");

async function saveMemory(role, content) {
  try {
    await axios.post(
      `${SUPABASE_URL}/rest/v1/Memory`,
      {
        role,
        content
      },
      {
        headers: {
          apikey: SUPABASE_KEY,
          Authorization: `Bearer ${SUPABASE_KEY}`,
          "Content-Type": "application/json",
          Prefer: "return=minimal"
        }
      }
    );
  } catch (err) {
    console.log("Memory Save Error:", err.message);
  }
}

async function getMemory() {
  try {
    const res = await axios.get(
      `${SUPABASE_URL}/rest/v1/memory?select=role,content&order=id.desc&limit=10`,
      {
        headers: {
          apikey: SUPABASE_KEY,
          Authorization: `Bearer ${SUPABASE_KEY}`
        }
      }
    );

    return res.data.reverse();
  } catch (err) {
    console.log("Memory Load Error:", err.message);
    return [];
  }
}

bot.on("message", async (msg) => {

  const chatId = msg.chat.id;
  const text = msg.text;

  if (!text) return;

  bot.sendChatAction(chatId, "typing");

  await saveMemory("user", text);

  const memory = await getMemory();

  const history = memory.map(m =>
    `${m.role}: ${m.content}`
  ).join("\n");

  try {

    const response = await axios.post(
      "https://api.groq.com/openai/v1/chat/completions",
      {
        model: "llama-3.1-8b-instant",
        messages: [
          {
            role: "system",
            content:
              "You are a powerful CEO AI assistant managing business growth, automation, AI agents, content systems, apps, websites, YouTube automation, blogging, and online business infrastructure."
          },
          {
            role: "user",
            content: history + `\nUser: ${text}`
          }
        ]
      },
      {
        headers: {
          Authorization: `Bearer ${GROQ_API}`,
          "Content-Type": "application/json"
        }
      }
    );

    const reply =
      response.data.choices[0].message.content;

    await saveMemory("assistant", reply);

    bot.sendMessage(chatId, reply);

  } catch (err) {

    console.log(err.response?.data || err.message);

    bot.sendMessage(
      chatId,
      "AI system error. Check logs."
    );
  }

});