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

micro-bot

v2.5.3

Published

Zero-configuration Telegram bot runner

Downloads

140

Readme

NPM Version node Build Status js-standard-style

μ-bot

🤖 Zero-configuration Telegram bot runner

Documentation

micro-bot was built on top of Telegraf library.

Telegraf API documentation.

Installation

Install from NPM:

$ npm install micro-bot

Scaffolding

If you have installed latest yarn or npm you can use create-bot scaffolding tool:

$ npm init bot smart-bot
$ cd smart-bot

Or using yarn:

$ yarn create bot smart-bot
$ cd smart-bot

Quick start

The following example will answer with important information about everything.

$ mkdir smart-bot
$ cd smart-bot
$ npm init
$ npm install micro-bot --save

Then write your index.js.

module.exports = ({ reply }) => reply('42')

Then in your package.json:

"main": "index.js",
"scripts": {
  "start": "micro-bot"
}

To run the bot, use the micro-bot command:

$ BOT_TOKEN='TOKEN' npm start

or

$ micro-bot -t TOKEN index.js

To run the bot with webhook support, provide webhook domain name:

$ micro-bot -t TOKEN -d yourdomain.tld echo.js

Supported environment variables:

  • process.env.BOT_TOKEN - Bot token
  • process.env.BOT_DOMAIN - Webhook domain

Deployment to now

Let's deploy your micro-bot with Realtime global deployments by Zeit.

First, install now

$ npm install now -g
$ now login

Finally use now to deploy:

$ now -e BOT_TOKEN='YOUR BOT TOKEN'

Congratulations, your bot is alive! 🎉

Deployment to Heroku

Okay, now we will deploy our micro-bot to Heroku. Why not?!

First, install heroku binaries and login via console.

Then, init new git repo:

$ git init
$ heroku create

Afterwards, update Heroku config:

$ heroku config:set --app YourAppId BOT_TOKEN='YOUR BOT TOKEN'
$ heroku config:set --app YourAppId BOT_DOMAIN='https://YourAppId.herokuapp.com'

Then add Procfile into the root of your project, with one line:

web: micro-bot -p $PORT

Finally use git to deploy:

$ git add index.js package.json
$ git commit -m 'initial commit'
$ git push heroku master

Example μ-bots

Advanced Examples

const { mount, reply } = require('micro-bot')
module.exports = mount('sticker', reply('👍'))
const { readFileSync } = require('fs')
const { Composer } = require('micro-bot')
const bot = new Composer()

bot.start((ctx) => ctx.reply('Welcome'))
bot.help((ctx) => ctx.reply('Help message'))
bot.hears('hi', ({ reply }) => reply('Hello'))
bot.on('sticker', ({ reply }) => reply('👍'))

// Export bot handler
module.exports = bot

// Or you can export hash with handlers and options
module.exports = {
  bot: bot,
  init: (bot) => {
    console.log('Bot initialization hook')
  },
  server: (req, res, next) => {
    console.log('Http request hook')
  },
  options: {
    telegram: {
      agent: new HttpsProxyAgent('proxy url')
    }
  },
  tlsOptions: {
    key:  readFileSync('server-key.pem'),
    cert: readFileSync('server-cert.pem'),
    ca: [
      // This is necessary only if the client uses the self-signed certificate.
      readFileSync('client-cert.pem')
    ]
  }
}

Stages & Scenes

const { Composer, Stage, Scene, session } = require('micro-bot')

// Greeter scene
const greeter = new Scene('greeter')
greeter.enter((ctx) => ctx.reply('Hi'))
greeter.leave((ctx) => ctx.reply('Buy'))
greeter.hears(/hi/gi, (ctx) => ctx.scene.leave())
greeter.on('message', (ctx) => ctx.reply('Send `hi`'))

const stage = new Stage()
stage.register(greeter)

const bot = new Composer()
bot.use(session())
bot.use(stage)
bot.command('greeter', (ctx) => ctx.scene.enter('greeter'))
bot.command('cancel', (ctx) => ctx.scene.leave())
module.exports = bot

Credits

micro-bot is highly inspired by Micro