-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.js
executable file
·48 lines (34 loc) · 1.32 KB
/
bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { GetSecretValueCommand, SecretsManagerClient } from '@aws-sdk/client-secrets-manager';
import JaneAustenQuotes from
'JaneAustenQuotes';
import { Mastodon } from 'megalodon';
import midsomerplots from 'midsomerplots-content';
const secretsclient = new SecretsManagerClient();
const post = async (seed, MastodonClient) => {
let text = process.env.BOT_NAME == 'midsomerplots' ? midsomerplots.generate(seed) : JaneAustenQuotes.generate(seed);
try {
if (process.env.BOT_NAME === 'midsomerplots') {
await MastodonClient.postStatus(text, {spoiler_text: '#murderplot', visibility: 'unlisted'});
} else {
await MastodonClient.postStatus(text, {visibility: 'unlisted'});
}
} catch (e) {
console.error(e);
}
};
const unixTimeInSec = function() {
return Math.round((new Date()).getTime()/1000);
};
export const handler = async () => {
try {
const secretscommand = new GetSecretValueCommand({'SecretId':process.env.BOT_NAME});
const data = await secretsclient.send(secretscommand);
const config = JSON.parse(data.SecretString);
const MastodonClient = new Mastodon('https://mastodon.cloud', config.MASTODON_ACCESS_TOKEN);
await post(unixTimeInSec(),MastodonClient);
return 'bot posted successfully';
} catch(e) {
console.log(e.message);
return e.message;
}
};