Controlling HA with Telegram results in duplicated messages

I am using Telegram to control Home Assistant when on the go.

If I use this I only get a message once:

   def receive_telegram_callback(self, event_id, payload_event, *args):
        """Event listener for Telegram callback queries."""
        assert event_id == 'telegram_callback'
        data_callback = payload_event['data']
        callback_id = payload_event['id']
        chat_id = payload_event['chat_id']

        if data_callback == '/alarm':  # Keyboard editor:
            # Answer callback query
            state = self.get_state(self.args["alarm"])

            msg = "The alarm is currently {}".format(state)

            self.call_service(
                              'telegram_bot/answer_callback_query',
                              #'telegram_bot/send_message',
                              message=msg,
                              show_alert = True,
                              callback_query_id=callback_id)

But if I instead use this I get the message more than once:

      elif data_callback == '/light_status':

            group_skal = self.get_state("group.al_lys", attribute="entity_id")
            a = []
            for entity in group_skal:
                    friendly_name = self.get_state(entity, attribute="friendly_name")
                    friendly_state =  "tændt" if self.get_state(entity) == "on" else "slukket" if self.get_state(entity) == "off" else self.get_state(entity)
                    a.append(friendly_name+ ' er ' + friendly_state)
            list_text = ( '\n'.join(map(str, a)))
            msg = list_text
            self.call_service('telegram_bot/send_message',
                          target = chat_id,
                          message=msg,
                          disable_notification=True
                         )

I suspect it is because I am using “telegram_bot/send_message” instead of “telegram_bot/answer_callback_query”. Any good ideas? It works, but it is annoying to get the samme message up to 10 times :slight_smile:

Can you add some logging at the top of the method to show how often it’s being called and what the data is in each call? It seems that if you’re getting 10 messages then this must be getting called 10 times, or else there’s something else in the code you’re not showing that is causing a loop. I can’t imaging Home Assistant’s telegram/send_message being called once would result in 10 identical messages.

I inserted a “self.log” as this


elif data_callback == '/light_status':
            self.log("light message")
            group_skal = self.get_state("group.al_lys", attribute="entity_id")
            a = []
            for entity in group_skal:
                    friendly_name = self.get_state(entity, attribute="friendly_name")
                    friendly_state =  "tændt" if self.get_state(entity) == "on" else "slukket" if self.get_state(entity) == "off" else self.get_state(entity)
                    a.append(friendly_name+ ' er ' + friendly_state)
            list_text = ( '\n'.join(map(str, a)))
            msg = list_text
            self.call_service('telegram_bot/send_message',
                          target = chat_id,
                          message=msg,
                          disable_notification=True
                         )

And it seems like I am typically receiving the duplicated messages when I have sent the “/light_status” command and received the message and exited Telegram, and next time I upen Telegram again I receive the message again, as if Telegram is sending the message again

And do you see the “light message” log again when you see the message again?

If so, then, yes, it sounds like Telegram is resending the message for some reason. I use commands in my telegram setup. And, like your light_status, I use telegram_bot/send_message to respond. But I don’t get duplicates. I’m listening to the telegram_command and telegram_callback events, like you probably are.

The only difference in our setups is, potentially, I am using the “webhooks” platform. If you’re using polling, perhaps there is an issue there?

I use polling so that might be the case, but I don’t know if that makes sense since it is only when I go into the Telegram app that I receive the message again - meaning it is like the app doesn’t consider the job done and does it one or selveral more times. Do you have the same as “target”?

Yes. My full call_service() line looks like this:

        self.call_service(
            'telegram_bot/send_message',
            target=target,
            message=msg,
            disable_notification=False,
            inline_keyboard=[])

My target is “chat_id” which is: chat_id = payload_event[‘chat_id’] is your target the same?

In some cases, yes, my “target” is the chat_id that came in from the payload_event. In other cases, it’s a hardcoded chat_id (mine). It depends on the purpose of that particular method. But both seem to work just fine with no repeats.

I have tested a little bit more - when I click on one of the inline keyboard buttons I receive the desired message from Telegram, but my Telegram app on the phone continues to state “Loading…” in the top banner. If I then exit the app by just clicking the home button I receive a new message next time I go into the app on my phone, and sometimes messages in between. If I however forces my app on the phone to close completely (so it doesn’t run in the background either) and I open the app again I receive no duplicated messages.

So I feel pretty confident that the problem lies with the phone app or bot not considering the job done, and therefore sends the command multiple times to HA and thus I receive the messages multiple times. I wonder if it is due to that in all the non-problematic cases I use “telegram_bot/answer_callback_query’” in the call_service() part, but in the problem cases I use “telegram_bot/send_message” even though it is in a “Receive_telegram_callback” function?