Files
matrix-searxng/example.js

100 lines
3.5 KiB
JavaScript
Raw Normal View History

2023-02-11 22:27:52 -05:00
const sdk = require("matrix-js-sdk");
const matrixcs = require("matrix-js-sdk/lib/matrix");
const request = require('request');
matrixcs.request(request);
const startupTime = Date.now();
// --------- The following settings are all required: ----------------------
// Provide a matrix ID, access token, and homeserver that the bot will use.
2023-02-11 22:27:52 -05:00
const myUserId = "@glowy:glowers.club"
const myAccessToken = "syt_TotallyMyRealAccessTokenAndNotAFake";
const homeserver = "https://glowers.club";
// Provide the link to SearXNG. Include any directories used in a search query.
const searxng_server = "https://searxng.example.com/searxng"
// --------------------------------------------------------------------------
2023-02-11 22:27:52 -05:00
const matrixClient = sdk.createClient({
baseUrl: homeserver,
2023-02-11 22:27:52 -05:00
accessToken: myAccessToken,
userId: myUserId
});
const Searx = require('./index.js');
const searx = new Searx(matrixClient)
searx.setSearxInstance(searxng_server);
// --------- The following settings are all optional: ----------------------
2023-02-11 22:27:52 -05:00
// This allows you to use a different Matrix user and homeserver for uploading the image results. If this is not provided then the bot's Matrix ID and homeserver will be used instead.
2025-12-15 22:20:56 -05:00
/*
2023-02-11 22:27:52 -05:00
const uploadMatrixClient = sdk.createClient({
baseUrl: "https://midov.pl",
accessToken: "syt_AnotherRealAccessTokenDoNotCommit",
userId: "@upload_leech:midov.pl"
});
searx.setUploadMatrixClient(uploadMatrixClient);
2025-12-15 22:20:56 -05:00
*/
// Set which rooms are not allowed to search for images.
2025-12-15 22:20:56 -05:00
//searx.setBlacklistedImgRooms(["!wehateimagesearching:matrix.org", "!wehateittoo:nerdsin.space"]);
// Set which rooms are not allowed to search for images with safesearch disabled.
2025-12-15 22:20:56 -05:00
//searx.setBlacklistedUimgRooms(["!sfw:matrix.org", "!church:nerdsin.space"]);
// Set the amount of time in milliseconds users in a room are required to wait between searches (and allow some users to be immune to the cooldown and send as many as they want)
2025-12-15 22:20:56 -05:00
/*
searx.setCooldownImmuneUsers(["@q:glowers.club", "@romanticusgondolaticus:glowers.club"]);
2023-02-11 22:27:52 -05:00
searx.setRoomCooldowns(new Map([
["!sCbWouzHiLEyfOAaSJ:midov.pl",30000]
]));
2025-12-15 22:20:56 -05:00
*/
// Lets you set which URL parameters are used in SearXNG queries. Useful for changing which search engines are included and excluded. See SearXNG's documentation for all of the parameters available.
2025-12-15 22:20:56 -05:00
//searx.setSearxParameters("disabled_engines=flickr__images");
2023-02-11 22:27:52 -05:00
// Lets you add a whitelist for a room. When added, only the users in that whitelist will be able to search. Other users will receive an error.
2025-12-15 22:20:56 -05:00
/*
searxng.setRoomUserWhitelists(new Map([
["!blahblahblah:midov.pl",
[
"@q:glowers.club",
"@romanticusgondolaticus:glowers.club"
]
]
]));
2025-12-15 22:20:56 -05:00
*/
// --------------------------------------------------------------------------
2023-02-11 22:27:52 -05:00
matrixClient.on("Room.timeline", function(event, room, toStartOfTimeline) {
if (event.getTs() >= startupTime && event.getType() == "m.room.message" && event.getContent().body != null) {
if (event.getContent().body.indexOf("!help") == 0) {
const helpMessage = "Commands:\n"+searx.getHelp();
const formattedHelpMessage = "Commands:<br />"+searx.getFormattedHelp();
const content = {
"format": "org.matrix.custom.html",
"body": helpMessage,
"formatted_body": formattedHelpMessage,
"msgtype": "m.text"
};
matrixClient.sendEvent(room.roomId, "m.room.message", content, "", (err, res) => {
if (err != null) {
console.error(err);
}
});
return;
}
searx.handleMessage(event, room);
}
});
searx.startUploadMatrixClient();
matrixClient.startClient();