0

I'm building a telegram chatbot in nodejs that will work on webhook. Currently, bot hits my webhook URL with every message in chat. Is it possible to only push payload on command execution for the bot?

So I would like only to get the payload from the chat when the user executes /test command and any other messages in the chat should not git to my URL.

#Edit

Current setup of privacy

'Enable' - your bot will only receive messages that either start with the '/' symbol or mention the bot by username. 'Disable' - your bot will receive all messages that people send to groups. Current status is: ENABLED

I want to use bot in groups and in direct chat with bot - me so I can test things.

I created a test group added the bot and whatever I type into the group I can see in logs of the Webhook URL. So no matter if its /test or some text it's beeing pushed

#Edit 2

This it what I receive in my webhook URL (normal chat text, and bot command)

{
  "update_id": 1,
  "channel_post": {
    "message_id": 65,
    "sender_chat": {
      "id": -1,
      "title": "Tssos",
      "type": "channel"
    },
    "chat": {
      "id": -1,
      "title": "Tssos",
      "type": "channel"
    },
    "date": 1,
    "text": "test"
  }
}
{
  "update_id": 1,
  "channel_post": {
    "message_id": 67,
    "sender_chat": {
      "id": -1,
      "title": "Tssos",
      "type": "channel"
    },
    "chat": {
      "id": -1,
      "title": "Tssos",
      "type": "channel"
    },
    "date": 1,
    "text": "/test@TESTss_bot",
    "entities": [
      {
        "offset": 0,
        "length": 23,
        "type": "bot_command"
      }
    ]
  }
}
Mugetsu
  • 1,739
  • 2
  • 20
  • 41
  • #1 Is your bot in a single chat or group? #2 Do you receive all messages at once? I mean if chat has 5 lines and someone write a new line, Is your bot receiving 6 lines? #3 Check this https://github.com/jrichardsz/telegram-bot-starter – JRichardsz Jun 03 '21 at 23:30
  • I'm focusing on the group. No I receive messages one after another. – Mugetsu Jun 04 '21 at 00:04
  • 1
    If you see this line in my bot template https://github.com/jrichardsz/telegram-bot-starter/blob/989484f542093522b5ef8e8b0b40f3e2ec0426fc/endpoints/IncomingNewMessageEndpoint.js#L18 just one message is received. – JRichardsz Jun 04 '21 at 00:35

2 Answers2

2

You have to use @BotFather to set your bot privacy:

  1. Send /mybots command to @BotFather
  2. Select your bot by its username
  3. Select Bot Settings
  4. Select Group Privacy
  5. Enable or disable your bot's privacy

If Privacy Mode is enabled, your bot only receive messages which are start with slash /

Ehsan
  • 265
  • 3
  • 8
  • I have this enabled and In my webhook url I see all messages that I post in chat – Mugetsu Jun 03 '21 at 23:35
  • this only works for group chats. you have to ignore other messages which are sent at private chats manually using JavaScript (NodeJS) and check the begging of the messages text for having '/', for ex: text.startsWith("/") – Ehsan Jun 03 '21 at 23:39
  • So there is No way to protect my URL from spamming in the chat? Say 1000 users typing then all this gonna be pushed to my Webhook URL? – Mugetsu Jun 03 '21 at 23:41
  • Telegram servers might prevent that. also you can use Nginx for load balance between multiple servers. Enabling Privacy Mode have nothing to do with spamming. Spammers can send their messages text using slash at the beginning. After updating privacy mode try remove the bot from group chat and add it again. – Ehsan Jun 03 '21 at 23:47
  • Didn't helped. I see when posting normal chat message that it contains only the text where as when I do the command there is additional property "entities" which contains bot_command value. – Mugetsu Jun 04 '21 at 00:03
  • "Say 1000 users typing then all this gonna be pushed to my Webhook URL".. this is the main purpose of bots: Ingest all line chats and perform some task if text chat is a valid operation. This helps if you want to create a human commands instead /img /get-time. You could choose between /commands or a human command like: get me the time – JRichardsz Jun 04 '21 at 00:38
  • Yeah I know. But I should be able to restrict what I would like to receive. So in my case I would only care about /test command payload to be pushed to my URL instead whole chat 24/7 – Mugetsu Jun 04 '21 at 07:58
  • Telegram servers will not filter out payload for you. you have to do it on your server. tip: you can find spammers ID then restrict them by using restrictChatMember method. – Ehsan Jun 05 '21 at 00:24
0

Alright, so I found the answer to my problem.

I was adding a bot as an administrator to the chat that's why he was sending all messages. Admins bot will push all chat messages. Adding a bot like a normal user solved the issue and now I only receive /command updates.

Privacy mode is enabled by default for all bots, except bots that were added to the group as admins (bot admins always receive all messages). It can be disabled, so that the bot receives all messages like an ordinary user (the bot will need to be re-added to the group for this change to take effect). We only recommend doing this in cases where it is absolutely necessary for your bot to work — users can always see a bot's current privacy setting in the group members list. In most cases, using the force reply option for the bot's messages should be more than enough.

Docs

Mugetsu
  • 1,739
  • 2
  • 20
  • 41