From 00efac8c31765382dc397f90bb2d06237ab6d3e9 Mon Sep 17 00:00:00 2001 From: David O Neill Date: Sun, 24 Nov 2024 06:17:13 +0000 Subject: [PATCH] fix news --- strategies/News.js | 61 ++-- test.js | 795 --------------------------------------------- web.js | 94 ------ 3 files changed, 33 insertions(+), 917 deletions(-) delete mode 100644 test.js delete mode 100644 web.js diff --git a/strategies/News.js b/strategies/News.js index 7b88e40..c1484d3 100644 --- a/strategies/News.js +++ b/strategies/News.js @@ -305,40 +305,45 @@ class News extends MessageStrategy { return [resshort, resfull] } - - async getLocsFromXML (message, sitemapUrl) { + + async getLocsFromXML(message, sitemapUrl) { const locs = [] - - // Fetch the XML data from the sitemap URL - const xmlData = await MessageStrategy.axiosHttpRequest(message, 'GET', sitemapUrl, false, 200, false) - - // Parse XML data using xml2js - // eslint-disable-next-line no-undef - const parser = new xml2js.Parser({ explicitArray: false }) - const parsedData = await parser.parseStringPromise(xmlData) - - // Check for subsitemaps in tags - if (parsedData.sitemapindex && parsedData.sitemapindex.sitemap) { - const sitemaps = parsedData.sitemapindex.sitemap - // Loop through subsitemaps and recursively fetch locs - for (const sitemap of Array.isArray(sitemaps) ? sitemaps : [sitemaps]) { - const subsitemapLoc = sitemap.loc - const subsitemapLocs = await this.getLocsFromXML(message, subsitemapLoc) - locs.push(...subsitemapLocs) + + try { + // Fetch the XML data from the sitemap URL + const xmlData = await MessageStrategy.axiosHttpRequest(message, 'GET', sitemapUrl, false, 200, false) + + // Parse XML data using xml2js + const parser = new xml2js.Parser({ explicitArray: false }) + const parsedData = await parser.parseStringPromise(xmlData) + + // Check for subsitemaps in tags + if (parsedData.sitemapindex && parsedData.sitemapindex.sitemap) { + const sitemaps = parsedData.sitemapindex.sitemap + // Loop through subsitemaps and recursively fetch locs + for (const sitemap of Array.isArray(sitemaps) ? sitemaps : [sitemaps]) { + const subsitemapLoc = sitemap.loc + const subsitemapLocs = await this.getLocsFromXML(message, subsitemapLoc) + locs.push(...subsitemapLocs) + } } - } - - // Get URLs from tags in the current sitemap - if (parsedData.urlset && parsedData.urlset.url) { - const urls = parsedData.urlset.url - for (const url of Array.isArray(urls) ? urls : [urls]) { - locs.push(url.loc) + + // Get URLs from tags in the current sitemap + if (parsedData.urlset && parsedData.urlset.url) { + const urls = parsedData.urlset.url + for (const url of Array.isArray(urls) ? urls : [urls]) { + locs.push(url.loc) + } } + } catch (error) { + console.error(`Error processing XML from ${sitemapUrl}:`, error) + // Optionally: Re-throw the error or return a default value + // throw error } - + return locs } - + async subscribe (message) { try { const parts = message.body.trim().split(' ') diff --git a/test.js b/test.js deleted file mode 100644 index 965f20c..0000000 --- a/test.js +++ /dev/null @@ -1,795 +0,0 @@ -// const MS = require("./MessageStrategy.js"); -const Hi = require('./strategies/Hi.js') - -// jest.mock('cron'); -// jest.mock('fs'); -// jest.mock('globby'); -// jest.mock('js-levenshtein'); -// jest.mock('sync-request'); -// jest.mock('crypto'); -// jest.mock('coinmarketcap-api'); -// jest.mock('puppeteer'); -// jest.mock('deluge'); -// jest.mock('resize-image-buffer'); -// jest.mock('axios'); -// jest.mock('rawurlencode'); -// jest.mock('name-to-imdb'); -// jest.mock('bing-translate-api'); -// jest.mock('urban-dictionary'); -// jest.mock('weather-js'); -// jest.mock('wikipedia'); -// jest.mock('youtube-search-without-api-key'); -// jest.mock('youtube-thumbnail'); -// jest.mock('node-webcam'); -// jest.mock('child_process'); -// jest.mock('jsdom'); - -const msg = { - from: '', - body: '', - thumbnail: '', - id: false, - sender: { - id: false - } -} - -const client = { - sendText: (to, message) => { }, - sendImage: (to, message, image, text) => { }, - sendLinkWithAutoPreview: (to, message) => { }, - simulateTyping: (chatid) => { }, - reply: (to, message) => { } -} - -const sendTextMock = jest.spyOn(client, 'sendText') -const sendImageMock = jest.spyOn(client, 'sendImage') -const replyTextMock = jest.spyOn(client, 'reply') -const simulateTypingMock = jest.spyOn(client, 'simulateTyping') -const sendLinkWithAutoPreviewMock = jest.spyOn(client, 'sendLinkWithAutoPreview') - -describe('Test strategies', () => { - const message = {} - - beforeEach(async () => { - // MS.client = client; - // MS.update_strategies(); - // message = {}; - // message['chatId'] = "1234567890@c.us"; - // message['sender'] = { - // "id": "1234567890@c.us" - // } - // await new Promise((resolve) => { setTimeout(resolve, 150); }); - }) - - it('should return a list of strategies', async () => { - // console.log(MS.strategies); - // expect(Object.keys(MS.strategies).length).toBeGreaterThan(0); - }) - - it('check hi strategy in stategies', async () => { - // expect(Object.keys(MS.strategies).includes('Hi')).toBe(true); - }) - - it('check all strategies enabled', async () => { - // Object.keys(MS.strategies).forEach(element => { - // expect(MS.strategies[element].enabled).toBe(true); - // }); - }) - - it('check all strategies have provides', async () => { - // Object.keys(MS.strategies).forEach(element => { - // expect(Array.isArray(MS.strategies[element].provides())).toBe(true); - // }); - }) - - it('check all strategies handles message', async () => { - // Object.keys(MS.strategies).forEach(element => { - // message['body'] = "£££total nonsense***"; - // message['from'] = "3538619387876@c.us"; - // expect(MS.strategies[element].handleMessage(message, MS.strategies)).toBe(false); - // }); - }) -}) - -describe('Test Hi strategy', () => { - const mockTyping = jest.fn() - const message = {} - let test_obj = null - - beforeEach(async () => { - test_obj = new Hi() - }) - - it('Check Hi enabled', async () => { - expect(test_obj.enabled).toBe(true) - }) - - it('Hi returns true', async () => { - message.body = 'hi' - message.from = '3538619387876@c.us' - expect(test_obj.handleMessage(message)).toBe(true) - expect(mockTyping).toHaveBeenCalled() - }) - - it('Hi returns false', async () => { - message.body = 'who' - message.from = '3538619387876@c.us' - expect(test_obj.handleMessage(message, MS.strategies)).toBe(false) - }) -}) - -// describe('Test Spam strategy', () => { -// let mockTyping = jest.fn(); -// let message = {}; - -// beforeEach(async () => { -// MS.client = client; -// MS.typing = mockTyping; -// MS.update_strategies(); -// message = {}; -// message['chatId'] = "1234567890@c.us"; -// message['sender'] = { -// "id": "1234567890@c.us" -// }; -// await new Promise((resolve) => { setTimeout(resolve, 150); }); -// }); - -// it('Check Spam enabled', async () => { -// const Spam = MS.strategies.Spam; -// expect(Spam.enabled).toBe(true); -// }); - -// it('Spam returns true', async () => { -// const Spam = MS.strategies.Spam; -// message['body'] = "who"; -// message['from'] = "3538619387876@c.us"; -// //expect(Spam.handleMessage(message, strategies)).toBe(true); -// }); - -// it('Spam returns false', async () => { -// const Spam = MS.strategies.Spam; -// message['body'] = "who"; -// message['from'] = "3538619387876@c.us"; -// expect(Spam.handleMessage(message, MS.strategies)).toBe(false); -// }); -// }); - -// describe('Test Help strategy', () => { -// let mockTyping = jest.fn(); -// let message = {}; -// let strategies = null; - -// beforeEach(async () => { -// MS.client = client; -// MS.typing = mockTyping; -// MS.update_strategies(); -// message = {}; -// message['body'] = "who"; -// message['from'] = "3538619387876@c.us"; -// await new Promise((resolve) => { setTimeout(resolve, 150); }); -// }); - -// it('Check Help enabled', async () => { -// const Help = strategies.Help; -// expect(Help.enabled).toBe(true); -// }); - -// it('Help returns true', async () => { -// const Help = strategies.Help; -// //expect(Help.handleMessage(message, strategies)).toBe(true); -// //expect(mockTyping).toHaveBeenCalled(); -// }); - -// it('Help returns false', async () => { -// const Help = strategies.Help; -// message['body'] = "not help"; -// expect(Help.handleMessage(message, strategies)).toBe(false); -// }); -// }); - -// describe('Test Harass strategy', () => { -// let mockTyping = jest.fn(); -// let message = {}; -// let strategies = null; - -// beforeEach(async () => { -// MS.client = client; -// MS.typing = mockTyping; -// MS.update_strategies(); -// message = {}; -// message['body'] = "who"; -// message['from'] = "3538619387876@c.us"; -// await new Promise((resolve) => { setTimeout(resolve, 150); }); -// }); - -// it('Check Harass enabled', async () => { -// const Harass = strategies.Harass; -// expect(Harass.enabled).toBe(true); -// }); - -// it('Harass returns true', async () => { -// const Harass = strategies.Harass; -// //expect(Harass.handleMessage(message, strategies)).toBe(true); -// //expect(mockTyping).toHaveBeenCalled(); -// }); - -// it('Harass returns false', async () => { -// const Harass = strategies.Harass; -// message['body'] = "not harass"; -// expect(Harass.handleMessage(message, strategies)).toBe(false); -// }); -// }); - -// describe('Test ChuckJokes strategy', () => { -// let mockTyping = jest.fn(); -// let message = {}; -// let strategies = null; - -// beforeEach(async () => { -// MS.client = client; -// MS.typing = mockTyping; -// MS.update_strategies(); -// message = {}; -// message['body'] = "who"; -// message['from'] = "3538619387876@c.us"; -// await new Promise((resolve) => { setTimeout(resolve, 150); }); -// }); - -// it('Check ChuckJokes enabled', async () => { -// const ChuckJokes = strategies.ChuckJokes; -// expect(ChuckJokes.enabled).toBe(true); -// }); - -// it('ChuckJokes returns true', async () => { -// const ChuckJokes = strategies.ChuckJokes; -// //expect(ChuckJokes.handleMessage(message, strategies)).toBe(true); -// //expect(mockTyping).toHaveBeenCalled(); -// }); - -// it('ChuckJokes returns false', async () => { -// const ChuckJokes = strategies.ChuckJokes; -// message['body'] = "not lul"; -// expect(ChuckJokes.handleMessage(message, strategies)).toBe(false); -// }); -// }); - -// describe('Test Asthanga strategy', () => { -// let mockTyping = jest.fn(); -// let message = {}; -// let strategies = null; - -// beforeEach(async () => { -// MS.client = client; -// MS.typing = mockTyping; -// MS.update_strategies(); -// message = {}; -// message['body'] = "who"; -// message['from'] = "3538619387876@c.us"; -// await new Promise((resolve) => { setTimeout(resolve, 150); }); -// }); - -// it('Check Asthanga enabled', async () => { -// const Asthanga = strategies.Asthanga; -// expect(Asthanga.enabled).toBe(true); -// }); - -// it('Asthanga returns true', async () => { -// const Asthanga = strategies.Asthanga; -// message['body'] = "navasana"; -// message['from'] = "3538619387876@c.us"; -// expect(Asthanga.handleMessage(message, strategies)).toBe(true); -// expect(mockTyping).toHaveBeenCalled(); -// }); - -// it('Asthanga returns false', async () => { -// const Asthanga = strategies.Asthanga; -// message['body'] = "not poses"; -// expect(Asthanga.handleMessage(message, strategies)).toBe(false); -// }); -// }); - -// describe('Test Youtube strategy', () => { -// let mockTyping = jest.fn(); -// let message = {}; -// let strategies = null; - -// beforeEach(async () => { -// MS.client = client; -// MS.typing = mockTyping; -// MS.update_strategies(); -// message = {}; -// await new Promise((resolve) => { setTimeout(resolve, 150); }); -// }); - -// it('Check Youtube enabled', async () => { -// const Youtube = strategies.Youtube; -// expect(Youtube.enabled).toBe(true); -// }); - -// it('Youtube search returns true', async () => { -// const Youtube = strategies.Youtube; -// message['body'] = "youtube hot wings"; -// message['from'] = "3538619387876@c.us"; -// expect(Youtube.handleMessage(message, strategies)).toBe(true); -// // expect(mockTyping).toHaveBeenCalled(); -// }); - -// it('Youtube returns false', async () => { -// const Youtube = strategies.Youtube; -// message['body'] = "not youtube xrp"; -// expect(Youtube.handleMessage(message, strategies)).toBe(false); -// }); -// }); - -// describe('Test TikTok strategy', () => { -// let mockTyping = jest.fn(); -// let message = {}; -// let strategies = null; - -// beforeEach(async () => { -// MS.client = client; -// MS.typing = mockTyping; -// MS.update_strategies(); -// message = {}; -// await new Promise((resolve) => { setTimeout(resolve, 150); }); -// }); - -// it('Check TikTok enabled', async () => { -// const TikTok = strategies.TikTok; -// expect(TikTok.enabled).toBe(true); -// }); - -// it('TikTok returns true', async () => { -// const TikTok = strategies.TikTok; -// message['body'] = "https://vm.tiktok.com/ddwssa32s"; -// message['from'] = "3538619387876@c.us"; -// postTiktokPreviewMock = jest.spyOn(TikTok, "postTiktokPreview"); -// expect(TikTok.handleMessage(message, strategies)).toBe(true); -// // expect(mockTyping).toHaveBeenCalled(); -// }); - -// it('TikTok returns false', async () => { -// const TikTok = strategies.TikTok; -// message['body'] = "not http://vm.tiktok.com/"; -// expect(TikTok.handleMessage(message, strategies)).toBe(false); -// }); -// }); - -// describe('Test Twitter strategy', () => { -// let mockTyping = jest.fn(); -// let message = {}; -// let strategies = null; - -// beforeEach(async () => { -// MS.client = client; -// MS.typing = mockTyping; -// MS.update_strategies(); -// message = {}; -// await new Promise((resolve) => { setTimeout(resolve, 150); }); -// }); - -// it('Check Twitter enabled', async () => { -// const Twitter = strategies.Twitter; -// expect(Twitter.enabled).toBe(true); -// }); - -// it('Twitter returns true', async () => { -// const Twitter = strategies.Twitter; -// message['body'] = "https://twitter.com/ddwssa32s"; -// message['from'] = "3538619387876@c.us"; -// postTwitterPreviewMock = jest.spyOn(Twitter, "postTwitterPreview"); -// expect(Twitter.handleMessage(message, strategies)).toBe(true); -// // expect(mockTyping).toHaveBeenCalled(); -// }); - -// it('Twitter returns false', async () => { -// const Twitter = strategies.Twitter; -// message['body'] = "not http://twitter.com/"; -// expect(Twitter.handleMessage(message, strategies)).toBe(false); -// }); -// }); - -// describe('Test Facebook strategy', () => { -// let mockTyping = jest.fn(); -// let message = {}; -// let strategies = null; - -// beforeEach(async () => { -// MS.client = client; -// MS.typing = mockTyping; -// MS.update_strategies(); -// message = {}; -// await new Promise((resolve) => { setTimeout(resolve, 150); }); -// }); - -// it('Check Facebook enabled', async () => { -// const Facebook = strategies.Facebook; -// expect(Facebook.enabled).toBe(true); -// }); - -// it('Facebook returns true', async () => { -// const Facebook = strategies.Facebook; -// message['body'] = "https://www.facebook.com/ggggg"; -// message['from'] = "3538619387876@c.us"; -// postFacebookPreviewMock = jest.spyOn(Facebook, "postFacebookPreview"); -// expect(Facebook.handleMessage(message, strategies)).toBe(true); -// //expect(mockTyping).toHaveBeenCalled(); -// }); - -// it('Facebook returns false', async () => { -// const Facebook = strategies.Facebook; -// message['body'] = "not facebook"; -// expect(Facebook.handleMessage(message, strategies)).toBe(false); -// }); -// }); - -// describe('Test HyperLink strategy', () => { -// let mockTyping = jest.fn(); -// let message = {}; -// let strategies = null; - -// beforeEach(async () => { -// MS.client = client; -// MS.typing = mockTyping; -// MS.update_strategies(); -// message = {}; -// await new Promise((resolve) => { setTimeout(resolve, 150); }); -// }); - -// it('Check HyperLink enabled', async () => { -// const HyperLink = strategies.HyperLink; -// expect(HyperLink.enabled).toBe(true); -// }); - -// it('HyperLink returns true', async () => { -// const HyperLink = strategies.HyperLink; -// message['body'] = "https://www.google.com/test"; -// message['from'] = "3538619387876@c.us"; -// message['thumbnail'] = ""; -// expect(HyperLink.handleMessage(message, strategies)).toBe(true); -// // expect(mockTyping).toHaveBeenCalled(); -// }); - -// it('HyperLink returns false', async () => { -// const HyperLink = strategies.HyperLink; -// message['body'] = "not http://google.com"; -// expect(HyperLink.handleMessage(message, strategies)).toBe(false); -// }); -// }); - -// describe('Test Currency strategy', () => { -// let mockTyping = jest.fn(); -// let message = {}; -// let strategies = null; - -// beforeEach(async () => { -// MS.client = client; -// MS.typing = mockTyping; -// MS.update_strategies(); -// message = {}; -// await new Promise((resolve) => { setTimeout(resolve, 150); }); -// }); - -// it('Check Currency enabled', async () => { -// const Currency = strategies.Currency; -// expect(Currency.enabled).toBe(true); -// }); - -// it('Currency returns true', async () => { -// const Currency = strategies.Currency; -// //expect(Currency.handleMessage(message, strategies)).toBe(true); -// //expect(mockTyping).toHaveBeenCalled(); -// }); - -// it('Currency returns false', async () => { -// const Currency = strategies.Currency; -// message['body'] = "not fiat"; -// expect(Currency.handleMessage(message, strategies)).toBe(false); -// }); -// }); - -// describe('Test Crypto strategy', () => { -// let mockTyping = jest.fn(); -// let message = {}; -// let strategies = null; - -// beforeEach(async () => { -// MS.client = client; -// MS.typing = mockTyping; -// MS.update_strategies(); -// message = {}; -// await new Promise((resolve) => { setTimeout(resolve, 150); }); -// }); - -// it('Check Crypto enabled', async () => { -// const Crypto = strategies.Crypto; -// expect(Crypto.enabled).toBe(true); -// }); - -// it('Crypto list', async () => { -// const Crypto = strategies.Crypto; -// message['body'] = "coin"; -// message['from'] = "3538619387876@c.us"; -// expect(Crypto.handleMessage(message, strategies)).toBe(true); -// expect(mockTyping).toHaveBeenCalled(); -// }); - -// it('Crypto get coin', async () => { -// const Crypto = strategies.Crypto; -// message['body'] = "coin xrp"; -// message['from'] = "3538619387876@c.us"; -// expect(Crypto.handleMessage(message, strategies)).toBe(true); -// expect(mockTyping).toHaveBeenCalled(); -// }); - -// it('Crypto returns false', async () => { -// const Crypto = strategies.Crypto; -// message['body'] = "not coin"; -// expect(Crypto.handleMessage(message, strategies)).toBe(false); -// }); -// }); - -// describe('Test Imdb strategy', () => { -// let mockTyping = jest.fn(); -// let message = {}; -// let strategies = null; - -// beforeEach(async () => { -// MS.client = client; -// MS.typing = mockTyping; -// MS.update_strategies(); -// message = {}; -// await new Promise((resolve) => { setTimeout(resolve, 150); }); -// }); - -// it('Check Imdb enabled', async () => { -// const Imdb = strategies.Imdb; -// expect(Imdb.enabled).toBe(true); -// }); - -// it('Imdb returns true', async () => { -// const Imdb = strategies.Imdb; -// message['body'] = "imdb bad boys"; -// message['from'] = "3538619387876@c.us"; -// expect(Imdb.handleMessage(message, strategies)).toBe(true); -// // expect(mockTyping).toHaveBeenCalled(); -// }); - -// it('Imdb returns false', async () => { -// const Imdb = strategies.Imdb; -// message['body'] = "not imdb bad boys"; -// expect(Imdb.handleMessage(message, strategies)).toBe(false); -// }); -// }); - -// describe('Test Google strategy', () => { -// let mockTyping = jest.fn(); -// let message = {}; -// let strategies = null; - -// beforeEach(async () => { -// MS.client = client; -// MS.typing = mockTyping; -// MS.update_strategies(); -// message = {}; -// await new Promise((resolve) => { setTimeout(resolve, 150); }); -// }); - -// it('Check Google enabled', async () => { -// const Google = strategies.Google; -// expect(Google.enabled).toBe(true); -// }); - -// it('Google returns true', async () => { -// const Google = strategies.Google; -// message['body'] = "google ttt"; -// message['from'] = "3538619387876@c.us"; -// expect(Google.handleMessage(message, strategies)).toBe(true); -// }); - -// it('Google returns false', async () => { -// const Google = strategies.Google; -// message['body'] = "not google cork"; -// expect(Google.handleMessage(message, strategies)).toBe(false); -// }); -// }); - -// describe('Test Wikipedia strategy', () => { -// let mockTyping = jest.fn(); -// let message = {}; -// let strategies = null; - -// beforeEach(async () => { -// MS.client = client; -// MS.typing = mockTyping; -// MS.update_strategies(); -// message = {}; -// await new Promise((resolve) => { setTimeout(resolve, 150); }); -// }); - -// it('Check Wikipedia enabled', async () => { -// const Wikipedia = strategies.Wikipedia; -// expect(Wikipedia.enabled).toBe(true); -// }); - -// it('Wikipedia returns true', async () => { -// const Wikipedia = strategies.Wikipedia; -// message['body'] = "wiki ff"; -// message['from'] = "3538619387876@c.us"; -// expect(Wikipedia.handleMessage(message, strategies)).toBe(true); -// }); - -// it('Wikipedia returns false', async () => { -// const Wikipedia = strategies.Wikipedia; -// message['body'] = "not wikipedia cork"; -// expect(Wikipedia.handleMessage(message, strategies)).toBe(false); -// }); -// }); - -// describe('Test Weather strategy', () => { -// let mockTyping = jest.fn(); -// let message = {}; -// let strategies = null; - -// beforeEach(async () => { -// MS.client = client; -// MS.typing = mockTyping; -// MS.update_strategies(); -// message = {}; -// message['body'] = "who"; -// message['from'] = "3538619387876@c.us"; -// await new Promise((resolve) => { setTimeout(resolve, 150); }); -// }); - -// it('Check Weather enabled', async () => { -// const Weather = strategies.Weather; -// expect(Weather.enabled).toBe(true); -// }); - -// it('Weather returns true', async () => { -// const Weather = strategies.Weather; -// message['body'] = "weather cork"; -// message['from'] = "3538619387876@c.us"; -// expect(Weather.handleMessage(message, strategies)).toBe(true); -// // expect(mockTyping).toHaveBeenCalled(); -// }); - -// it('Weather returns false', async () => { -// const Weather = strategies.Weather; -// message['body'] = "not weather cork"; -// expect(Weather.handleMessage(message, strategies)).toBe(false); -// }); -// }); - -// describe('Test Levenshteiner strategy', () => { -// let mockTyping = jest.fn(); -// let message = {}; -// let strategies = null; - -// beforeEach(async () => { -// MS.client = client; -// MS.typing = mockTyping; -// MS.update_strategies(); -// message = {}; -// await new Promise((resolve) => { setTimeout(resolve, 150); }); -// }); - -// it('Check Levenshteiner enabled', async () => { -// const Levenshteiner = strategies.Levenshteiner; -// expect(Levenshteiner.enabled).toBe(true); -// }); - -// it('Levenshteiner returns true', async () => { -// const Levenshteiner = strategies.Levenshteiner; -// message['body'] = "levenshtein gg ttt"; -// message['from'] = "3538619387876@c.us"; -// expect(Levenshteiner.handleMessage(message, strategies)).toBe(true); -// expect(mockTyping).toHaveBeenCalled(); -// }); - -// it('Levenshteiner returns false', async () => { -// const Levenshteiner = strategies.Levenshteiner; -// message['body'] = "not levenshtein gg ttt"; -// expect(Levenshteiner.handleMessage(message, strategies)).toBe(false); -// }); -// }); - -// describe('Test UrbanDictionary strategy', () => { -// let mockTyping = jest.fn(); -// let message = {}; -// let strategies = null; - -// beforeEach(async () => { -// MS.client = client; -// MS.typing = mockTyping; -// MS.update_strategies(); -// message = {}; -// await new Promise((resolve) => { setTimeout(resolve, 150); }); -// }); - -// it('Check UrbanDictionary enabled', async () => { -// const UrbanDictionary = strategies.UrbanDictionary; -// expect(UrbanDictionary.enabled).toBe(true); -// }); - -// it('UrbanDictionary returns true', async () => { -// const UrbanDictionary = strategies.UrbanDictionary; -// message['body'] = "urban"; -// message['from'] = "3538619387876@c.us"; -// postUrbanQuoteMock = jest.spyOn(UrbanDictionary, "postUrbanQuote"); -// expect(UrbanDictionary.handleMessage(message, strategies)).toBe(true); -// }); - -// it('UrbanDictionary returns false', async () => { -// const UrbanDictionary = strategies.UrbanDictionary; -// message['body'] = "not urban tosser"; -// message['from'] = "3538619387876@c.us"; -// expect(UrbanDictionary.handleMessage(message, strategies)).toBe(false); -// }); -// }); - -// describe('Test Translate strategy', () => { -// let mockTyping = jest.fn(); -// let message = {}; -// let strategies = null; - -// beforeEach(async () => { -// MS.client = client; -// MS.typing = mockTyping; -// MS.update_strategies(); -// message = {}; -// message['body'] = "who"; -// message['from'] = "3538619387876@c.us"; -// await new Promise((resolve) => { setTimeout(resolve, 150); }); -// }); - -// it('Check Translate enabled', async () => { -// const Translate = strategies.Translate; -// expect(Translate.enabled).toBe(true); -// }); - -// it('Translate returns false', async () => { -// const Translate = strategies.Translate; -// message['body'] = "translate"; -// expect(Translate.handleMessage(message, strategies)).toBe(false); -// }); - -// it('Translate returns false', async () => { -// const Translate = strategies.Translate; -// expect(Translate.handleMessage(message, strategies)).toBe(false); -// }); - -// it('Translate set default language empty', async () => { -// const Translate = strategies.Translate; -// message['body'] = "translate default"; -// expect(Translate.handleMessage(message, strategies)).toBe(false); -// }); - -// it('Translate set default language not empty', async () => { -// const Translate = strategies.Translate; -// message['body'] = "translate default de"; -// expect(Translate.handleMessage(message, strategies)).toBe(true); -// expect(Object.keys(Translate.get_defaults()).includes("3538619387876@c.us")).toBe(true); -// expect(Translate.get_defaults()["3538619387876@c.us"] == "de").toBe(true); -// }); - -// it('Translate set default language off', async () => { -// const Translate = strategies.Translate; -// message['body'] = "translate off"; -// expect(Translate.handleMessage(message, strategies)).toBe(true); -// expect(Object.keys(Translate.get_defaults()).includes("3538619387876@c.us")).toBe(false); -// }); - -// it('Translate from en to pt', async () => { -// const Translate = strategies.Translate; -// message['body'] = "translate en/pt this is a test"; -// expect(Translate.handleMessage(message, strategies)).toBe(true); -// }); - -// it('Translate from en to fdfdsfsdf', async () => { -// const Translate = strategies.Translate; -// message['body'] = "translate en/fdfdsfsdf this is a test"; -// expect(Translate.handleMessage(message, strategies)).toBe(false); -// }); -// }); diff --git a/web.js b/web.js deleted file mode 100644 index b910199..0000000 --- a/web.js +++ /dev/null @@ -1,94 +0,0 @@ -const express = require('express') -const app = express() -const bodyParser = require('body-parser') - -// #################################### -// Listener -// #################################### - -class WebInterface { - message_strategy = null - server = null - - constructor (MessageStrategy) { - this.message_strategy = MessageStrategy - } - - stop () { - if (this.server) { - this.server.close() - } - } - - async launch (MessageStrategy) { - try { - this.message_strategy = MessageStrategy - this.message_strategy.prototype.call_update_active_chat_contacts() - - await new Promise(r => setTimeout(r, 1000)) - - const port = Math.floor(Math.random() * (4000 - 3000 + 1) + 3000) - const jsonParser = bodyParser.json() - const urlencodedParser = bodyParser.urlencoded({ extended: false }) - - app.use(express.json({ extended: true, limit: '1mb' })) - - app.get('/', (req, res) => { - res.send('Hello World!') - }) - - app.post('/', (req, res) => { - res.send('Hello World!') - }) - - app.get('/get_contacts', (req, res) => { - res.send(JSON.stringify(this.message_strategy.prototype.get_contacts().sort())) - }) - - app.get('/get_contacts_verbose', (req, res) => { - res.send(JSON.stringify(this.message_strategy.prototype.get_contacts_verbose().sort())) - }) - - app.get('/get_groups', (req, res) => { - res.send(JSON.stringify(this.message_strategy.prototype.get_groups().sort())) - }) - - app.get('/get_groups_verbose', (req, res) => { - res.send(JSON.stringify(this.message_strategy.prototype.get_groups_verbose().sort())) - }) - - app.post('/send_message', urlencodedParser, (req, res) => { - const { contact, message } = req.body - - const msg = { - from: contact, - body: message, - thumbnail: '', - id: false, - sender: { - id: false - } - } - - ChuckBot.Strategies.doHandleMessage(ChuckBot.chuck, msg) - - const logline = 'Sending "' + message + '" to ' + contact - // this.client.sendText(contact, message); - // console.log(logline); - res.send(logline) - }) - - this.server = app.listen(port, () => { - console.log('\n==============================================\n') - console.log(`Example app listening on port ${port}`) - console.log('\n==============================================\n\n') - }) - } catch (err) { - console.log(err) - } - } -} - -module.exports = { - Web: WebInterface -}