Skip to content

Commit

Permalink
feat: Integrate NDTV News API for Random News Retrieval This commit a…
Browse files Browse the repository at this point in the history
…dds 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/)
  • Loading branch information
Guru322 authored Aug 21, 2023
1 parent 0df9260 commit 55e2f63
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions plugins/tools-ndtv.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 55e2f63

Please sign in to comment.