Skip to content

Commit

Permalink
Fixes for sending images
Browse files Browse the repository at this point in the history
  • Loading branch information
dmzoneill committed Oct 16, 2024
1 parent 95bf6a9 commit 9fef6c2
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 82 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
**/COVID_HELPER.data.json
**/coincap-api.key
**/strategies/config/**
**/strategies/webcam/**
# managed by open-wa
**.data.json
**.node-persist**
Expand Down
11 changes: 7 additions & 4 deletions MessageStrategy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const CronJob = require('cron').CronJob
const fs = require('fs')
const path = require('path');
let globby = undefined
const levenshtein = require('js-levenshtein')
const request = require('sync-request')
Expand Down Expand Up @@ -27,10 +28,12 @@ const ccxt = require('ccxt')
const asciichart = require('asciichart')
const IG = require('instagram-web-api')
const usetube = require('usetube')
const { Innertube } = require('youtubei.js');
const talib = require('talib');
const { createCanvas } = require('canvas');
const worldFlags = require('./lib/flags.js');
const browserConfig = require('./lib/browserconfig.js')
const xml2js = require('xml2js');

// Import modules asynchronously
Promise.all([
Expand Down Expand Up @@ -543,7 +546,7 @@ class MessageStrategy {
const page = await self.get_page(self, fullurl, wait)
if (!page) {
console.log("Failed to get page");
return [null, null];
return [null, null, null];
}

const data = await page.evaluate(() => document.querySelector('*').outerHTML);
Expand Down Expand Up @@ -573,15 +576,15 @@ class MessageStrategy {

if (image_url == null) {
console.log("get_page_og_data image_url is null")
return [null, null]
return [null, null, null]
} else {
const desc = description == null ? title : description
MessageStrategy.typing(self.message)
return [desc, await MessageStrategy.get_image(image_url, 320, data_url)]
return [desc, await MessageStrategy.get_image(image_url, 320, data_url), title]
}
} catch (err) {
console.log(err)
return [null, null]
return [null, null, null]
}
}

Expand Down
53 changes: 48 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
"usetube": "^2.2.7",
"weather-js": "^2.0.0",
"wikipedia": "^2.1.2",
"xml2js": "^0.6.2",
"xml2json": "^0.12.0",
"yahoo-stock-api": "^2.2.1",
"yaml": "^2.4.2",
"youtube-search-without-api-key": "^2.0.1"
"youtube-search-without-api-key": "^2.0.1",
"youtubei.js": "^10.5.0"
},
"devDependencies": {
"@types/jest": "^29.5.12",
Expand Down
2 changes: 1 addition & 1 deletion state.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion strategies/Facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Facebook extends MessageStrategy {
}

// MessageStrategy.client.sendLinkWithAutoPreview(message.from, message.body, data[0], data[1])
MessageStrategy.client.sendMessageWithThumb(data[1], message.body, "Facebook", data[0], "", message.from)
MessageStrategy.client.sendMessageWithThumb(data[1], message.body, 'Facebook', data[0], '', message.from)
} catch (err) {
console.log(err)
}
Expand Down
4 changes: 2 additions & 2 deletions strategies/Hyperlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ class HyperLink extends MessageStrategy {
const data = await HyperLink.self.get_page_og_data(HyperLink.self, message.body, 500)

if (data[1] == null) {
MessageStrategy.client.reply(message.from, 'Sorry no preview', message.id, true)
// MessageStrategy.client.reply(message.from, 'Sorry no preview', message.id, true)
return false
}

// MessageStrategy.client.sendLinkWithAutoPreview(message.from, message.body, data[0], data[1])
MessageStrategy.client.sendMessageWithThumb(data[1], message.body, "Preview", data[0], "", message.from)
MessageStrategy.client.sendMessageWithThumb(data[1], message.body, data[2], data[0], '', message.from)
return true
} catch (err) {
console.log(err)
Expand Down
87 changes: 28 additions & 59 deletions strategies/News.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,30 +306,36 @@ class News extends MessageStrategy {
return [resshort, resfull]
}

async getLocsFromXML(message, url) {
let urls = []
try {
let xml = await MessageStrategy.axiosHttpRequest(message, 'GET', url, false, 200, false)
let json = x2j.toJson(xml)
json = JSON.parse(json)

if (json.hasOwnProperty('urlset')) {
for (let i = 0; i < json['urlset']['url'].length; i++) {
urls.push(json['urlset']['url'][i]['loc']);
async getLocsFromXML(message, sitemapUrl) {
let locs = [];

// Fetch the XML data from the sitemap URL
let xmlData = await MessageStrategy.axiosHttpRequest(message, 'GET', sitemapUrl, false, 200, false);

// Parse XML data using xml2js
let parser = new xml2js.Parser({ explicitArray: false });
let parsedData = await parser.parseStringPromise(xmlData);

// Check for subsitemaps in <sitemap> tags
if (parsedData.sitemapindex && parsedData.sitemapindex.sitemap) {
let sitemaps = parsedData.sitemapindex.sitemap;
// Loop through subsitemaps and recursively fetch locs
for (let sitemap of Array.isArray(sitemaps) ? sitemaps : [sitemaps]) {
let subsitemapLoc = sitemap.loc;
let subsitemapLocs = await this.getLocsFromXML(message, subsitemapLoc);
locs.push(...subsitemapLocs);
}
}
}

if (json.hasOwnProperty('rss')) {
for (let i = 0; i < json['rss']['channel']['item'].length; i++) {
let item = json['rss']['channel']['item'][i]
urls.push(item['link']);
// Get URLs from <url> tags in the current sitemap
if (parsedData.urlset && parsedData.urlset.url) {
let urls = parsedData.urlset.url;
for (let url of Array.isArray(urls) ? urls : [urls]) {
locs.push(url.loc);
}
}
} catch (err) {
console.log(err);
}

return urls;
return locs;
}

async subscribe(message) {
Expand All @@ -343,8 +349,6 @@ class News extends MessageStrategy {

let sitemaps = await News.self.getSiteMaps(message, parts[2])

console.log(sitemaps)

if (sitemaps[0].length == 0) {
MessageStrategy.client.sendText(message.from, "No sitemaps or robots.txt found.")
return
Expand All @@ -369,97 +373,67 @@ class News extends MessageStrategy {
return;
}

console.log("1")

if (Object.keys(MessageStrategy.state).indexOf('News') == -1) {
MessageStrategy.state['News'] = {}
}

console.log("2")

if (Object.keys(MessageStrategy.state['News']).indexOf('enabled') == -1) {
MessageStrategy.state['News']['enabled'] = true
}

console.log("3")

if (Object.keys(MessageStrategy.state['News']).indexOf('Subscribed') == -1) {
MessageStrategy.state['News']['Subscribed'] = {}
}

console.log("4")

if (Object.keys(MessageStrategy.state['News']).indexOf('Notified') == -1) {
MessageStrategy.state['News']['Notified'] = {}
}

console.log("5")

if (Object.keys(MessageStrategy.state['News']).indexOf('LastNotified') == -1) {
MessageStrategy.state['News']['LastNotified'] = {}
}

console.log("6")

if (Object.keys(MessageStrategy.state['News']).indexOf('Filter') == -1) {
MessageStrategy.state['News']['Filter'] = {}
}

console.log("7")

if (Object.keys(MessageStrategy.state['News']['Filter']).indexOf(message.from) == -1) {
MessageStrategy.state['News']['Filter'][message.from] = []
}

console.log("8")

if (Object.keys(MessageStrategy.state['News']['Subscribed']).indexOf(message.from) == -1) {
MessageStrategy.state['News']['Subscribed'][message.from] = []
}

console.log("9")

if (MessageStrategy.state['News']['Subscribed'][message.from].indexOf(sitemaps[1][index]) == -1) {
MessageStrategy.state['News']['Subscribed'][message.from].push(sitemaps[1][index])
} else {
MessageStrategy.client.sendText(message.from, "Already subscribed");
return
}

console.log("10")

if (Object.keys(MessageStrategy.state['News']['Notified']).indexOf(message.from) == -1) {
MessageStrategy.state['News']['Notified'][message.from] = {}
}

console.log("12")

if (Object.keys(MessageStrategy.state['News']['Notified'][message.from]).indexOf(sitemaps[1][index]) == -1) {
MessageStrategy.state['News']['Notified'][message.from][sitemaps[1][index]] = []
}

console.log("13")

if (Object.keys(MessageStrategy.state['News']['LastNotified']).indexOf(message.from) == -1) {
MessageStrategy.state['News']['LastNotified'][message.from] = {}
}

console.log("14")

if (Object.keys(MessageStrategy.state['News']['LastNotified'][message.from]).indexOf(sitemaps[1][index]) == -1) {
MessageStrategy.state['News']['LastNotified'][message.from][sitemaps[1][index]] = 0
}

console.log("15")

MessageStrategy.state['News']['Queue'] = {}

for (let i = 0; i < urls.length; i++) {
MessageStrategy.state['News']['Notified'][message.from][sitemaps[1][index]].push(urls[i])
}

console.log("17")

MessageStrategy.client.sendText(message.from, "Subscribed")
News.self.post(message)
}
Expand Down Expand Up @@ -548,14 +522,9 @@ class News extends MessageStrategy {
}
}

if (allowed) {
console.log("================================")
console.log(chat)
console.log(url)

//MessageStrategy.client.sendLinkWithAutoPreview(chat, url, data[0], data[1])
console.log("here")
console.log("================================")
if (allowed) {
// MessageStrategy.client.sendLinkWithAutoPreview(chat, url, data[0], data[1])
MessageStrategy.client.sendMessageWithThumb(data[1], url, data[2], data[0], '', chat)
}
}
}
Expand Down
Loading

0 comments on commit 9fef6c2

Please sign in to comment.