-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathgo.ts
More file actions
55 lines (47 loc) · 1.5 KB
/
Copy pathgo.ts
File metadata and controls
55 lines (47 loc) · 1.5 KB
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
import { Message } from "whatsapp-web.js";
import axios from "axios";
import log from "npmlog";
export const command = "go";
export default async function (msg: Message) {
const query = msg.body.replace(/^go\s+/i, "").trim();
if (!query) {
await msg.reply("Please provide a search query.");
return;
}
try {
const response = await axios.get("https://api.duckduckgo.com/", {
params: {
q: query,
format: "json",
pretty: 1,
no_redirect: 1,
no_html: 1,
},
headers: {
"User-Agent": "Mozilla/5.0",
},
});
const data = response.data;
if (data.AbstractText) {
await msg.reply(`${data.AbstractText}\n${data.AbstractURL}`);
return;
}
// If no abstract, try to get the first related topic
if (Array.isArray(data.RelatedTopics) && data.RelatedTopics.length > 0) {
const firstTopic =
data.RelatedTopics.find(
(t: any) => typeof t.Text === "string" && t.FirstURL
) || data.RelatedTopics[0];
if (firstTopic && firstTopic.Text && firstTopic.FirstURL) {
await msg.reply(`${firstTopic.Text}\n${firstTopic.FirstURL}`);
return;
}
}
// Fallback
const searchUrl = `https://duckduckgo.com/?q=${encodeURIComponent(query)}`;
await msg.reply(`DuckDuckGo search results for "${query}":\n${searchUrl}`);
} catch (error) {
log.error("GoCommand", "Error fetching DuckDuckGo results:", error);
await msg.reply("Failed to search DuckDuckGo.");
}
}