0

i try to run this code on my raspberry pi 3 but i get this problem

i tried to uninstall telegram library but he said E: Unable to locate package telegram

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
  File "/usr/local/lib/python2.7/dist-packages/python_telegram_bot-12.0.0b1-py2.7.egg/telegram/ext/__init__.py", line 28, in <module>
    from .updater import Updater
  File "/usr/local/lib/python2.7/dist-packages/python_telegram_bot-12.0.0b1-py2.7.egg/telegram/ext/updater.py", line 33, in <module>
    from telegram.utils.webhookhandler import (WebhookServer, WebhookAppClass)
  File "/usr/local/lib/python2.7/dist-packages/python_telegram_bot-12.0.0b1-py2.7.egg/telegram/utils/webhookhandler.py", line 27, in <module>
    from tornado.httpserver import HTTPServer
  File "/usr/local/lib/python2.7/dist-packages/tornado-6.0.2-py2.7-linux-armv7l.egg/tornado/httpserver.py", line 144
    def __init__(self, *args: Any, **kwargs: Any) -> None:
                            ^
SyntaxError: invalid syntax

i tried to uninstall telegram library but he said E: Unable to locate package telegram

https://github.com/python-telegram-bot/python-telegram-bot/issues/395

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import logging
import time
import RPi.GPIO as GPIO
import Adafruit_DHT
led = 5
DHT_pin = 27

GPIO.setmode(GPIO.BOARD)
GPIO.setup(led, GPIO.OUT, initial = 0)
# Enable logging
#logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
 #                   level=logging.INFO)

#logger = logging.getLogger(__name__)

# Define a few command handlers. These usually take the two arguments bot and
# update. Error handlers also receive the raised TelegramError object in error.
def on(bot, update):
    """Send a message when the command /start is issued."""
    GPIO.output(led, GPIO.HIGH)
    update.message.reply_text('LED turned ON')

def off(bot, update):
    """Send a message when the command /help is issued."""
    GPIO.output(led, GPIO.LOW)
    update.message.reply_text('LED turned OFF')

def hum(bot, update):
    """Send a message when the command /hum is issued."""
    humidity = Adafruit_DHT.read_retry(11, DHT_pin)[0]
    update.message.reply_text("Humidity: {} %".format(humidity))

def temp(bot, update):
    """Send a message when the command /temp is issued."""
    temperature = Adafruit_DHT.read_retry(11, DHT_pin)[1]
    update.message.reply_text("Temperature: {} C".format(temperature))

def echo(bot, update):
    """Echo the user message."""
    update.message.reply_text(update.message.text)

def error(bot, update, error):
    """Log Errors caused by Updates."""
    logger.warning('Update "%s" caused error "%s"', update, error)

def main():
    """Start the bot."""
    # Create the EventHandler and pass it your bot's token.
    updater = Updater("Enter your token here")

    # Get the dispatcher to register handlers
    dp = updater.dispatcher

    # on different commands - answer in Telegram
    dp.add_handler(CommandHandler("on", on))
    dp.add_handler(CommandHandler("off", off))
    dp.add_handler(CommandHandler("temp", temp))
    dp.add_handler(CommandHandler("hum", hum))

    # on noncommand i.e message - echo the message on Telegram
    dp.add_handler(MessageHandler(Filters.text, echo))

    # log all errors
    dp.add_error_handler(error)

    # Start the Bot
    updater.start_polling()

    # Run the bot until you press Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT. This should be used most of the time, since
    # start_polling() is non-blocking and will stop the bot gracefully.
    updater.idle()

if __name__ == '__main__':
    main()

1 Answer 1

3

Perhaps you are trying to run the code using Python2? The syntax error complains about the type annotations used in the library code. These typing hints were introduced in Python3.5.

Sign up to request clarification or add additional context in comments.

16 Comments

sorry but im new to programming,, so how i can make raspberry run it in Python3.5 ??
Perhaps you can start your script using: python3 my script.py
i get this now : sudo python3 main.py Traceback (most recent call last): File "main.py", line 4, in <module> from telegram.ext import Updater, CommandHandler, MessageHandler, Filters ImportError: No module named 'telegram'
Ah, probably you installed telegram using pip install telegram, is that correct? That defaults apparently to pip2 on your system. Try installing using pip3 install telegram instead and then try to run your script again.
thats right .. Now i tried pip3 install telegram and i see the folder in (/usr/local/lib/python3.5/dist-packages) but in cmd i get this : sudo python3 main.py Traceback (most recent call last): File "main.py", line 4, in <module> from telegram.ext import Updater, CommandHandler, MessageHandler, Filters ImportError: No module named 'telegram.ext'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.