N
N
NeRudic2021-07-26 23:29:49
Node.js
NeRudic, 2021-07-26 23:29:49

I am using node-telegram-bot-api library. Shows error ReferenceError: msg is not defined. What could be the problem?

At the initial stage, it immediately gives an error ReferenceError: msg is not defined. I looked at the office. documentation, the same const chatId = msg.chat.id is also used there, but for some reason it gives me an error

const TelegramApi = require('node-telegram-bot-api')

//Создаём переменную с токеном бота
const token = 'token'

const bot = new TelegramApi(token, {polling:true})

//Вытаскиваем поле текст для взаимодействия с ним
const text = msg.text;
//Вытаскиваем Id чата
const chatId = msg.chat.id;
bot.on('message', msg => {
  bot.sendMessage(chatId, "Повторяю за тобой ${text}")
})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rag'n' Code Man, 2021-07-27
@iDmitriyWinX

Yours msgis only available in this part of the code:

bot.on('message', msg => {
  bot.sendMessage(chatId, "Повторяю за тобой ${text}")
})

Because it is a local callback variable. Or rather, a parameter, not a variable.
If it is not clear, then it msgis available inside these curly brackets
msg => {}
. Outside of them, this variable does not exist.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question