Cannot get telegram_text event to fire in Appdaemon

Hi, I am running latest version of HA (0.97.2) and Appdaemon 4.0.2. I am trying to implement a Telegram bot as per https://www.wouterbulten.nl/blog/tech/talking-to-my-house-telegram-homeassistant-bot/. I have stripped the telegram.py code down to the absolute minimum :

import appdaemon.plugins.hass.hassapi as hass

class TelegramBot(hass.Hass):

	def initialize(self):
		# Start listening for Telegram updates
		try:
			self.listen_event(self.receive_telegram_text, "telegram_text")
			self.log("*** Successfully subscribed to telegram_text events")
		except:
			self.log("*** Failed to subscribe to telegram_text events") 
        

	def receive_telegram_text(self, event_id, payload_event, *args):
		# Do something with the text
		self.log("*** Received message from Telegram")

The problem is that I never see the telegram_text event being passed to my python code. The “*** Successfully subscribed to telegram_text events” message is received and despite being able to see the telegram_text event being fired by listening to it on the HA Developer Tools / Events page, it never seems to get to Appdaemon (i.e I don’t see the “*** Received message from Telegram” message in the Appdaemon log),
Any help solving this would be much appreciated.
Thanks

Are there any errors in the AppDaemon error log?
Did you try to subscribe to another event to see if it works?

Why do you have the try-except for the event listener? I can’t think of a scenario where you would enter the except clause. I doesn’t make sense to use try-except here in my opinion.

Try listening to all events and log them to see what events, if any, are received:

self.listen_event(self.receive_telegram_text)

and then log all events in the callback:

self.log(event_id)
self.log(payload_event)

Tomas / Burningstone,
Changed my code to listen for all events. Once again I received the message to say the subscription was successful, but no events are received by the callback function.
Try and except are there because I thought (maybe mistakenly - I am a newbie to python) that if the self.listen_event failed it would execute the ‘except’ branch of the code, so that I would know it had failed.
The Appdaemon log shows no errors.
Thanks for your assistance.

A little more info. On restart of appdaemon, my code which subscribes to all events results in the following in the appdaemon log:

2019-08-14 17:40:55.894599 INFO AppDaemon: Initializing app bot using class TelegramBot from module telegram
2019-08-14 17:40:55.897176 INFO bot: *** Successfully subscribed to all events
2019-08-14 17:40:55.898231 INFO AppDaemon: App initialization complete
2019-08-14 17:40:55.900365 INFO bot: appd_started
2019-08-14 17:40:55.900643 INFO bot: {}

I was thinking maybe my subscribe to the telegram_text event was being issued too early and I need to wait a while for appdaemon to start. However it does seem strange that I receive the appd_started event, but subsequently no other events are reported by the callback function.

EDIT: Thinking about it further, I cannot just wait for appd_started event to be fired as that would not work if I updated the bot app code, but did not restart appdaemon, or the bot code was reloaded by appdaemon for a reason other than a restart of appdaemon.

Try to fire an event manually on the events page in HA, like Event Type test and {"test": "test event"} as the event data. It should be received by the appdaemon app.

Tomas
Just tried firing the test event, as suggested. Received the message that the event was fired successfully, but nothing reported by my appdaemon script, which is subscribed to all events.

This is the appdaemon.log but there’s also an error.log

Can you please post the json reponse received in the HA Event page when you subscribe to the “telegram_text” event?

I have the following in appdaemon.yaml

log:
  logfile: STDOUT
  errorfile: STDOUT

So I presume any error messages would appear in the appdaemon log. If not correct, how do I locate the error log?

Here is the json from listening to the telegram_text event on the HA Developer / Event page (personal info redacted):

Event 0 fired 6:12 PM:

{
    "event_type": "telegram_text",
    "data": {
        "user_id": 00000000,
        "from_first": "xxxxxxx",
        "chat_id": 00000000,
        "text": "Some test text"
    },
    "origin": "LOCAL",
    "time_fired": "2019-08-14T17:12:27.459171+00:00",
    "context": {
        "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "parent_id": null,
        "user_id": null
    }
}

Ah I understand, that’s correct like this.

Are other Appdaemon apps you have working as expected? If not, there might be a connection issue between appdaemon and HA.

Try adding this to your initialize function to test the connection:

self.set_state("sensor.my_test_sensor", state= "my_test_state")

This will create a temporary sensor named sensor.my_test_sensor that should now show up in the state page in HA with the state my_test_state

If you add the the following to the receive_telegram_text function and fire an event what do you see?

Self.log(event_id)
Self.log(payload)
Self.log(args)

I only have one other app running, but that does not make use of any events or HA entity status, it simply drops a MariaDB table for the Traccar plugin which can cause some issues if left lying around.
Appdaemon seems to connect OK as I can see the connection message in the log:

2019-08-14 18:09:10.974025 INFO AppDaemon: HASS: Connected to Home Assistant 0.97.2

There are no disconnection message either.

Ok, the connection looks fine.

Hi Tomas, that code results in :

2019-08-14 18:21:54.251974 WARNING AppDaemon: bot: Entity sensor.my_test_sensor not found in AppDaemon

That would be expected. Please look in the HA state explorer page for the sensor and se if it now exists.

Tomas,
After executing that code and seeing the message:

WARNING AppDaemon: bot: Entity sensor.my_test_sensor not found in AppDaemon

in the log, sensor.my_test_sensor does NOT appear in the list of entities on the Developer Options / States page of HA…

I don’t think it should make any difference, but just in case, this is a HASS.IO installation and Appdaemon was installed via the Hass.io Add-on-store