• This repository has been archived on 21/Oct/2023
  • Stars
    star
    181
  • Rank 204,593 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🤖 Zero-configuration Telegram bot runner

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

More Repositories

1

telegraf

Modern Telegram Bot Framework for Node.js
TypeScript
7,511
star
2

telegraf-i18n

Internationalization middleware for Telegraf
JavaScript
83
star
3

telegra.ph

📝 Tiny API helper for Telegra.ph
JavaScript
67
star
4

telegraf-session-redis

Redis session middleware for Telegraf
JavaScript
50
star
5

telegraf-command-parts

Command parameters parser middleware for Telegraf
JavaScript
40
star
6

telegraf-flow

[Deprecated] Control flow middleware for Telegraf
JavaScript
38
star
7

telegraf-ratelimit

Rate-limiting middleware for Telegraf
JavaScript
34
star
8

telegraf-wit

wit.ai middleware for Telegraf
JavaScript
34
star
9

telegraph.md

Markdown powered pages for Telegra.ph
JavaScript
30
star
10

entity

Convert Telegram entities to HTML or Markdown.
TypeScript
29
star
11

microgames

🎮 Telegram game platform example
HTML
26
star
12

telegraf-mixpanel

Mixpanel middleware for Telegraf
JavaScript
23
star
13

kwiz

Highly flexible JavaScript quiz engine
JavaScript
21
star
14

session

Official storage adapters for Telegraf v4.12+ sessions
TypeScript
20
star
15

create-bot

CLI for Telegram Bot scaffolding.
JavaScript
14
star
16

telegraf-session-firebase

Firebase session for Telegraf
JavaScript
14
star
17

telegraf-widget

Widgets for Telegraf
JavaScript
13
star
18

colorizer-bot

Telegram bot for colorizing black & white photos
JavaScript
12
star
19

telegraf-recast

recast.ai middleware for Telegraf
JavaScript
10
star
20

telegraf-statsd

statsd middleware for Telegraf
JavaScript
9
star
21

types

Types for the Telegram Bot API in TypeScript
TypeScript
8
star
22

telegram-test-server

[WIP] Telegram BotAPI server emulator
JavaScript
8
star
23

tg-cdn

Telegram file proxy
JavaScript
7
star
24

template-bot

Scaffolding out a Telegram bot
JavaScript
6
star
25

telegraf-experiments

Multivariate and A/B testing middleware for Telegraf
JavaScript
6
star
26

client

Low-level library for performing Telegram Bot API calls.
TypeScript
3
star