UNPKG

substreams-sink-slack

Version:
147 lines (142 loc) 4.55 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // index.ts var substreams_sink_slack_exports = {}; __export(substreams_sink_slack_exports, { DEFAULT_SLACK_API_TOKEN_ENV: () => DEFAULT_SLACK_API_TOKEN_ENV, action: () => action, logger: () => import_substreams_sink2.logger }); module.exports = __toCommonJS(substreams_sink_slack_exports); var import_substreams = require("substreams"); var import_substreams_sink2 = require("substreams-sink"); var import_substreams_sink_socials = require("substreams-sink-socials"); // src/slack.ts var import_substreams_sink = require("substreams-sink"); var import_web_api = require("@slack/web-api"); var import_zod = require("zod"); var SlackConfigSchema = import_zod.z.object({}); var Slack = class { client; constructor(token) { this.client = new import_web_api.WebClient(token, { retryConfig: import_web_api.retryPolicies.fiveRetriesInFiveMinutes }); } async sendMessage(chatId, message, _) { try { await this.client.chat.postMessage({ text: message, channel: chatId }); } catch (error) { if (error.code === import_web_api.ErrorCode.PlatformError) { import_substreams_sink.logger.error(error); } else { import_substreams_sink.logger.error("failed to send message"); } } } }; // package.json var package_default = { name: "substreams-sink-slack", version: "0.1.0", description: "Substreams slack sink module", main: "dist/index.js", types: "dist/index.d.ts", bin: { "substreams-sink-slack": "dist/bin/cli.js" }, repository: "git@github.com:pinax-network/substreams-sink-slack.git", keywords: [ "substreams", "streamingfast", "firehose", "thegraph", "pinax" ], author: { name: "Charles", email: "charles@pinax.network" }, files: [ "dist" ], contributors: [ { name: "Denis", email: "denis@pinax.network" }, { name: "Charles", email: "charles@pinax.network" } ], license: "MIT OR Apache-2.0", scripts: { prepublishOnly: "tsc", build: "tsup index.ts bin/cli.ts --dts", pkg: "ncc build bin/cli.ts -o build && pkg dist/bin/cli.js --target linux-x64,mac-x64,win-x64,mac-arm64 --output substreams-sink-slack" }, dependencies: { "@slack/web-api": "^6.8.1", commander: "^10.0.0", dotenv: "^16.0.3", substreams: "0.7.x", "substreams-sink": "0.4.x", "substreams-sink-socials": "^0.0.4", winston: "^3.8.2", zod: "^3.21.4" }, devDependencies: { "@tsconfig/recommended": "latest", "@vercel/ncc": "latest", pkg: "latest", tsup: "latest", typescript: "latest" } }; // index.ts import_substreams_sink2.logger.setName(package_default.name); var DEFAULT_SLACK_API_TOKEN_ENV = "SLACK_API_TOKEN"; async function action(manifest, moduleName, options) { const spkg = await (0, import_substreams.download)(manifest); const { config, slackApiTokenEnvvar, slackApiToken } = options; let social = new import_substreams_sink_socials.Social(config, SlackConfigSchema); const slack_api_token = slackApiToken ?? process.env[slackApiTokenEnvvar]; if (!slack_api_token) { import_substreams_sink2.logger.error("[slack_api_token] is required"); process.exit(1); } const slackBot = new Slack(slack_api_token); const substreams = (0, import_substreams_sink2.run)(spkg, moduleName, options); substreams.on("anyMessage", async (messages) => { await social.distributeMessages(messages, (chatId, message, config2) => { slackBot.sendMessage(chatId, message, config2); }); }); substreams.start(options.delayBeforeStart); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { DEFAULT_SLACK_API_TOKEN_ENV, action, logger });