npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@xleddyl/telegen-ts

v2.1.0

Published

The autogenerated TypeScript library for the official Telegram Bot API

Downloads

11

Readme

Telegen-TS

A simple and effective library that solely provides an implementation of the official Telegram Bot API.

📚 Docs

This module doesn't need much documentation as all the infos can be retrieved from the official telegram's docs: https://core.telegram.org/bots/api

However all the methods and types are well documented within the editor's linter 😉

💡 How does Telegen-TS works?

EASY! Telegen works by scraping all the important info relative to methods and types from the official API page (cited above). Then it autogenerates (telegram + autogen = telegen eheheh) two files:

  • autogen-Methods.ts contains the abstract class Methods, containing all the methods
    • TelegenTS extends Methods
    • all the methods simply call Methods.makeRequest(...) with the right parameters
    • makeRequest is an abstract method that is then implemented inTelegenTS
  • autogen-types.ts containing TypeScript's interfaces
    • used in Methods for defining the types for parameters

Finally we have the most importante piece of the puzzle: translator.js
This is the file that manages the scraping and the translation of the API (surely it can be improved but literally who cares, probably no one other than me will ever read it lol)

⚙️ Installation

npm install @xleddyl/telegen-ts

💻 Usage

First import @xleddyl/telegen-ts into your project

const { TelegenTS, Types } = require('@xleddyl/telegen-ts') // JavaScript
import { TelegenTS, Types } from '@xleddyl/telegen-ts' // TypeScript

Then create a new TelegenTS instance by passing your bot's token to the constructor

const bot = new TelegenTS(TOKEN)

And you are pretty much ready to go now. Here I just leave a small recap on what you will import from this library:

  • TelegenTS is a class from which all the Available methods can be called (alongside with the ones explained here)
  • Types contains all the Available types used as return values/function parameters (alongside with the ones explained here)
    • suitable for type casting in TypeScript
    • note: for return types manual inference is required 🤧 (read the docs to determine the return type of methods)

🧪 Examples

Get the latest message sent to the bot and respond with the same text

JavaScript example

const { TelegenTS, Types } = require('@xleddyl/telegen-ts')
const bot = new TelegenTS('1234567890:ASDFGHJKL')

const update = (await bot.getUpdates()).pop() // .pop() to get only the last message
const msg = update.message
if(msg !== undefined) {
  await bot.sendMessage(msg.text, msg.chat.id, { reply_to_message_id: msg.message_id })
}

Very simple and dumb polling mechanism

TypeScript example

import { TelegenTS, Types } from '@xleddyl/telegen-ts'
const bot = new TelegenTS('1234567890:ASDFGHJKL')

async function poll(callbackfn: (updates: Types.Update[]) => Promise<void>) {
   let lastUpdateId = 0
   while (1) {
      try {
         const updates: Types.Update[] = await bot.getUpdates({ offset: lastUpdateId })
         if (updates === undefined) break
         if (updates.length === 0) continue
         lastUpdateId = updates[updates.length - 1].update_id + 1
         await callbackfn(updates)
      } catch (e) {
         console.log(e)
         return
      }
   }
}

async function handler(update: Update[]) {
   try {
      for (const update of updates) {
         const msg = update.message
         if(msg === undefined) continue
         await bot.sendMessage('ciao', msg.chat.id, {reply_to_message_id: msg.message_id})
      }
   } catch (e) {
      console.log(e)
   }
}

poll(handler)

Sending a media file

If you want you can send files whenever the API specifies that the method accepts an InputFile type as parameter. To do so please use fs.createReadStream(path) (I've not tested other methods for uploading files so you'll need to experiment)

const fs = require('fs')

const img = fs.createReadStream('./testimg.png')
await bot.sendPhoto(img, msg.chat.id)

And that's pretty much it!! 🥳
Happy botting! 🤖