wabot
Version:
Whatsapp Bot Module for automate response and interact whit users
327 lines (281 loc) • 13.1 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Wabot Documentation plugins/news.js</title>
<script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script src="./build/entry.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link href="https://fonts.googleapis.com/css?family=Roboto:100,400,700|Inconsolata,700" rel="stylesheet">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
<link type="text/css" rel="stylesheet" href="https://jmblog.github.io/color-themes-for-google-code-prettify/themes/tomorrow-night.min.css">
<link type="text/css" rel="stylesheet" href="styles/app.min.css">
<link type="text/css" rel="stylesheet" href="styles/iframe.css">
<link type="text/css" rel="stylesheet" href="">
<script async defer src="https://buttons.github.io/buttons.js"></script>
</head>
<body class="layout small-header">
<div id="stickyNavbarOverlay"></div>
<div class="top-nav">
<div class="inner">
<a id="hamburger" role="button" class="navbar-burger" aria-label="menu" aria-expanded="false">
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
</a>
<div class="logo">
<a href="index.html">
<h1 class="navbar-item">Wabot Documentation</h1>
</a>
</div>
<div class="menu">
<div class="navigation">
<a
href="index.html"
class="link"
>
API Documentation
</a>
<a
class="link user-link "
href="https://github.com/luiscruzga/wabot"
>
Github
</a>
<a
class="link user-link "
href="https://github.com/luiscruzga/wabot/blob/master/example/test.js"
>
Example
</a>
</div>
</div>
</div>
</div>
<div id="main">
<div
class="sidebar "
id="sidebarNav"
>
<div class="search-wrapper">
<input id="search" type="text" placeholder="Search docs..." class="input">
</div>
<nav>
<h2><a href="index.html">Documentation</a></h2><div class="category"><h3>Namespaces</h3><ul><li><a href="Plugins.html">Plugins</a></li></ul><h3>Classes</h3><ul><li><a href="WABOT.html">WABOT</a></li></ul><h3>Events</h3><ul><li><a href="WABOT.html#event:command">command</a></li><li><a href="WABOT.html#event:message">message</a></li><li><a href="WABOT.html#event:onAddedToGroup">onAddedToGroup</a></li><li><a href="WABOT.html#event:onBattery">onBattery</a></li><li><a href="WABOT.html#event:onMessageFromBloqued">onMessageFromBloqued</a></li><li><a href="WABOT.html#event:onMessageFromNoPrivileges">onMessageFromNoPrivileges</a></li><li><a href="WABOT.html#event:onMessageMediaUploadedEvent">onMessageMediaUploadedEvent</a></li><li><a href="WABOT.html#event:onParticipantsChanged">onParticipantsChanged</a></li><li><a href="WABOT.html#event:onPlugged">onPlugged</a></li><li><a href="WABOT.html#event:onRemovedFromGroup">onRemovedFromGroup</a></li><li><a href="WABOT.html#event:onStateChanged">onStateChanged</a></li><li><a href="WABOT.html#event:ready">ready</a></li><li><a href="WABOT.html#event:vcard">vcard</a></li><li><a href="WABOT.html#event:waitNewAcknowledgements">waitNewAcknowledgements</a></li></ul></div>
</nav>
</div>
<div class="core" id="main-content-wrapper">
<div class="content">
<header class="page-title">
<p>Source</p>
<h1>plugins/news.js</h1>
</header>
<section>
<article>
<pre class="prettyprint source linenums"><code>// Modules to install separately
const request = require('request');
const base64 = require('node-base64-image');
const feed = require("feed-read");
const scrape = require('html-metadata');
const thumbDefault = 'https://st2.depositphotos.com/3837271/6711/i/950/depositphotos_67114347-stock-photo-news-piece-of-paper.jpg';
var timeRefresh = 10;
var contMaxNotice = 3;
var contNews = 0;
var newsArray = [];
const loadNewsCache = (feeds) => {
newsArray = [];
feeds.forEach(source => {
feed(source, (err, articles) => {
if(!err){
if(articles.length > 0)
{
articles.forEach(article => {
newsArray.push(article);
})
}
}
});
})
}
const getNews = (search) => {
return new Promise((resolve, reject) => {
if(search.trim() === ''){
let indexNew = Math.floor(Math.random() * newsArray.length);
let urlThumb = newsArray[indexNew].link;
scrape(urlThumb, (error, metadata) => {
if(!error){
if(typeof metadata.openGraph != 'undefined' && metadata.openGraph != undefined){
const title = metadata.openGraph.title || "News";
const description = metadata.openGraph.description || "";
const urlNews = metadata.openGraph.url || urlThumb;
let options = {string: true}
let urlImage;
if(typeof metadata.openGraph.image.url != "undefined" && metadata.openGraph.image.url != undefined && metadata.openGraph.image.url != ""){
urlImage = metadata.openGraph.image.url;
}else{
urlImage = thumbDefault;
}
base64.encode(urlImage, options, (err, data64) => {
if(err){
resolve({
"thumb": thumbDefault,
"urlNews": urlNews,
"title": title,
"description": description
});
}else{
resolve({
"thumb": data64,
"urlNews": urlNews,
"title": title,
"description": description
});
}
})
}
}else{
resolve({
"thumb": '',
"urlNews": urlThumb,
"title": '',
"description": ''
});
}
});
}else{
let newsFilter = [];
newsArray.forEach(news => {
if(news.title.toUpperCase().indexOf(search.trim().toUpperCase()) !== -1 ){
newsFilter.push(news);
}
})
if(newsFilter.length > 0){
let indexNew = Math.floor(Math.random() * newsFilter.length);
let urlThumb = newsFilter[indexNew].link;
scrape(urlThumb, function(error, metadata){
if(!error){
if(typeof metadata.openGraph != 'undefined' && metadata.openGraph != undefined){
const title = metadata.openGraph.title || "News";
const description = metadata.openGraph.description || "";
const urlNews = metadata.openGraph.url || urlThumb;
let options = {string: true}
let urlImage;
if(typeof metadata.openGraph.image.url != "undefined" && metadata.openGraph.image.url != undefined && metadata.openGraph.image.url != ""){
urlImage = metadata.openGraph.image.url;
}else{
urlImage = thumbDefault;
}
base64.encode(urlImage, options, (err, data64) => {
if(err){
reject(err);
}else{
resolve({
"thumb": data64,
"urlNews": urlNews,
"title": title,
"description": description
});
}
})
}
}else{
resolve({
"thumb": '',
"urlNews": urlThumb,
"title": '',
"description": ''
});
}
});
}
}
});
}
const defaultConfig = {
idChat: '',
search: '',
messageError: '*Ooops, an error occurred while trying to get news, try again later*'
}
/**
* Plugin to get news from the different rss sources that are configured
* @function news
* @memberof Plugins
* @param {string} idChat - Chat id to send the new image to
* @param {string} search - Customer search parameter
* @param {string} messageError - Message to send in case of error
*/
module.exports = {
/**
* Id - Name of the plugin to use
* @property {string} id - Name of the plugin to use
*/
id: 'news',
/**
* Initial setting function
* @param {object} data - Initial information for the plugin
* @param {number} data.timeRefresh - Time in minutes to refresh the news in cache memory
* @param {string[]} data.feeds - Rss sources for getting news
*/
setup(data) {
if (typeof data.feeds !== 'undefined' && Array.isArray(data.feeds)) {
if (data.timeRefresh) {
timeRefresh = data.timeRefresh;
}
// REFRESH NEWS EVERY X MINUTES
loadNewsCache(data.feeds);
setInterval(() => {
loadNewsCache(data.feeds);
}, 60000 * timeRefresh);
}
},
plugin(_args) {
const _this = this;
const args = _this.mergeOpts(defaultConfig, _args);
if (args.idChat !== '') {
getNews(args.search.trim())
.then(data => {
_this.sendLink({
"idChat": args.idChat,
"description": data.description,
"title": data.title,
"thumb": data.thumb,
"link": data.urlNews
});
})
.catch(err => {
_this.sendMessage({
"idChat": args.idChat,
"message": args.messageError
});
})
}
}
};
</code></pre>
</article>
</section>
</div>
<footer class="footer">
<div class="content has-text-centered">
<p>Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.6</a></p>
<p class="sidebar-created-by">
<a href="https://github.com/SoftwareBrothers/better-docs" target="_blank">BetterDocs theme</a> provided with <i class="fas fa-heart"></i> by
<a href="http://softwarebrothers.co" target="_blank">SoftwareBrothers - JavaScript Development Agency</a>
</p>
</div>
</footer>
</div>
<div id="side-nav" class="side-nav">
</div>
</div>
<script src="scripts/app.min.js"></script>
<script>PR.prettyPrint();</script>
<script src="scripts/linenumber.js"> </script>
<script src="scripts/search.js"> </script>
</body>
</html>