Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including node-telegram-random-message-replier with all npm packages installed. Try it out:

require("node-telegram-bot-api/package.json"); // node-telegram-bot-api is a peer dependency. var nodeTelegramRandomMessageReplier = require("node-telegram-random-message-replier")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

node-telegram-random-message-replier v2.1.0

Node module which allows the bot to reply to a random message in the chat

Node-telegram-random-message-replier

Build Status

Node module which allows the bot to reply to a random message in the chat.

You just set the probability between 1 and 100 and then the magic happens.

Install:

npm install node-telegram-random-message-replier

Use:

node-telegram-random-message-replier initially works with node-telegram-bot-api.

const TelegramBot = require('node-telegram-bot-api');
const TelegramRandomMessageReplier = require('node-telegram-random-message-replier');

// initialize
const bot = new TelegramBot(BOT_TOKEN);
const replier = new TelegramRandomMessageReplier({
  bot,
  dbPath: path.resolve(process.cwd(), 'db'),
  defaultChance: 0,
  showChanceMessage: 'Current chance is CURRENT_CHANCE%',
  setChanceMessage: 'Current chance changed from CURRENT_CHANCE% to NEXT_CHANCE%'
  // CURRENT_CHANCE and NEXT_CHANCE strings will be replaced with currentChance and nextChance values
});

// on command "/chance" will show current chance value
bot.onText(/^\/chance($|@)/, msg => {
  replier.showChance(msg);
});

// on command "/setchance 30" will set chance value to 30% to reply to the message
// command "/setchance" without value is equal to "/chance". So you can use only one command in your bot
bot.onText(/^\/chance(@.* )?(.+)?/, (msg, match) => {
  const chanceValue = match[2];

  replier.setChance(msg, chanceValue);
});

// handle each message to be ready for replying
bot.on('message', msg => {
  // process() method calculates chance for message of being replied and calls one of the callbacks
  // the chance is set previously via "/setchance <value>" command
  replier.process(
    msg,
    repliedMsg => {
      // success callback. Do something with lucky message
      bot.sendMessage(repliedMsg.chat.id, repliedMsg);
    },
    repliedMsg => {
      // error callback [optional]. Do something with unlucky message if you want
    }
  );
});

Options:

new TelegramRandomMessageReplier({
  bot: <your bot instance> // previously created bot via node-telegram-bot-api
  defaultChance: <number> // default chance to reply to a message | default: 0
  showChanceMessage: <string> // set message for show chance command
  setChanceMessage: <string> // set message for change chance command
});
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free