index.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { createStreamingAPIClient, createRestAPIClient, mastodon } from "masto";
  2. import config from "../../config";
  3. import quotes from "./quotes";
  4. import Emojis from "../../enums/emojis";
  5. import LogLevels from "../../enums/log-levels";
  6. export default class Fortune {
  7. private readonly _mastodonStreamingClient: mastodon.streaming.Client;
  8. private readonly _mastodonRestClient: mastodon.rest.Client;
  9. constructor (accessToken = config.MASTODON_TEST_ACCESS_TOKEN) {
  10. this._mastodonStreamingClient = createStreamingAPIClient({ streamingApiUrl: config.MASTODON_STREAMING_URL, accessToken });
  11. this._mastodonRestClient = createRestAPIClient({ url: config.MASTODON_API_URL, accessToken });
  12. }
  13. private async reply(inReplyToId: string | undefined, user: string, visibility, iteration = 1, count = 1): Promise<void> {
  14. let status = "";
  15. let spoilerText;
  16. let stop = false;
  17. let result: mastodon.v1.Status;
  18. if (count == 1) {
  19. status = `${Emojis.FAIRY}: @${user} prepárate para un viaje mágico ${Emojis.SPARKLES}`;
  20. console.log("Sending status\n", status);
  21. result = await this._mastodonRestClient.v1.statuses.create({ inReplyToId, status, visibility });
  22. inReplyToId = result.id;
  23. if (config.LOG_LEVEL === LogLevels.DEBUG) {
  24. console.debug("Result", result);
  25. }
  26. }
  27. console.log("Iteration", iteration++);
  28. console.log("Count", count++);
  29. iteration = count >= 4 ? 10 : iteration;
  30. switch (iteration) {
  31. case 1:
  32. status = `${Emojis.GENIE}: "¡Oh! Ya lo veo..."`;
  33. break;
  34. case 2:
  35. status = `${Emojis.MAGIC_SHELL}: ${Emojis.EAR} ${Emojis.WAVE} ${Emojis.WAVE} ${Emojis.WAVE}`;
  36. break;
  37. case 3:
  38. status = `${Emojis.MAGIC_WAND}: "Alohomora!... ${Emojis.PADLOCK}"`;
  39. break;
  40. case 4:
  41. status = `${Emojis.WIZARD}: "La magia no es mi fuerte, pero el caos sí ${Emojis.SPARKLES}"`;
  42. break;
  43. case 5:
  44. status = `${Emojis.CRISTAL_BALL}: "Acércate... más cerca..."`;
  45. break;
  46. case 6:
  47. status = `${Emojis.UNICORN}: "Flores! colores!" ${Emojis.RAINBOW}`;
  48. break;
  49. case 7:
  50. status = `${Emojis.FAIRY}: ${Emojis.SPARKLES}`;
  51. break;
  52. default:
  53. status = `${Emojis.MAGNIFYING_GLASS}${Emojis.SCROLL}: "${quotes[Math.floor(Math.random() * quotes.length)]}"`;
  54. spoilerText = `Acá se encuentra tu fortuna ${Emojis.FORTUNE_COOKIE}`;
  55. stop = true;
  56. }
  57. console.log("Sending status\n", status);
  58. result = await this._mastodonRestClient.v1.statuses.create({ inReplyToId, spoilerText, status, visibility });
  59. if (config.LOG_LEVEL === LogLevels.DEBUG) {
  60. console.log("Result", result);
  61. }
  62. if (!stop && count < 4) {
  63. setTimeout(async () => await this.reply(result.id, user, visibility, iteration, count), 3000);
  64. }
  65. }
  66. public async suscribe(): Promise<void> {
  67. console.log("Listening to incoming events...");
  68. for await (const event of this._mastodonStreamingClient.user.notification.subscribe()) {
  69. switch(event.event) {
  70. case "notification":
  71. if (event.payload.type == "mention") {
  72. const {id, createdAt, visibility, url, content} = event.payload.status ?? {};
  73. if (config.DEVELOP) {
  74. console.log("Event received\n", event.payload);
  75. } else {
  76. console.log("Event received\n", { id, createdAt, visibility, url, content });
  77. }
  78. await this.reply(event.payload.status?.id, event.payload.account.acct, visibility, Math.floor(Math.random() * 10));
  79. }
  80. break;
  81. }
  82. };
  83. }
  84. }
  85. try {
  86. new Fortune(config.DEVELOP ? config.MASTODON_TEST_ACCESS_TOKEN: config.MASTODON_KEY_FORTUNE).suscribe();
  87. } catch (error) {
  88. console.error(error);
  89. }