Writing a python door sensor with telegram bot integration

Question:

*This is a follow-up question to a previous problem I had writing a similar code, however the telgram-send library that I was using has not been updated since last year and I cannot configure it to make it work with my bot anymore; it gives me an error:
ImportError: cannot import name 'MAX_MESSAGE_LENGTH' from 'telegram.constants' (/usr/local/lib/python3.10/dist-packages/python_telegram_bot-20.1-py3.10.egg/telegram/constants.py) *

*I decided to use the python-telegram-bot library instead, which seems to be more regularly updated and more broadly used. *

Now with the question:

I’m trying to build a Python script that sends a message to Telegram whenever a door sensor is triggered. I’m using the python-telegram-bot library for this. However, when I try to run the script, I get the following error:

AttributeError: 'Updater' object has no attribute 'dispatcher'

Here’s my code:

from time import sleep
import RPi.GPIO as GPIO
import telegram
from telegram.ext import Updater, CommandHandler
from queue import Queue

sensor = 21 # The sensor is on GPIO pin 21
OPEN_DOOR = 1 # The state of the sensor is 1 when the door is open
CLOSED_DOOR = 0 # The state of the sensor is 0 when the door is closed

DOOR_LAST_STATE = -1

# Create a Telegram bot object
bot = telegram.Bot(token='YOUR_TELEGRAM_BOT_TOKEN')

# Define a function to send a message to Telegram
def send_telegram_message(message):
    bot.send_message(chat_id='YOUR_CHAT_ID', text=message)

# Define a function to handle the door state change
def door_state_change(update, context):
    global DOOR_LAST_STATE

    curr_state = GPIO.input(sensor)
    if curr_state == OPEN_DOOR: # If the door is open
        if DOOR_LAST_STATE == CLOSED_DOOR: # And the las known state is "closed"
            message = "The door is open!"
            print(message) # Send the message 
            send_telegram_message(message)
            DOOR_LAST_STATE = OPEN_DOOR # And upate the last known state to "open"

        elif DOOR_LAST_STATE == OPEN_DOOR: # But if the last known state is the same as the current
            print("The door is still open")
            # Do Nothing

        else:
            print("The door is at unknow state")

    elif curr_state == CLOSED_DOOR: # If the door is closed
        if DOOR_LAST_STATE == OPEN_DOOR:  # And the last know state is open
            message = "The door is closed!"
            print(message) # Send the message
            send_telegram_message(message)
            DOOR_LAST_STATE = CLOSED_DOOR # And update the last known state to "closed"

        elif DOOR_LAST_STATE == CLOSED_DOOR: # If the last known state is te same as the current
            print("The door is still closed")
            # Do Nothing

        else:
            print("The door is at unknow state")

    else:
        print("The door is at unknow state")

# Create an updater object and add the door_state_change handler to it
updater = Updater(bot=bot, update_queue=Queue())
updater.dispatcher.add_handler(CommandHandler('door_state_change', door_state_change))

if __name__ == '__main__':
    # Sets the GPIO pinout to BCM
    GPIO.setmode(GPIO.BCM)
    # Pull the sensor to 3.3v when not engaged, It will be pulled to ground when engaged
    GPIO.setup(sensor,GPIO.IN,pull_up_down=GPIO.PUD_UP)

    # Recovering current state of the door
    DOOR_LAST_STATE = GPIO.input(sensor)

    # Start the updater
    updater.start_polling()

    print("Program started!")

    while True:
        sleep(1)  # What we do we will always sleep at the end

# Stop the updater when the program is interrupted
updater.dispatcher.stop()

I’m not sure what’s causing this error. Can anyone help me out?


This is the error that I am receiving when running the code:

ubuntu@ubuntu:~$ sudo python3 door.py

Traceback (most recent call last):
  File "/home/ubuntu/door.py", line 58, in <module>
    updater.dispatcher.add_handler(CommandHandler('door_state_change', door_state_change))
AttributeError: 'Updater' object has no attribute 'dispatcher'

I already tried several iterations of the code that I managed to stitch up from different sources, but none of that worked. It keep giving me the same errors. I’m by no means an expert coder, so I’m pretty lost here. Any help is appreciated.

Asked By: Alex

||

Answers:

I think that you are using the new version of python-telegram-bot, but you are using the old syntax.

New syntax looks like:

app = ApplicationBuilder().token("YOUR TOKEN HERE").build()
app.add_handler(CommandHandler("hello", hello))
app.run_polling()

Or just downgrade python-telegram-bot, maybe python-telegram-bot==13.14 ?

Answered By: ScreamOFF