initial commit

This commit is contained in:
2023-02-09 20:18:05 -05:00
commit f2be2b8982
8 changed files with 1391 additions and 0 deletions

33
lib/api.js Normal file
View File

@ -0,0 +1,33 @@
// API reference: https://api.pleroma.social/ and https://docs-develop.pleroma.social/backend/development/API/pleroma_api/
'use strict';
import fetch from 'node-fetch';
export const getLatestStatuses = async (count) =>{
const headers = {Authorization: `Bearer ${process.env.ADMIN_BEARER}`};
const statusUrlArr = [];
if (!count){
count = 1;
}
try{
const res = await fetch(`https://${process.env.INSTANCE_NAME}/api/v1/pleroma/admin/statuses?page_size=${count}&godmode=false&local_only=false`, {
method: "GET",
headers: headers,
});
const data = await res.json();
for (let account in data){
statusUrlArr.push([
data[account].id,
data[account].account.username,
data[account].pleroma.conversation_id,
data[account].mentions,
]);
};
} catch (error){
throw new Error(error);
};
//console.log(statusUrlArr);
return statusUrlArr;
};

49
lib/replybot.js Normal file
View File

@ -0,0 +1,49 @@
'use strict';
import fetch from 'node-fetch';
import { getLatestStatuses } from './api.js';
const chooseStatusID = (array) => {
return array[Math.floor(Math.random() * array.length)];
};
const emoteChoice = async () =>{ // pulls current emojis from the instance
const res = await fetch(`https://${process.env.INSTANCE_NAME}/api/v1/pleroma/emoji`, {
method: "GET",
});
const data = await res.json();
let emoteArray = [];
for (let e in data){
emoteArray.push(e);
}
return emoteArray[Math.floor(Math.random() * emoteArray.length)];
};
const replyChoice = async () =>{
const replyArr = [ // add desired replies here - it's just a big ass array
"example1",
"example 2",
"EXAMPLE 3",
];
return replyArr[Math.floor(Math.random() * replyArr.length)];
};
export const postReply = async () =>{
const headers = {Authorization: `Bearer ${process.env.REPLYBOT_BEARER}`, "Content-Type": "application/json"};
const mentions = [];
const statusIDArr = await getLatestStatuses(20);
const target = chooseStatusID(statusIDArr);
if (target[3]) {
for (let i = 0; i < target[3].length; i++){
mentions.push(target[3][i].acct);
}
};
const emoji = await emoteChoice();
const word = await replyChoice();
const res = await fetch(`https://${process.env.INSTANCE_NAME}/api/v1/statuses`, {
method: "POST",
headers: headers,
body: JSON.stringify({status: `${word} :${emoji}:`, in_reply_to_id: target[0], to: mentions}),
});
if (res.status === 200) {console.log('Replybot posted!')} else {console.log('Replybot failed:', res.status, res.statusText, res.url, word, emoji)}
};