| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- import { createStreamingAPIClient, createRestAPIClient, mastodon } from "masto";
- import config from "../../config";
- import quotes from "./quotes";
- import Emojis from "../../enums/emojis";
- import LogLevels from "../../enums/log-levels";
- export default class Fortune {
- private readonly _mastodonStreamingClient: mastodon.streaming.Client;
- private readonly _mastodonRestClient: mastodon.rest.Client;
- constructor (accessToken = config.MASTODON_TEST_ACCESS_TOKEN) {
- this._mastodonStreamingClient = createStreamingAPIClient({ streamingApiUrl: config.MASTODON_STREAMING_URL, accessToken });
- this._mastodonRestClient = createRestAPIClient({ url: config.MASTODON_API_URL, accessToken });
- }
- private async reply(inReplyToId: string | undefined, user: string, visibility, iteration = 1, count = 1): Promise<void> {
- let status = "";
- let spoilerText;
- let stop = false;
- let result: mastodon.v1.Status;
- if (count == 1) {
- status = `${Emojis.FAIRY}: @${user} prepárate para un viaje mágico ${Emojis.SPARKLES}`;
- console.log("Sending status\n", status);
- result = await this._mastodonRestClient.v1.statuses.create({ inReplyToId, status, visibility });
- inReplyToId = result.id;
- if (config.LOG_LEVEL === LogLevels.DEBUG) {
- console.debug("Result", result);
- }
- }
- console.log("Iteration", iteration++);
- console.log("Count", count++);
- iteration = count >= 4 ? 10 : iteration;
- switch (iteration) {
- case 1:
- status = `${Emojis.GENIE}: "¡Oh! Ya lo veo..."`;
- break;
- case 2:
- status = `${Emojis.MAGIC_SHELL}: ${Emojis.EAR} ${Emojis.WAVE} ${Emojis.WAVE} ${Emojis.WAVE}`;
- break;
- case 3:
- status = `${Emojis.MAGIC_WAND}: "Alohomora!... ${Emojis.PADLOCK}"`;
- break;
- case 4:
- status = `${Emojis.WIZARD}: "La magia no es mi fuerte, pero el caos sí ${Emojis.SPARKLES}"`;
- break;
- case 5:
- status = `${Emojis.CRISTAL_BALL}: "Acércate... más cerca..."`;
- break;
- case 6:
- status = `${Emojis.UNICORN}: "Flores! colores!" ${Emojis.RAINBOW}`;
- break;
- case 7:
- status = `${Emojis.FAIRY}: ${Emojis.SPARKLES}`;
- break;
- default:
- status = `${Emojis.MAGNIFYING_GLASS}${Emojis.SCROLL}: "${quotes[Math.floor(Math.random() * quotes.length)]}"`;
- spoilerText = `Acá se encuentra tu fortuna ${Emojis.FORTUNE_COOKIE}`;
- stop = true;
- }
- console.log("Sending status\n", status);
- result = await this._mastodonRestClient.v1.statuses.create({ inReplyToId, spoilerText, status, visibility });
- if (config.LOG_LEVEL === LogLevels.DEBUG) {
- console.log("Result", result);
- }
- if (!stop && count < 4) {
- setTimeout(async () => await this.reply(result.id, user, visibility, iteration, count), 3000);
- }
- }
- public async suscribe(): Promise<void> {
- console.log("Listening to incoming events...");
- for await (const event of this._mastodonStreamingClient.user.notification.subscribe()) {
- switch(event.event) {
- case "notification":
- if (event.payload.type == "mention") {
- const {id, createdAt, visibility, url, content} = event.payload.status ?? {};
- if (config.DEVELOP) {
- console.log("Event received\n", event.payload);
- } else {
- console.log("Event received\n", { id, createdAt, visibility, url, content });
- }
- await this.reply(event.payload.status?.id, event.payload.account.acct, visibility, Math.floor(Math.random() * 10));
- }
- break;
- }
- };
- }
- }
- try {
- new Fortune(config.DEVELOP ? config.MASTODON_TEST_ACCESS_TOKEN: config.MASTODON_KEY_FORTUNE).suscribe();
- } catch (error) {
- console.error(error);
- }
|