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

botfather

v3.8.0

Published

Lightweight node.js module for easier use telegram bot platform.

Downloads

4,732

Readme

npm npm npm npm npm

Getting Started with BotFather

Bots using BotFather

| Name | Description | | --- | --- | | RadioArchiveBot | Listen your favorite radio stations right in the Telegram! | | EPGBot | Electronic program guides (TV guides)

Want to add your bot? Please submit a pull request on GitHub to update this page!

Installing BotFather

$ cd MyBot
$ npm install botfather --save

Using BotFather

// We recommend storing the token as environment variable.
const TOKEN = process.env.TOKEN

const BotFather = require('botfather')
const bf = new BotFather(TOKEN)

Example #1 (Getting basic information about the bot)

bf.api('getMe')
  .then(json => {
    if(json.ok) {
      return json.result
    }
    console.error(json.description)
  })
  .then(bot => {
    console.info(`Your bot is @${bot.username}, right? :)`)
  })
  .catch(exception => {
    console.error(exception.stack)
  })

Example #2 (Sending file)

const fs = require("fs");
// ...
bf.api("sendDocument", {
  chat_id: CHAT_ID,
  document: fs.createReadStream(PATH_TO_FILE)
})
  .then(json => {
    if(json.ok) {
      return json.result
    }
    console.error(json.description)
  })
  .then(result => {
    console.info(result)
  })
  .catch(exception => {
    console.error(exception.stack)
  })

Example #3 (Extending your own class)

class MyBot extends BotFather {

  /**
   * @param {string} token
   * @see https://core.telegram.org/bots#6-botfather
   */
  constructor(token) {
    super(token)
    this.api('getMe')
      .then(json => {
        if(json.ok) {
          return json.result
        }
        console.error(json.description)
      })
      .then(bot => {
        console.info(`Your bot is @${bot.username}, right? :)`)
      })
      .catch(exception => {
        console.error(exception.stack)
      })
  }
}

new MyBot(TOKEN)

Example #4 (Getting updates recursively)

class MyBot extends BotFather {

  /**
   * @constructor
   * @param {string} token
   * @see https://core.telegram.org/bots#6-botfather
   */
  constructor(token) {
    super(token)
    this.getUpdates()
  }

  /**
   * @param {Object} parameters
   * @see https://core.telegram.org/bots/api#getupdates
   */
  getUpdates(parameters = {limit: 100, timeout: 60 * 2}) {
    this.api('getUpdates', parameters)
      .then(json => {
        if(json.ok) {
          return json.result
        }
        console.error(json.description)
        setTimeout(() => this.getUpdates(parameters), 5000)
      })
      .then(updates => {
        for(let update of updates) {
          this.onReceiveUpdate(update)
        }
        // offset = update_id of last processed update + 1
        if(updates.length > 0) {
          const identifiers = updates.map((update) => update.update_id)
          parameters.offset = Math.max.apply(Math, identifiers) + 1
        }
        this.getUpdates(parameters)
      })
      .catch(exception => {
        console.error(exception.stack)
        setTimeout(() => this.getUpdates(parameters), 5000)
      })
  }

  /**
   * @param {Object} update
   * @see https://core.telegram.org/bots/api#update
   */
   onReceiveUpdate(update) {
     console.log(update)
   }
}

new MyBot(TOKEN)