T
T
Tw1nk1e2018-04-20 11:12:59
JavaScript
Tw1nk1e, 2018-04-20 11:12:59

How to implement a restart of the function after the output of the variable?

There is such a telegram bot that parses the site, takes text information and sends it with the button pressed. It works, but displays the same info (Joke variable). Please tell me how to implement a function restart or similar functionality.

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
};

var needle = require('needle');
var cheerio = require('cheerio');
var RandPage = getRandomInt(10, 100);
var URL = 'http://www.site' + RandPage + '.' + 'shtml';

needle.get(URL, function(error, response, body) {

  const TelegramBot = require('node-telegram-bot-api');
  const token = '';
  const bot = new TelegramBot(token, {polling: true});

  var rand = getRandomInt(0, 21);
  var $ = cheerio.load(body);


function getJoke() {
  var currentJoke = [];
     currentJoke.splice(0, 1, ($('div').find('p').eq(rand).text()));
      return currentJoke;
};

var Joke = getJoke();



    var robot = "Joke";
    if (msg.text.indexOf(robot) === 0) {
      bot.sendMessage(msg.chat.id, Joke[0]);
    }
  });

  bot.onText(/\/start/, (msg) => {
    bot.sendMessage(msg.chat.id, "Welcome", {
      "reply_markup": {
        "keyboard": 
      }
    });
  });
});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
RidgeA, 2018-04-20
@RidgeA

вызывать функцию каждый раз

I
iljaGolubev, 2018-04-20
@iljaGolubev

bot.sendMessage(msg.chat.id, Joke()[0]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question