I am trying to create a Telegram bot so that I can turn off my pc remotely. I’ve created all the code and everything seems correct, I’ve also installed all the libraries I use, but the telegram bot library doesn’t detect it, I’ve already tried updating it to the latest version but nothing, it keeps not detecting it. I have followed the step by step guide on their GitHub several times but nothing changes. Anyway here is the code I programmed and the error it gives me:
Code programmed
import telegram
import os
import daemon
# Token
TOKEN = '<My Token>'
# Crea bot
bot = telegram.Bot(token=TOKEN)
# Funzione bot
def turn_off_pc():
# Comando di spegnimento
os.system('shutdown /s /t 0')
# Imput di spegnimento
def handle_message(update, context):
message = update.message.text
if message == '/off':
turn_off_pc()
else:
pass
# Gestore comandi bot
def main():
# Crea il gestore dei comandi del bot
from telegram.ext import CommandHandler, Updater
command_handler = CommandHandler('off', handle_message)
updater = Updater(token=TOKEN, use_context=True)
dispatcher = updater.dispatcher
dispatcher.add_handler(command_handler)
# Start in background
with daemon.DaemonContext():
updater.start_polling()
updater.idle()
# Avvia bot
if __name__ == '__main__':
main()
Error
Traceback (most recent call last):
File "D:PythonWindows Shutter.py", line 1, in <module>
import telegram
ModuleNotFoundError: No module named 'telegram'
Oh yes, I use Windows 11 22H2 kb5023778 (ty @SUTerliakov)
2
Answers
First you have to run command prompt in administrator mode. To run in administrator mode:
Then type the following:
pip install python-telegram-bot