Node-telegram-bot-api: Problems with a welcome message and the rest of the commands

Created on 16 Jun 2018  路  18Comments  路  Source: yagop/node-telegram-bot-api

Hi, I've been experiencing an error for a while. Until recently everything worked correctly, but out of nowhere it shows me the following error:

Unable to read property 'username' from undefined
It happens with all the properties:
msg.new_chat_member.first_name
msg.new_chat_member.id

I understand that if it were not defined, it would show this error, but if it is defined. I show you the code

bot.on('message',function(msg, match){
    console.log(msg);

    var chatId = msg.chat.id;
    var newUserName = msg.new_chat_member.username;
    var newUserId = msg.new_chat_member.id;
    var firstName = msg.new_chat_member.first_name;
    var messageId = msg.message_id;

    if(msg.new_chat_members != undefined){

        bot.deleteMessage(msg.chat.id, messageId);
        bot.sendMessage(msg.chat.id, "隆Hola " + firstName + ", bienvenid@ al grupo " + msg.chat.title)
            } 
        });

Show the error when I try to use another command, but it does not respond
Given this error, it does not let me execute any other function / command

Thanks in advance

question sorry! my bad!

All 18 comments

You must assign that variables ONLY when msg.new_chat_members != undefined, so:

//...
var chatId = msg.chat.id;
var messageId = msg.message_id;

if (msg.new_chat_members != undefined){
    var newUserName = msg.new_chat_member.username;
    var newUserId = msg.new_chat_member.id;
    var firstName = msg.new_chat_member.first_name;

    //...
}

For the second question, maybe when you solve that error it should be working fine.

You shall definitly use the event of a new comer rather that polling all new messages and check for new comers

bot.on("new_chat_members", function(msg) {
bot.sendMessage(msg.chat.id,"Hi new comers ! )
})

Hello. I tried the above syntax, and it does not work for me. msg is just undefined. Has this method changed in some way?

@NatalieChin80 Post your code, so that we can see it.

I currently have the following. As above, I was trying to make use of the new_chat_members event. The new chat message works only when the bot.on('message') part is commented out. Can we not have multiple bot.on? Or am I missing something?

bot.on("new_chat_members", function (msg) {
    console.log(msg.new_chat_members);
    console.log(msg.new_chat_members[0].first_name);
    bot.sendMessage(msg.chat.id, "Hello " + msg.new_chat_members[0].first_name);
})
bot.on('message', (msg) => {
    //other code
});

Event new_chat_members not longer exists.
You should use:

bot.on('message', (msg) => {
    if (msg.new_chat_members != undefined)
        console.log(message.new_chat_member);
});

My fault.
Change bot.on line with bot.on('message', function (msg) {.

Ahhhh. I got it. Thanks @sidelux!

Hey guys, I'm trying to delete the previous message of my bot to reduce flood in the channel but I get: 400 Bad Request: message can't be deleted. The code:

bot.on("new_chat_members", function(msg) {
bot.deleteMessage(msg.chat.id, msg.message_id)

setTimeout(function(){
var chatId = msg.chat.id;
var messageId = msg.message_id;
}, 500);

if (msg.new_chat_members != undefined){
  if (msg.new_chat_member.id != undefined){
  var newUserName = msg.new_chat_member.username;
  } else {
  var newUserName = msg.new_chat_member.first_name;
  }
}
console.log(msg);

bot.sendMessage(msg.chat.id, 'Hi @' + newUserName + ', thanks for joining our community!

@Magush26 You should do something like this:

bot.sendMessage(msg.chat.id, 'Hi @' + newUserName + ', thanks for joining our community!").then(function (data) {
    setTimeout(function(){
        bot.deleteMessage(data.chat.id, data.message_id);
    }, 500);
});

@sidelux thank you for the answer, can't seem to get my head around it. What I basically want is that if 10 people join my chat, the bot will only show one message referencing the last person. So it needs to keep deleted its previous messages. The way I have it now, it just deletes the previous message in the chat, not from the bot. Sorry, I'm new to this, appreciate all the help

@Magush26 It's not so easy to understand your way, anyway, open new issue with some example and your code (well formatted) so other people can help you and not mess up this issue, thanks

hi guys. i have a problem with new_chat_members. i want kick the new member after 5 minutes from to join .

bot.on('message', function (msg) {
  if (msg.new_chat_members != undefined)
      const chatid = msg.chat.id
      const userid = msg.new_chat_members
      setTimeout(function(){
          bot.kickChatMember(chatid, userid)
      }, 300)
})

bot is dead with this code. if i delete this code, bot answer at the commands

@NexKo try to debug with console.log to know where it stops working.

@sidelux
const chatid = msg.chat.id
^^^^^

SyntaxError: Unexpected token const
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:282:19)

Try to use let or var to declare

Curly braces are missing on the IF.
With that snippet, chatid is created only within that IF.

So, probably, it dies because kickChatMember it cannot find chatid.
It was valid in the previous answers because @sidelux was using it for just a single line.

how to close Chat section in node js by node-telegram-bot-api

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Dohoon-Kim picture Dohoon-Kim  路  3Comments

antonrifco picture antonrifco  路  3Comments

sidelux picture sidelux  路  3Comments

kenanchristian picture kenanchristian  路  4Comments

lenny76 picture lenny76  路  3Comments