diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..9d33a3f --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,49 @@ +name: Publish package + +on: workflow_dispatch + +permissions: + contents: write + +jobs: + publish_package: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Install Bun + uses: oven-sh/setup-bun@v1 + + - id: changelog + name: Generate changelog + run: bun scripts/generate-changelog.ts + + - name: Install modules + run: bun install + + - name: Install Node + uses: actions/setup-node@v4 + with: + node-version: "20.x" + registry-url: "https://registry.npmjs.org" + scope: "@gramio" + + - name: Publish package + run: npm publish --access public + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + + - name: GitHub Release + uses: ncipollo/release-action@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} + tag: v${{ steps.changelog.outputs.version }} + name: v${{ steps.changelog.outputs.version }} + body: ${{ steps.changelog.outputs.changelog }} + draft: false + prerelease: false + generateReleaseNotes: true diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b1e3c8d --- /dev/null +++ b/.gitignore @@ -0,0 +1,177 @@ +# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore + +# Logs + +logs +_.log +npm-debug.log_ +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Caches + +.cache + +# Diagnostic reports (https://nodejs.org/api/report.html) + +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# Runtime data + +pids +_.pid +_.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover + +lib-cov + +# Coverage directory used by tools like istanbul + +coverage +*.lcov + +# nyc test coverage + +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) + +.grunt + +# Bower dependency directory (https://bower.io/) + +bower_components + +# node-waf configuration + +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) + +build/Release + +# Dependency directories + +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) + +web_modules/ + +# TypeScript cache + +*.tsbuildinfo + +# Optional npm cache directory + +.npm + +# Optional eslint cache + +.eslintcache + +# Optional stylelint cache + +.stylelintcache + +# Microbundle cache + +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history + +.node_repl_history + +# Output of 'npm pack' + +*.tgz + +# Yarn Integrity file + +.yarn-integrity + +# dotenv environment variable files + +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) + +.parcel-cache + +# Next.js build output + +.next +out + +# Nuxt.js build / generate output + +.nuxt +dist + +# Gatsby files + +# Comment in the public line in if your project uses Gatsby and not Next.js + +# https://nextjs.org/blog/next-9-1#public-directory-support + +# public + +# vuepress build output + +.vuepress/dist + +# vuepress v2.x temp and cache directory + +.temp + +# Docusaurus cache and generated files + +.docusaurus + +# Serverless directories + +.serverless/ + +# FuseBox cache + +.fusebox/ + +# DynamoDB Local files + +.dynamodb/ + +# TernJS port file + +.tern-port + +# Stores VSCode versions used for testing VSCode extensions + +.vscode-test + +# yarn v2 + +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* + +# IntelliJ based IDEs +.idea + +# Finder (MacOS) folder config +.DS_Store + +test.ts \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..0283eb7 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# @gramio/auto-retry + +A plugin that catches errors with the retry_after field (rate limit errors), waits for the specified time and repeats the API request. + +```ts +import { Bot } from "gramio"; +import { autoRetry } from "@gramio/auto-retry"; + +const bot = new Bot(process.env.TOKEN!) + .extend(autoRetry()) + .command("start", async (context) => { + for (let index = 0; index < 100; index++) { + await context.reply(`some ${index}`); + } + }) + .onStart(console.log); + +bot.start(); +``` diff --git a/bun.lockb b/bun.lockb new file mode 100644 index 0000000..b50aed8 Binary files /dev/null and b/bun.lockb differ diff --git a/package.json b/package.json new file mode 100644 index 0000000..e9b4ebc --- /dev/null +++ b/package.json @@ -0,0 +1,32 @@ +{ + "name": "@gramio/auto-retry", + "version": "0.0.1", + "main": "dist/index.js", + "devDependencies": { + "@types/bun": "^1.0.12", + "typescript": "^5.4.3", + "gramio": "^0.0.25" + }, + "peerDependencies": { + "gramio": "^0.0.25" + }, + "description": "Auto-retry plugin for GramIO", + "files": [ + "dist" + ], + "homepage": "https://github.com/gramiojs/auto-retry", + "keywords": [ + "gramio", + "gramio-plugin", + "auto-retry", + "api", + "telegram", + "telegram-bot", + "telegram-bot-api" + ], + "readme": "https://gramio.netlify.app/", + "scripts": { + "prepublishOnly": "tsc && bunx tsc-alias" + }, + "type": "commonjs" +} diff --git a/scripts/generate-changelog.ts b/scripts/generate-changelog.ts new file mode 100644 index 0000000..c31cfc4 --- /dev/null +++ b/scripts/generate-changelog.ts @@ -0,0 +1,36 @@ +import { execSync } from "node:child_process"; +import { randomUUID } from "node:crypto"; +import { appendFileSync } from "node:fs"; +import { EOL } from "node:os"; + +function getLatestTag() { + try { + return execSync("git describe --abbrev=0 --tags").toString().trim(); + } catch (e) { + console.warn(e); + return execSync("git rev-list --max-parents=0 HEAD").toString().trim(); + } +} + +const commits = execSync( + `git log ${getLatestTag()}..HEAD --pretty="format:%s%b"`, +) + .toString() + .trim() + .split("\n") + .reverse(); + +console.log(getLatestTag(), commits); + +const version = execSync("npm pkg get version").toString().replace(/"/gi, ""); + +const delimiter = `---${randomUUID()}---${EOL}`; + +if (process.env.GITHUB_OUTPUT) + appendFileSync( + process.env.GITHUB_OUTPUT, + `changelog<<${delimiter}${commits.join( + EOL.repeat(2), + )}${EOL}${delimiter}version=${version}${EOL}`, + ); +else console.log("Not github actions"); diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..c07e057 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,16 @@ +import { Plugin } from "gramio"; + +export function autoRetry() { + return new Plugin("@gramio/auto-retry").onResponseError( + async (error, api) => { + if (error.payload?.retry_after) { + console.log(error.method, error.payload); + setTimeout( + //@ts-expect-error + () => api[error.method](error.params), + error.payload.retry_after * 1000, + ); + } + }, + ); +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..1189968 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "lib": ["ESNext"], + "target": "ES2022", + "module": "NodeNext", + "moduleDetection": "force", + + /* Bundler mode */ + "moduleResolution": "NodeNext", + /* Linting */ + "skipLibCheck": true, + "strict": true, + "noFallthroughCasesInSwitch": true, + "forceConsistentCasingInFileNames": true, + "declaration": true, + "baseUrl": "./src", + "rootDir": "./src", + "outDir": "./dist", + }, + "include": ["src"] +}