mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
#1345 Add API endpoint for web search interaction
This commit is contained in:
@ -21,6 +21,7 @@ const SECRET_KEYS = {
|
||||
ONERING_URL: 'oneringtranslator_url',
|
||||
DEEPLX_URL: 'deeplx_url',
|
||||
PALM: 'api_key_palm',
|
||||
SERPAPI: 'api_key_serpapi',
|
||||
}
|
||||
|
||||
/**
|
||||
|
39
src/serpapi.js
Normal file
39
src/serpapi.js
Normal file
@ -0,0 +1,39 @@
|
||||
const fetch = require('node-fetch').default;
|
||||
const { readSecret } = require('./secrets');
|
||||
|
||||
/**
|
||||
* Registers the SerpApi endpoints.
|
||||
* @param {import("express").Express} app
|
||||
* @param {any} jsonParser
|
||||
*/
|
||||
function registerEndpoints(app, jsonParser) {
|
||||
app.post('/api/serpapi/search', jsonParser, async (request, response) => {
|
||||
try {
|
||||
const key = readSecret('serpapi_key');
|
||||
|
||||
if (!key) {
|
||||
console.log('No SerpApi key found');
|
||||
return response.sendStatus(401);
|
||||
}
|
||||
|
||||
const { query } = request.body;
|
||||
const result = await fetch(`https://serpapi.com/search.json?q=${encodeURIComponent(query)}&api_key=${key}`);
|
||||
|
||||
if (!result.ok) {
|
||||
const text = await result.text();
|
||||
console.log('SerpApi request failed', result.statusText, text);
|
||||
return response.status(500).send(text);
|
||||
}
|
||||
|
||||
const data = await result.json();
|
||||
return response.json(data);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return response.sendStatus(500);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
registerEndpoints,
|
||||
};
|
Reference in New Issue
Block a user