1

I'm struggling to understand where I've gone wrong here so I'm looking for some extra eyes. I'm using Telegraf to make a Telegram bot in NodeJS. When a new user logs in, it should restrict them from being able to post anything until they answer a simple Captcha-style question. Assuming they answer correctly, they should be granted to ability to post again. If they answer incorrectly, they are kicked from the group.

Everything seems to be coming together except the part where the user is given back the ability to post after answering correctly. My understanding is restrictChatMember can be passed "true" for permissions you want to allow, but it's not working. What else am I missing?

bot.on('new_chat_members', (ctx) => {
  newMember = ctx.message.new_chat_members[0].id;
  newMemberName = ctx.message.new_chat_members[0].first_name;
  bot.telegram.restrictChatMember(ctx.chat.id, newMember);
  const keyboard = Keyboard.make([
    Key.callback('Answer1', 'Fail'),
    Key.callback('Answer2', 'Fail'),
    Key.callback('Answer3', 'Pass'),
    Key.callback('Answer4', 'Fail'),
    Key.callback('Answer5', 'Fail'),
  ]).inline();
    bot.telegram.sendMessage(ctx.chat.id, `Hello, ${newMemberName}!\nYou can post once You select Answer3`, keyboard).then(
        ({ message_id }) => { spamBlocker = message_id; });
});

bot.on("callback_query", function(callbackQuery) {
  let chatID = callbackQuery.update.callback_query.message.chat.id;
  if (callbackQuery.update.callback_query.data == "Pass") {
    bot.telegram.deleteMessage(chatID, spamBlocker);
    bot.telegram.restrictChatMember(chatID, newMember, [true, true, true, true]);
    bot.telegram.sendMessage(chatID, `${newMemberName}, ${welcomeMessage}`)
  } else {
    bot.telegram.kickChatMember(chatID, newMember);
    bot.telegram.deleteMessage(chatID, spamBlocker);
  }
});
boydster
  • 31
  • 5

1 Answers1

1

Thanks to the help of a kind stranger on the CodingHelp Discord, I was able to arrive at the following solution. The issue is restrictChatMember takes a chat ID, a member ID, and an object. The object takes key pairs (obvious in hind sight), so you tell it "can_send_messages": true, for example, and keep going for each line.

In my case, change this line:

bot.telegram.restrictChatMember(chatID, newMember, [true, true, true, true]);

to this:

bot.telegram.restrictChatMember(chatID, newMember, {"can_send_messages": true, "can_send_media_messages": true, "can_send_other_messages": true, "can_add_web_page_previews": true});
boydster
  • 31
  • 5