𝗗𝗔π—₯π—₯π—˜π—Ÿπ—Ÿ π— π—¨π—–π—›π—˜π—₯π—œ ⚑ commited on
Commit
c7d527a
Β·
unverified Β·
1 Parent(s): aa825d5

Create ai-chatbot.js

Browse files
Files changed (1) hide show
  1. plugins/ai-chatbot.js +39 -0
plugins/ai-chatbot.js ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const axios = require('axios');
2
+ const { cmd } = require('../command');
3
+
4
+ cmd({
5
+ pattern: "chatai",
6
+ desc: "Chat with the AI chatbot.",
7
+ category: "fun",
8
+ react: "πŸ€–",
9
+ filename: __filename
10
+ },
11
+ async (conn, mek, m, { from, reply, text }) => {
12
+ // Ensure the user has sent a query
13
+ if (!text) {
14
+ return reply("Please type a question or message to chat with the bot.");
15
+ }
16
+
17
+ try {
18
+ // Construct the API URL
19
+ const url = `https://api.davidcyriltech.my.id/ai/chatbot?query=${encodeURIComponent(text)}`;
20
+
21
+ // Make the request to the chatbot API
22
+ const response = await axios.get(url);
23
+
24
+ // Extract the chatbot's reply from the API response
25
+ const botReply = response.data.reply;
26
+
27
+ // Check if the response contains the bot's reply
28
+ if (botReply) {
29
+ // Send the bot's reply back to the user
30
+ return reply(botReply);
31
+ } else {
32
+ // If the bot response is empty or undefined
33
+ return reply("Sorry, I couldn't understand your message. Please try again.");
34
+ }
35
+ } catch (e) {
36
+ console.error("Error with chatbot API:", e);
37
+ reply("There was an issue while chatting with the bot. Please try again later.");
38
+ }
39
+ });