From 55e2f6360f11edb8805a4e872f74589ba38bd6c3 Mon Sep 17 00:00:00 2001 From: GURU Date: Mon, 21 Aug 2023 21:33:14 +0530 Subject: [PATCH] feat: Integrate NDTV News API for Random News Retrieval This commit adds functionality to fetch and display random news articles from the NDTV News API. The code queries the API for the latest news articles and selects a random article to present to users. The displayed news includes the headline, description, and URL. Thumbnail images are also included in the display. Error handling and reactions are provided for success and failure cases. [API Source]: NDTV News API (https://ndtvapi.vercel.app/) --- plugins/tools-ndtv.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 plugins/tools-ndtv.js diff --git a/plugins/tools-ndtv.js b/plugins/tools-ndtv.js new file mode 100644 index 0000000000..db6e461c41 --- /dev/null +++ b/plugins/tools-ndtv.js @@ -0,0 +1,42 @@ +import fetch from 'node-fetch'; + +let handler = async (m, { conn }) => { + try { + let res = await fetch('https://ndtvapi.vercel.app/general?category=latest&field=headline,description,url'); + if (!res.ok) throw await res.text(); + let data = await res.json(); + if (!data.news || !data.news.length) throw new Error('No news available.'); + + let randomCategoryIndex = Math.floor(Math.random() * data.news.length); + let newsCategory = data.news[randomCategoryIndex]; + let articles = newsCategory.articles; + + if (!articles.length) throw new Error('No news available in the selected category.'); + + let randomArticleIndex = Math.floor(Math.random() * articles.length); + let newsItem = articles[randomArticleIndex]; + + let newsInfo = `•───── ୨❀୧ ─────• + ❖ 𝑺𝑻𝑨𝑻𝑼𝑺: Active + ㋡ 𝑪𝑹𝑬𝑨𝑻𝑶𝑹: GURU + ☞ 𝑯𝑬𝑨𝑫𝑳𝑰𝑵𝑬: ${newsItem.headline} + ${newsItem.description} + 🔗 𝑹𝑬𝑨𝑫 𝑴𝑶𝑹𝑬: ${newsItem.url} + •───── ୨❀୧ ─────•`; + + let thumbnail = 'https://yt3.ggpht.com/-L8AxmJwZuy8/AAAAAAAAAAI/AAAAAAAAAAA/eZRzS7tRVX0/s900-c-k-no/photo.jpg'; + + conn.sendFile(m.chat, thumbnail, 'thumbnail.jpg', newsInfo, m); + + m.react('✅'); + } catch (e) { + console.error(e); + m.react('❌'); + } +}; + +handler.help = ['news']; +handler.tags = ['news']; +handler.command = ['ndtv']; + +export default handler;