sudo apt install python3-pip
 pip3 install telepot

import telepot
from telepot.loop import MessageLoop
from datetime import datetime
import RPi.GPIO as GPIO
from time import sleep

red = 22
yellow = 23

now = datetime.now()
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(red, GPIO.OUT)
GPIO.output(red, False)

GPIO.setup(yellow, GPIO.OUT)
GPIO.output(yellow, False)

def action(msg):
    chat_id = msg["chat"]["id"]
    command = msg.get("text", "").lower()
    print("Received:", command)

    if "red" in command:
        if "on" in command:
            GPIO.output(red, True)
            telegram_bot.sendMessage(chat_id, "Red light ON")
        elif "off" in command:
            GPIO.output(red, False)
            telegram_bot.sendMessage(chat_id, "Red light OFF")
        else:
            telegram_bot.sendMessage(chat_id, "Specify ON or OFF for red")

    elif "yellow" in command:
        if "on" in command:
            GPIO.output(yellow, True)
            telegram_bot.sendMessage(chat_id, "Yellow light ON")
        elif "off" in command:
            GPIO.output(yellow, False)
            telegram_bot.sendMessage(chat_id, "Yellow light OFF")
        else:
            telegram_bot.sendMessage(chat_id, "Specify ON or OFF for yellow")

    else:
        telegram_bot.sendMessage(chat_id, "Invalid command (use red/yellow + on/off)")

telegram_bot = telepot.Bot("YOUR BOT TOKEN")
print(telegram_bot.getMe())
MessageLoop(telegram_bot, action).run_as_thread()

try:
    while True:
        sleep(10)
finally:
    GPIO.output(red, 0)
    GPIO.output(yellow, 0)
    GPIO.cleanup()