File size: 3,757 Bytes
08bfcbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const cron = require('node-cron');
const axios = require("axios");

const fetchWeather = async () => {
  try {
    const response = await axios.get('https://ccexplorerapisjonell.vercel.app/api/weather');
    const { synopsis, issuedAt, temperature, humidity } = response.data;
    return `Weather Update:\n\n${synopsis}\n\nIssued at: ${issuedAt}\nMax Temperature: ${temperature.max.value} at ${temperature.max.time}\nMin Temperature: ${temperature.min.value} at ${temperature.min.time}\nMax Humidity: ${humidity.max.value} at ${humidity.max.time}\nMin Humidity: ${humidity.min.value} at ${humidity.min.time}`;
  } catch (error) {
    return 'Unable to fetch weather information at the moment.';
  }
};

module.exports = async ({ api }) => {
  const config = {
    autoRestart: {
      status: false,
      time: 10,
      note: 'To avoid problems, enable periodic bot restarts',
    },
    greetings: [
      {
        cronTime: '0 5 * * *',
        messages: [`Good morning! Have a great day ahead!`],
      },
      {
        cronTime: '0 8 * * *',
        messages: [`Hello Everyone Time Check 8:00 AM :>`],
      },
      {
        cronTime: '0 10 * * *',
        messages: [`Hello everyone! How's your day going?`],
      },
      {
        cronTime: '0 12 * * *',
        messages: [`Lunchtime reminder: Take a break and eat well!`],
      },
      {
        cronTime: '0 14 * * *',
        messages: [`Reminder: Don't forget your tasks for today!`],
      },
      {
        cronTime: '0 18 * * *',
        messages: [`Good evening! Relax and enjoy your evening.`],
      },
      {
        cronTime: '0 20 * * *',
        messages: [`Time to wind down. Have a peaceful evening.`],
      },
      {
        cronTime: '0 22 * * *',
        messages: [`Good night! Have a restful sleep.`],
      },
      {
        cronTime: '0 7 * * *',
        messages: async () => `Good morning! Have a great day ahead!\n\n${await fetchWeather()}`,
      },
      {
        cronTime: '0 19 * * *',
        messages: async () => `Good evening! Relax and enjoy your evening.\n\n${await fetchWeather()}`,
      }
    ]
  };

  config.greetings.forEach((greeting) => {
    cron.schedule(greeting.cronTime, async () => {
      const message = typeof greeting.messages[0] === 'function' ? await greeting.messages[0]() : greeting.messages[0];
      api.getThreadList(20, null, ['INBOX']).then((list) => {
        list.forEach((thread) => {
          if (thread.isGroup) {
            api.sendMessage(message, thread.threadID).catch((error) => {
              console.log(`Error sending message: ${error}`, 'AutoGreet');
            });
          }
        });
      }).catch((error) => {
        console.log(`Error getting thread list: ${error}`, 'AutoGreet');
      });
    }, {
      scheduled: true,
      timezone: "Asia/Manila"
    });
  });

  if (config.autoRestart.status) {
    cron.schedule(`*/${config.autoRestart.time} * * * *`, () => {
      api.getThreadList(20, null, ['INBOX']).then((list) => {
        list.forEach((thread) => {
          if (thread.isGroup) {
            api.sendMessage("๐Ÿ”ƒ ๐—ฅ๐—ฒ๐˜€๐˜๐—ฎ๐—ฟ๐˜๐—ถ๐—ป๐—ด ๐—ฝ๐—ฟ๐—ผ๐—ฐ๐—ฒ๐˜€๐˜€\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\nBot is restarting...", thread.threadID).then(() => {
              console.log(`Restart message sent to thread`, 'Auto Restart');
            }).catch((error) => {
              console.log(`Error sending restart message to thread ${error}`, 'Auto Restart');
            });
          }
        });
        console.log('Start rebooting the system!', 'Auto Restart');
        process.exit(1);
      }).catch((error) => {
        console.log(`Error getting thread list for restart: ${error}`, 'Auto Restart');
      });
    });
  }
};