Prevent the social media bot from trying to use unavailable accounts

This commit is contained in:
Matt Isenhower 2023-07-23 09:06:13 -07:00
parent 541a14b607
commit 787e73338b
6 changed files with 47 additions and 21 deletions

View File

@ -40,6 +40,11 @@ export default class StatusGeneratorManager
let clientsToPost = [];
for (let client of this.clients) {
if (!(await client.canSend())) {
this.console(generator, client).warn('Client cannot send (missing credentials)');
continue;
}
if (force || await generator.shouldPost(client)) {
clientsToPost.push(client);
}

View File

@ -10,24 +10,23 @@ export default class BlueskyClient extends Client
name = 'Bluesky';
#agent;
#loggedIn = false;
constructor() {
super();
this.#agent = new BskyAgent({
service: process.env.BLUESKY_SERVICE,
});
async canSend() {
return process.env.BLUESKY_SERVICE
&& process.env.BLUESKY_IDENTIFIER
&& process.env.BLUESKY_PASSWORD;
}
async login() {
if (!this.#loggedIn) {
if (!this.#agent) {
this.#agent = new BskyAgent({
service: process.env.BLUESKY_SERVICE,
});
await this.#agent.login({
identifier: process.env.BLUESKY_IDENTIFIER,
password: process.env.BLUESKY_PASSWORD,
});
this.#loggedIn = true;
}
}

View File

@ -6,6 +6,10 @@ export default class Client
key;
name;
async canSend() {
return true;
}
/**
* @param {Status} status
* @param {StatusGenerator} generator

View File

@ -16,6 +16,10 @@ export default class MastodonClient extends Client
this.#accessToken = process.env.MASTODON_ACCESS_TOKEN;
}
async canSend() {
return this.#url && this.#accessToken;
}
async send(status, generator) {
// Mastodon API
const masto = await login({

View File

@ -17,6 +17,11 @@ export default class ThreadsClient extends Client {
});
}
async canSend() {
return process.env.THREADS_USERNAME
&& process.env.THREADS_PASSWORD;
}
async send(status, generator) {
let jpeg = await sharp(status.media[0].file).jpeg().toBuffer();

View File

@ -9,25 +9,34 @@ export default class TwitterClient extends Client
/** @var {TwitterApi} */
#api;
constructor() {
super();
async canSend() {
return process.env.TWITTER_CONSUMER_KEY
&& process.env.TWITTER_CONSUMER_SECRET
&& process.env.TWITTER_ACCESS_TOKEN_KEY
&& process.env.TWITTER_ACCESS_TOKEN_SECRET;
}
this.#api = new TwitterApi({
appKey: process.env.TWITTER_CONSUMER_KEY,
appSecret: process.env.TWITTER_CONSUMER_SECRET,
accessToken: process.env.TWITTER_ACCESS_TOKEN_KEY,
accessSecret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
});
api() {
if (!this.#api) {
this.#api = new TwitterApi({
appKey: process.env.TWITTER_CONSUMER_KEY,
appSecret: process.env.TWITTER_CONSUMER_SECRET,
accessToken: process.env.TWITTER_ACCESS_TOKEN_KEY,
accessSecret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
});
}
return this.#api;
}
async send(status, generator) {
// Upload images
let mediaIds = await Promise.all(
status.media.map(async m => {
let id = await this.#api.v1.uploadMedia(m.file, { mimeType: m.type });
let id = await this.api().v1.uploadMedia(m.file, { mimeType: m.type });
if (m.altText) {
await this.#api.v1.createMediaMetadata(id, { alt_text: { text: m.altText } });
await this.api().v1.createMediaMetadata(id, { alt_text: { text: m.altText } });
}
return id;
@ -35,6 +44,6 @@ export default class TwitterClient extends Client
);
// Send status
await this.#api.v2.tweet(status.status, { media: { media_ids: mediaIds } });
await this.api().v2.tweet(status.status, { media: { media_ids: mediaIds } });
}
}