Почему метод onText () выполняется только один раз? - PullRequest
0 голосов
/ 09 июля 2020

Метод onText () выполняется только один раз, и я не знаю причины (((Этот метод должен выполняться, когда бот телеграммы получает новое сообщение. Он работает только с первым сообщением. Как это исправить? Это мой код. Спасибо!

const Nightmare = require('nightmare')
const nightmare = Nightmare({show: true})

const TelegramBot = require('node-telegram-bot-api')
const TOKEN = ''

const bot = new TelegramBot(TOKEN, {
    polling: {
        interval:300,
        autoStart: true,
        params:{
            timeout: 10
        }
    }
})

console.log('Bot has been started...')

let price 
let rx = /\/getprice/

 async function getPrice(){
    return nightmare
        .goto('https://uk.investing.com/commodities/real-time-futures')
        .evaluate(()=>{
            let gold = document.querySelector('.pid-68-last').innerText
            let goldAug20 = document.querySelector('.pid-8830-last').innerText
            let diff = Math.abs( gold.replace(',','') - goldAug20.replace(',',''))
            return diff.toFixed(2)
        })
        .end()
        .then(data =>{
            price = data
            //console.log(price)
            return nightmare
        })
    
  }

  bot.onText(rx, async (msg) => {
    await getPrice()
    console.log(msg)
    bot.sendMessage(msg.chat.id, price)
  })

...